建包和类:
Book
Book:
package Book; public class Book { private String name; private String author; private int price; private String type; private boolean isBorrowed; public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } 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; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed == true)?" 已借出 ":" 未借出 ")+ //", isBorrowed=" + isBorrowed + '}'; } }
BookList:
package Book; public class BookList { private Book[] books = new Book[10];//创建一个数组 private int UsedSize;//有效的使用长度//实践上放书的个数 public BookList(){ this.books[0]=new Book("三国演义","罗贯中",10,"小说"); this.books[1]=new Book("红楼梦","曹雪芹",20,"小说"); this.books[2]=new Book("西游记","吴承恩",30,"小说"); this.UsedSize = 3;//书架上的书 } //用来访问数组的长度 public int getUsedSize() { return UsedSize; } public void setUsedSize(int usedSize) { UsedSize = usedSize; } //用来访问数组下标,所以我们要对这个进行修改,使其返回数组的下标 public Book getBook(int pos) { return books[pos]; } //给某一个位置放一本书 public void setBook(int pos,Book book) { this.books[pos] = book; } //得到书的数组的长度 public Book[] getBooks() { return books; } }
Main:
package Book; import User.User; import User.AdminUser; import User.NomalUser; import java.util.Scanner; public class Main { public static User login(){ Scanner scanner =new Scanner(System.in); System.out.println("请输入你的名字: "); String name = scanner.nextLine(); System.out.println("请输入你的身份:1.管理员 2.普通用户 "); int choice = scanner.nextInt();// 1 或者 2 if(choice == 1){ return new AdminUser(name); }else{ return new NomalUser(name); } } public static void main(String[] args) { BookList bookList = new BookList(); User user =login(); while(true){ //看user引用哪个对象,然后下一步调用哪个菜单 int choice = user.menu(); //根据返回值 调那个对象 使用哪个方法 //确认对象 包含了 哪些方法? //在进行调用子类的对象是,构造方法会初始化好对应的操作对象 //user引用了哪个对象 user.doIoperation(choice,bookList); } } }
User
User:
package User; import Book.BookList; import ioperations.IOperation; public abstract class User { protected String name; //此时这个数组没有初始化 protected IOperation[] iOperations; public User(String name){ this.name = name; } public abstract int menu(); //构造一个方法去实现调用 //通过数组下标使用数组进行调用,选择的方法 public void doIoperation(int choice , BookList bookList){ iOperations[choice].work(bookList); //通过点号访问方法 } }
AdminUser:
package User; import ioperations.*; 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 ShowOperation(), }; } public int menu(){ System.out.println("**欢迎"+this.name+"进入图书系统**"); System.out.println("******管理员菜单******"); 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); System.out.println("请输入你选择的数字: "); int choice = scanner.nextInt(); return choice; } }
NomalUser:
package User; import ioperations.*; import java.util.Scanner; public class NomalUser extends User{ public NomalUser(String name) { super(name); //创建新的数组,new 新的方法 this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } public int menu(){ System.out.println("****欢迎"+this.name+"进入图书系统****"); System.out.println("*******普通用户菜单*******"); 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); System.out.println("请输入你选择的数字: "); int choice = scanner.nextInt(); return choice; } }
ioperations
ioperation:
package ioperations; import Book.BookList; public interface IOperation { public void work(BookList bookList); }
AddOperation:
package ioperations; import Book.Book; import Book.BookList; import java.util.Scanner; public class AddOperation implements IOperation { //新增图书 public void work(BookList bookList) { System.out.println("新增图书....."); //1.判断是否已经满了 //2.输入各种成员变量 构建对象 // 3.放到数组的最后一个位置 //4.us++ //1.判满 int currentSize = bookList.getUsedSize(); if (currentSize == bookList.getBooks().length) { System.out.println("书架满了,不能放了"); return; } 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 newbook = new Book(name, author, price, type); //3.判断有没有这本书不能插入 for (int i = 0; i < currentSize; i++) { //第一步:从书架上拿到书 Book book = bookList.getBook(i); //通过书得到书的名字,然后用equals于输入的名字进行对比 if (book.getName().equals(name)) { System.out.println("有这本书不能插入"); System.out.println(book); return; } //4.插入 bookList.setBook(currentSize, newbook); bookList.setUsedSize(currentSize + 1); System.out.println("新增图书成功!!"); } } }
BorrowOperation:
package ioperations; import Book.Book; import Book.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation { //借阅图书 public void work(BookList bookList) { System.out.println("借阅图书....."); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要借阅的书名:"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { //第一步:从书架上拿到书 Book book = bookList.getBook(i); //通过书得到书的名字,然后用equals于输入的名字进行对比 if (book.getName().equals(name)) { if(book.isBorrowed()) { System.out.println("该书已经被借出!!"); return; } System.out.println("找到了这本书"); System.out.println(book); book.setBorrowed(true); System.out.println("借阅成功!!"); return; } } System.out.println("没有你要找的书"); } }
DelOperation:
package ioperations; import Book.Book; import Book.BookList; import java.util.Scanner; public class DelOperation implements IOperation{ //删除图书 public void work(BookList bookList) { System.out.println("删除图书....."); //找到自己要删除书的位置,然后后面往前面覆盖 us--; Scanner scanner = new Scanner(System.in); System.out.println("请输入你要删除的书名:"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); //记录下标 pos int pos = -1; int i = 0; for (i = 0; i < currentSize; i++) { //第一步:从书架上拿到书 Book book = bookList.getBook(i); //通过书得到书的名字,然后用equals于输入的名字进行对比 if (book.getName().equals(name)) { System.out.println("找到了这本书"); System.out.println(book); pos = i; break; } } if (i == currentSize) { System.out.println("没有你要删除的书"); return; } //开始删除... for (int j = pos; j <currentSize-1 ; j++) { //bookList[j] = bookList[j+1]; Book book = bookList.getBook(j+1); bookList.setBook(j,book); } bookList.setBook(currentSize-1,null); bookList.setUsedSize(currentSize-1); System.out.println("删除成功!!!"); } }
ExitOperation:
package ioperations; import Book.BookList; public class ExitOperation implements IOperation{ //退出系统 public void work(BookList bookList){ System.out.println("退出系统....."); //调用exit 设置为0 正常退出 System.exit(0); } }
FindOperation:
package ioperations; import Book.Book; import Book.BookList; import java.util.Scanner; public class FindOperation implements IOperation { //查找图书 public void work(BookList bookList) { System.out.println("查找图书....."); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要查找的书名:"); String name = scanner.nextLine(); //获得书架的长度存到currentSize中 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { //第一步:从书架上拿到书 Book book = bookList.getBook(i); //通过书得到书的名字,然后用equals于输入的名字进行对比 if (book.getName().equals(name)) { System.out.println("找到了这本书"); System.out.println(book); return; } } System.out.println("没有你要找的书"); } }
ReturnOperation:
package ioperations; import Book.Book; import Book.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation{ //归还图书 public void work(BookList bookList){ System.out.println("归还图书....."); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要归还的书名:"); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize; i++) { //第一步:从书架上拿到书 Book book = bookList.getBook(i); //通过书得到书的名字,然后用equals于输入的名字进行对比 if (book.getName().equals(name)) { if(book.isBorrowed()) { book.setBorrowed(false); System.out.println("归还成功!!"); return; } } } System.out.println("没有你要归还的图书!!"); } }
ShowOperation:
package ioperations; import Book.Book; import Book.BookList; public class ShowOperation implements IOperation{ //显示图书 public void work(BookList bookList){ System.out.println("显示图书....."); //currentSize 用来获取书架长度 int currentSize = bookList.getUsedSize(); for (int i = 0; i < currentSize ; i++) { Book book = bookList.getBook(i); System.out.println(book); } } }