JavaSE图书管理系统
- 思路
- 一.Main方法
- 二.User包
- 1.User类
- 2.NormaUser类
- 3.AdminUser类
- 三.book包
- 1.BookList类
- 2.Book类
- 四.operation包
- 1.IOPeration接口
- 2.AddOperation类
- 新增图书
- 3.BorrowOperation类
- 借阅图书
- 4.DelOperation类
- 删除图书
- 5.FindOperation类
- 查找图书
- 6.ReturnOperation类
- 归还图书
- 7.ShowOperation类
- 打印图书
- 8.ExitOperation类
- 退出系统
思路
实际上大概分为三步,关于书相关的放在book包里,关于用户相关的放在User包里,关于操作相关的我们放在Operation包里,不同的包用来管理不同的代码,那么为什么要把操作放在一个包里呢?原因是我们分为普通用户和管理员用户,那么普通用户和管理员用户的操作是不一样的,我们放在一个类里面,不太好区分。
关于book包,operation包,user包里面的类和接口。
关于book包里,定义两个类,bookList为书架的类,book关于书的类。
关于operation包里的类都是对bookList的操作,如查找,删除等。
关于User的类,大致为管理员类和普通用户类,和普通的User类。
一.Main方法
import book.BookList;
import user.AdminUser;
import user.NormaUser;
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 NormaUser(name);}}public static void main(String[] args) {BookList booklist = new BookList();//书架创建//user 指向哪个对象 就看返回值是哪个User user = login();//登录while(true) {//重复执行操作int choice = user.menu();//看user指向哪个对象,就调用哪个菜单System.out.println("choice: " + choice);//根据choice 的选择,来决定调用的是哪个方法?user.doOperation(choice, booklist);}}
}
二.User包
1.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){ioPerations[choice].work(bookList);//拿到对象,调用对象的方法}
}
2.NormaUser类
package user;import operation.*;import java.util.Scanner;public class NormaUser extends User {//普通用户public NormaUser(String name) {super(name);this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation(),};//初始化数组}public int menu(){ //菜单System.out.println("***********普通用户*********");System.out.println("1. 查找图书");System.out.println("2. 借阅图书");System.out.println("3. 归还图书");System.out.println("0. 退出系统");System.out.println("****************************");Scanner sc = new Scanner(System.in);System.out.println("请输入你的操作: ");int choice = sc.nextInt();return choice;}
}
3.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(),};}public int menu(){ //菜单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 sc = new Scanner(System.in);System.out.println("请输入你的操作: ");int choice = sc.nextInt();return choice;}
}
三.book包
1.BookList类
package book;public class BookList {private Book[] books;private int usedSize;//记录当前书架上 实际存放的书的数量public BookList(){this.books = new Book[10];this.books[0] = new Book("三国演义","罗贯中",10,"小说");this.books[1] = new Book("西游记","吴承恩",9,"小说");this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");this.usedSize = 3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book getBook(int pos){return books[pos];}public void setBooks(int pos,Book book){books[pos] = book;}public Book[] getBooks(){return books;}}
2.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 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;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((isBorrowed==true) ? ",已经借出" : ",未被借出")+'}';}
}
四.operation包
因为所以操作都是对于书架的,所以我们设置一个接口
1.IOPeration接口
package operation;import book.BookList;public interface IOPeration {void work(BookList bookList);
}
2.AddOperation类
新增图书
package operation;import book.Book;
import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;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("请输入类型: ");String type = scanner.nextLine();System.out.println("请输入价格: ");int price = scanner.nextInt();Book book = new Book(name,author,price,type);//检查数组当中有没有这本书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;}}//判断数组是否满了if(currentSize==bookList.getBooks().length){System.out.println("书架满了!");}else{bookList.setBooks(currentSize,book);bookList.setUsedSize(currentSize+1);}}
}
3.BorrowOperation类
借阅图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class BorrowOperation implements IOPeration {@Overridepublic 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);if(book.getName().equals(name)){book.setBorrowed(true);System.out.println("借阅成功!");System.out.println(book);return;}}System.out.println("你借阅的图书不存在!!!");}
}
4.DelOperation类
删除图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class DelOperation implements IOPeration {@Overridepublic void work(BookList bookList) {System.out.println("删除图书!");Scanner scanner = new Scanner(System.in);System.out.println("请输入你要删除的图书:");String name = scanner.nextLine();int pos = -1;int currenSize = bookList.getUsedSize();int i = 0;for(;i<currenSize;i++){Book book = bookList.getBook(i);if(book.getName().equals(name)){pos = i;break;}}if(i==currenSize){System.out.println("没有你要删除的图书!");return;}//开始删除int j = pos;for(;j<currenSize-1;j++){Book book = bookList.getBook(j+1);bookList.setBooks(j,book);}bookList.setBooks(j,null);bookList.setUsedSize(currenSize-1);}}
5.FindOperation类
查找图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class FindOperation implements IOPeration {@Overridepublic 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)){System.out.println("找到了这本书,信息如下:");System.out.println(book);return;}}System.out.println("没有找到这本书!");}
}
6.ReturnOperation类
归还图书
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class ReturnOperation implements IOPeration {@Overridepublic 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);if(book.getName().equals(name)){book.setBorrowed(false);System.out.println("归还成功!");System.out.println(book);return;}}System.out.println("你归还的图书不存在!!!");}
}
7.ShowOperation类
打印图书
package operation;import book.Book;
import book.BookList;public class ShowOperation implements IOPeration {@Overridepublic 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);}}
}
8.ExitOperation类
退出系统
package operation;import book.BookList;public class ExitOperation implements IOPeration {@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);}
}