1. 图书管理系统菜单
如上图给用户选项
1. 管理员
2. 普通用户
2. 实现基本框架
右键点src,选择new,选择Package命名三个包 book operation user
1.先选择book包,new两个类 book bookList
在book类中定义书的基本属性,并重写toString方法,构造方法 ,以及实现成员变量的Getter and Setter方法
在bookList类中定义一个一个数组储存图书Book[],以及一个usedSize记录图书的数量,重写构造方法,并实现成员变量的Getter and Setter方法
2. 选择operation包,创建一个接口并创建work方法(抽象方法),规范包中的类
package operation;import book.BookList;public interface IOperation {void work(); }
命名以上的类,并重写work方法
3. 选择user包,创建User类,AdminUser类,NormalUser类,让前者作为父类
在User类中定义name接受用户名,并创建抽象方法menu模拟进入程序
public abstract class User {protected String name;public User(String name) {this.name = name;}public abstract int menu(); }
程序的入口
package book; import user.*; 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("请输入你的身份: ");System.out.println("1. 管理员 0. 普通用户");int choice = scanner.nextInt();if(choice == 1){AdminUser adminUser = new AdminUser(name);return adminUser;}else{NormalUser normalUser = new NormalUser(name);return normalUser;}}public static void main(String[] args) {BookList bookList = new BookList();User user = login();while (true){int choice = user.menu();user.doOperation(choice,bookList);}} }
3. 实现业务
3.1 借阅图书
package operation;import book.*;import java.util.Scanner;public class BorrowedOperation 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 book1 = booklist.getBook(i);if(book1.getName().equals(name)) {if(!book1.isBorrowed()){book1.setBorrowed(true);System.out.println("借阅成功");}else{System.out.println("这本图书已经被借走了:");}return;}}System.out.println("书架上内有你要借阅的图书:");} }
3.2 查找图书
package operation;import book.*;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();int currentSize = booklist.getUsedSize();for (int i = 0; i < booklist.getUsedSize(); i++) {Book book = booklist.getBook(i);if(book.getName().equals(name)){System.out.println("找到了你要找的书: ");System.out.println(book);return;}}System.out.println("书架上没有这本书!");} }
3.3 归还图书
package operation;import book.*;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 book1 = booklist.getBook(i);if(book1.getName().equals(name)){if(!book1.isBorrowed()){System.out.println("这本图书没有被借走");}else{book1.setBorrowed(false);System.out.println("归还成功");}return;}}System.out.println("没有你要归还的图书");} }
3.4 新增图书
package operation;import book.*;import java.util.Scanner;public class AddOperation implements IOperation{@Overridepublic 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("请输入价格;");int price = scanner.nextInt();scanner.nextLine();System.out.println("请输入类型;");String style = scanner.nextLine();Book book = new Book(name,author,price,style);int currentSize = booklist.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book1 = booklist.getBook(i);if(book1.getName().equals(name)){System.out.println("书架上已经有这本书了");return;}}booklist.setBook(currentSize,book);booklist.setUsedSize(currentSize+1);} }
3.5 删除图书
package operation;import book.*;import java.util.Scanner;public class DelOperation 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();if(currentSize == 0){System.out.println("书架为空:");return;}int index = -1;for (int i = 0; i < currentSize; i++) {Book book1 = booklist.getBook(i);if(book1.getName().equals(name)) {index = i;break;}}if(index == -1){System.out.println("书架上没有这本书:");return;}for (int i = index; i < currentSize; i++) {Book book1 = booklist.getBook(i+1);booklist.setBook(i,book1);}booklist.setUsedSize(currentSize-1);} }
3.6 显示图书
package operation;import book.*;public class ShowOperation implements IOperation{public void work(BookList booklist) {System.out.println("展示图书");int currentSize = booklist.getUsedSize();for (int i = 0; i < booklist.getUsedSize(); i++) {Book book = booklist.getBook(i);System.out.println(book);}} }
3.7 退出系统
package operation;import book.Book; import book.BookList;public class ExitOperation implements IOperation{public void work(BookList booklist) {System.out.println("退出系统");for (int i = 0; i < booklist.getUsedSize(); i++) {booklist.setBook(i,null);}booklist.setUsedSize(0);System.exit(0);} }