图书管理系统--Java版本(2.0)
之前的图书管理系统在删除图书这个操作模块存在一定的bug,这次是修正之后的版本。
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 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; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + //", isBorrowed=" + isBorrowed + ((isBorrowed == true)? "已经借出" : "未借出" ) + '}'; } }
package book; public class BookList { public Book[] elem = new Book[10]; private int usedSize; public BookList(){ this.elem[0] = new Book("三国演义","罗贯中",78,"小说"); this.elem[1] = new Book("水浒传","施耐庵",78,"小说"); this.elem[2] = new Book("西游记","吴承恩",98,"小说"); this.usedSize = 3; } public void setBook(int pos,Book book){ this.elem[pos] = book; } public Book getBook(int pos){ return this.elem[pos]; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } }
package operation; import book.Book; import book.BookList; public class AddOperation implements IOperation{ public void work(BookList bookList) { System.out.println("新增图书!"); System.out.println("请输入书名:"); String name = this.sc.next(); System.out.println("请输入作者:"); String author = this.sc.next(); System.out.println("请输入价格:"); int price = this.sc.nextInt(); System.out.println("请输入类型:"); String type = this.sc.next(); Book book = new Book(name,author,price,type); int currentSize = bookList.getUsedSize(); bookList.setBook(currentSize,book); bookList.setUsedSize(currentSize+1); } }
package operation; import book.Book; import book.BookList; public class BorrowOperation implements IOperation{ public void work(BookList booklist){ System.out.println("租借图书!"); String name = this.sc.next(); 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(book.isBorrowed()); break; } } } }
package operation; import book.Book; import book.BookList; public class DelOperation implements IOperation{ public void work(BookList booklist){ System.out.println("删除图书!"); String name = this.sc.next(); int currentSize = booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = booklist.getBook(i); Book book1 = booklist.getBook(i+1); if(book.getName().equals(name)) { if(booklist.getBook(i+2) == null){ book1 = null; } booklist.setBook(i,booklist.getBook(i+1)); break; } } } }
package operation; import book.BookList; import book.Book; public class DisplayOperation implements IOperation{ public void work(BookList booklist){ System.out.println("展示图书!"); int currentSize = booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = booklist.getBook(i); System.out.println(book); } } }
package operation; import book.BookList; public class ExitOperation implements IOperation{ public void work(BookList booklist){ System.out.println("退出系统!"); System.exit(0); } }
package operation; import book.BookList; import book.Book; public class FindOperation implements IOperation{ public void work(BookList booklist){ System.out.println("查找图书!"); String name = this.sc.next(); int currentSize = booklist.getUsedSize(); for (int i = 0; i < currentSize; i++) { Book book = booklist.getBook(i); if(book.getName().equals(name)) { System.out.println(book); break; } } } }
package operation; import book.BookList; import java.util.Scanner; public interface IOperation { Scanner sc = new Scanner(System.in); void work(BookList booklist); }
package operation; import book.Book; import book.BookList; public class ReturnOperation implements IOperation{ public void work(BookList booklist){ System.out.println("归还图书!"); String name = this.sc.next(); 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(book.isBorrowed()); break; } } } }
import book.BookList; import operation.IOperation; import user.Admin; 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.next(); System.out.println("请输入你的身份:1是管理员,0是普通用户"); int choice = scanner.nextInt(); if(choice == 1){ return new Admin(name); }else{ return new NormalUser(name); } } public static void main(String[] args) { BookList bookList = new BookList(); User user = login(); while(true){ int ch = user.menu(); IOperation iOperation = user.doOperation(ch); iOperation.work(bookList); } } }