阅读量:0
目录
从三个模块来实现图书管理系统:书本、用户、实现的功能
实现过程:
首先在Book包下定义一个book类,包含书名、作者、价格、类型、是否借出成员变量。
这些成员变量都是私有的,因此需要构建get和set方法,为外部提供访问权限。
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; } //因为成员变量是private类型,需要提供get和set为外部提供访问权限 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 == true) ? "已借出" : "未借出") + // ", isBorrowed=" + isBorrowed + '}'; } }
然后构建一个类似于书架的功能,创建一个书的数组,并为私有成员变量提供set和get方法。
BookList类:
package Book; public class BookList { private Book[] books = new Book[10];//书的数组 private int usedSize; public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } public Book getBooks(int pos) { return books[pos]; } public void setBooks(Book book, int pos) { this.books[pos] = book; } //提供构造方法 public BookList() { this.books[0] = new Book("三国演义", "罗贯中", 10, "小说"); this.books[1] = new Book("红楼梦", "曹雪芹", 10, "小说"); this.books[2] = new Book("西游记", "吴承恩", 10, "小说"); this.usedSize = 3; } }
在User包中构建一个User抽象类,作为管理员和普通用户的父类,内部定义共同的功能。
构建一个功能数组来实现不同用户对应的不同功能。
User类:
package User; import Book.BookList; import Operation.Ioperation; import java.util.Scanner; public abstract class User { protected String name; public Ioperation[] ioperations;//创建功能数组 public User(String name) { this.name = name; } public abstract int menu(); public static User login() { Scanner sc = new Scanner(System.in); System.out.print("请输入你的名字:"); String username = sc.nextLine(); System.out.println("请输入你的身份:1.管理员 2.普通用户"); int userType = sc.nextInt(); if (userType == 1) { return new AdminUser(username); } else if (userType == 2) { return new NormalUser(username); } else { return null; } } public void doWork(int choice, BookList bookList) { this.ioperations[choice].work(bookList); } }
AdminUser类:
package User; 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 ShowOperation() }; } @Override 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.print("请输入你的操作:"); int choice = -1; Scanner sc = new Scanner(System.in); choice = sc.nextInt(); return choice; } }
NormalUser类:
package User; import Operation.*; import java.util.Scanner; public class NormalUser extends User { public NormalUser(String username) { super(username); this.ioperations = new Ioperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } @Override 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.print("请输入你的操作:"); int choice = -1; Scanner sc = new Scanner(System.in); choice = sc.nextInt(); return choice; } }
Operation包用来实现功能,构建一个接口对这些功能进行一个规范:
Ioperation接口:
package Operation; import Book.BookList; public interface Ioperation { public void work(BookList bookList); }
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.print("请输入书的名字:"); String bookName = scanner.nextLine(); System.out.print("请输入书的作者:"); String author = scanner.nextLine(); System.out.print("请输入书的类型:"); String type = scanner.nextLine(); System.out.print("请输入书的价格:"); int price = scanner.nextInt(); for (int i = 0; i < bookList.getUsedSize(); i++) { if (bookName.equals(bookList.getBooks(i).getName())) { System.out.println("该本书已存在"); return; } } bookList.setBooks(new Book(bookName, author, price, type), bookList.getUsedSize()); System.out.println("新增成功!"); bookList.setUsedSize(bookList.getUsedSize() + 1); } }
BorrowOperation类:
package Operation; 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); System.out.print("请输入要借的书的名字:"); String bookName = scanner.nextLine(); int x = -1; for (int i = 0; i < bookList.getUsedSize(); i++) { if (bookName.equals(bookList.getBooks(i).getName()) && !(bookList.getBooks(i).isBorrowed())) { bookList.getBooks(i).setBorrowed(true); x = 1; System.out.println("借阅成功!"); return; } } if (x == -1) { System.out.println("没有该本书"); } } }
DelOperation类:
package Operation; import Book.BookList; import java.util.Scanner; public class DelOperation implements Ioperation { @Override public void work(BookList bookList) { System.out.println("删除图书"); Scanner scanner = new Scanner(System.in); System.out.print("请输入要删除的书的名字:"); String bookName = scanner.nextLine(); int x = -1, index = 0; for (int i = 0; i < bookList.getUsedSize(); i++) { if (bookName.equals(bookList.getBooks(i).getName())) { x = 1; index = i; System.out.println("删除成功!"); break; } } if (x == -1) { System.out.println("没有该本书"); } for (int i = index; i < bookList.getUsedSize() - 1; i++) { bookList.setBooks(bookList.getBooks(i + 1), i); } bookList.setUsedSize(bookList.getUsedSize() - 1); bookList.setBooks(null, bookList.getUsedSize() - 1); System.out.println("删除成功!"); } }
ExitOperation类:
package Operation; import Book.BookList; public class ExitOperation implements Ioperation { @Override public void work(BookList bookList) { System.out.println("退出系统!"); System.exit(0); } }
FindOperation类:
package Operation; import Book.Book; import Book.BookList; import java.util.Scanner; public class FindOperation implements Ioperation { @Override public void work(BookList bookList) { System.out.println("查找图书"); Scanner scanner = new Scanner(System.in); System.out.print("请输入要查找的书的名字:"); String bookName = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getBooks(i); if (bookName.equals(bookList.getBooks(i).getName())) { System.out.print("找到该本书:"); System.out.println(book); return; } } System.out.println("没有找到本书"); } }
ReturnOperation类:
package Operation; import Book.BookList; import java.util.Scanner; public class ReturnOperation implements Ioperation { @Override public void work(BookList bookList) { System.out.println("归还图书"); Scanner scanner = new Scanner(System.in); System.out.print("请输入要归还的书的名字:"); String bookName = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { if (bookName.equals(bookList.getBooks(i).getName()) && (bookList.getBooks(i).isBorrowed())) { bookList.getBooks(i).setBorrowed(false); System.out.println("归还成功!"); return; } } } }
ShowOperation类:
package Operation; import Book.BookList; public class ShowOperation implements Ioperation { @Override public void work(BookList bookList) { System.out.println("显示图书"); for (int i = 0; i < bookList.getUsedSize(); i++) { System.out.println(bookList.getBooks(i)); } } }
Main函数:
import Book.BookList; import User.User; public class Main { public static void main(String[] args) { BookList bookList = new BookList(); User user = User.login(); BookList bookList1 = new BookList(); while (true) { int choice = user.menu(); user.doWork(choice, bookList); } } }
运行结果:
管理员菜单:
普通用户菜单: