前言
本篇文章我们来学习一个综合案例——图书管理系统,是对继承,封装和多态的一个小练习,通过这个案例可以加深我们对语法的理解。
一、实现的功能
实现对图书的管理,我们登录时有两个身份,一个是图书管理员,一个是普通用户,这两个实现的功能各不一样。
二、具体的代码
有三个包和一个main函数。
book包(书的信息和书架)
book包:
book类:
package book; public class book { private String name; //书名 private String author;//作者 private int price;//价格 private String type;//类型如小说散文等 private boolean isBorrowed;//是否被借出 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } public book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } @Override public String toString() { return "book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed==true)?"已经借出":"未借出")+'}'; } }
booklist类:
package book; public class booklist { private book[]books=new book[10]; //最多可以放十本书 private int usedSize;//实时记录,当前books这个数组中有多少本书。 public booklist(){ books[0]=new book("三国演义","罗贯中",19,"小说"); books[1]=new book("西游记","吴承恩",19,"小说"); books[2]=new book("红楼梦","曹雪芹",19,"小说"); usedSize=3; } public book getBook(int pos){ return books[pos]; //找书 } public void setBook(int pos,book Book){ books[pos]=Book; //增加书 } public void setUsedSize(int size){ usedSize=size; //实时修改当前书架上书的个数 } public int getUsedSize(){ //实时获取当前书的个数 return usedSize; } }
operation包(实现具体的功能)
AddOperation:
package operation; import book.book; import book.booklist; import java.util.Scanner; public class AddOperation implements IOperation { @Override public void work(booklist Booklist) { System.out.println("新增图书"); Scanner scanner = new Scanner(System.in); System.out.println("请输入图书的名字:"); String name=scanner.nextLine(); System.out.println("请输入图书的作者:"); String author=scanner.nextLine(); System.out.println("请输入图书的类型:"); String type=scanner.nextLine(); System.out.println("请输入图书的价格:"); int price=scanner.nextInt(); book Book=new book(name,author,price,type); int currentSize=Booklist.getUsedSize(); Booklist.setBook(currentSize,Book); Booklist.setUsedSize(currentSize+1); System.out.println("新增图书成功"); } }
BorrowOperation :
package operation; import book.book; import book.booklist; import java.util.Scanner; public class BorrowOperation implements IOperation { @Override public void work(booklist Booklist) { System.out.println("请输入借阅图书的名字"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = Booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { book Book = Booklist.getBook(i); if (Book.getName().equals(name)) { Book.setBorrowed(true); System.out.println("借阅成功"); return; } } } }
DelOperation:
package operation; import book.book; import book.booklist; import sun.security.provider.certpath.CertId; import java.util.Scanner; public class DelOperation implements IOperation{ @Override public void work(booklist Booklist) { System.out.println("请输入删除图书的名字"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = Booklist.getUsedSize(); int SIZE=0; for (int i = 0; i < currentSize; i++) { book Book = Booklist.getBook(i); if (Book.getName().equals(name)) { SIZE=i; break; } } for(int i=SIZE;i<currentSize-1;i++){ book Book= Booklist.getBook(i+1); Booklist.setBook(i,Book); Booklist.setUsedSize(currentSize-1); System.out.println("删除成功"); } } }
DisplayOperation:
package operation; import book.book; import book.booklist; public class DisplayOperation implements IOperation{ @Override public void work(booklist Booklist) { int currentSize = Booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { book Book = Booklist.getBook(i); System.out.println(Booklist.getBook(i)); } } }
ExitOperation:
package operation; import book.booklist; public class ExitOperation implements IOperation { @Override public void work(booklist Booklist) { System.out.println("退出系统"); System.exit(0); } }
FindOperation:
import java.util.Scanner; public class FindOperation implements IOperation{ @Override public void work(booklist Booklist) { System.out.println("查找图书"); int currentSize = Booklist.getUsedSize(); System.out.println("请输入图书的名字"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0; i < currentSize; i++) { book Book = Booklist.getBook(i); if (Book.getName().equals(name)) { System.out.println("找到了这本书"); System.out.println(Book); return; } } System.out.println("没有你要找的这本书"); } }
ReturnOperation:
package operation; import book.book; import book.booklist; import java.util.Scanner; public class ReturnOperation implements IOperation { @Override public void work(booklist Booklist) { System.out.println("归还图书"); System.out.println("请输入归还图书的名字"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = Booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { book Book = Booklist.getBook(i); if (Book.getName().equals(name)) { Book.setBorrowed(false); System.out.println("归还成功"); return; } } } }
接口: IOperation
package operation; import book.booklist; public interface IOperation { void work(booklist Booklist); }
user包(管理员 用户 main方法)
AdminUser:
package user; import book.*; import operation.*; import java.util.Scanner; public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation(), }; } public int menu(){ System.out.println("hello"+this.name+"欢迎来到图书小练习"); System.out.println("1.查找图书"); System.out.println("2.新增图书"); System.out.println("3.删除图书"); System.out.println("4.显示图书"); System.out.println("0.退出系统"); System.out.println("请输入你的操作"); Scanner scanner=new Scanner(System.in); int choice= scanner.nextInt(); return choice; } }
NormalUser:
package user; import operation.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation(), }; } public int menu(){ System.out.println("hello"+this.name+"欢迎来到图书小练习"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("请输入你的操作"); Scanner scanner=new Scanner(System.in); int choice= scanner.nextInt(); return choice; } }
User:
package user; import book.booklist; import operation.IOperation; public abstract class User { protected String name; protected IOperation[]iOperations; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, booklist Booklist){ this.iOperations[choice].work(Booklist); } }
main方法:
import book.booklist; import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { public static User login(){ System.out.println("请输入你的姓名"); Scanner scanner =new Scanner(System.in); String name=scanner.nextLine(); System.out.println("请输入你的身份:1.管理员 2.用户"); int choice=scanner.nextInt(); if(choice==1){ return new AdminUser(name) ; } else { return new NormalUser(name); } } public static void main(String[] args) { booklist Booklist = new booklist(); User user = login();//登录 while(true) { int choice = user.menu(); //动态绑定 user.doOperation(choice, Booklist); } } }
总结
这个小程序可以加深我们对继承封装和多态的理解,可以让我们更熟悉语法。
本篇文章内容结束,感谢大家观看。如果意见或建议,可以在评论区留言,您的点赞是我更新的动力。我们下篇文章再见。