目录
具体实现类是根据用户的需求所添加的
- package com.book;
-
-
- public class Book {
- private String name;//书名
- private String author;//书的作者
- private int price;//书的价格
- private String type;//书的类型
- private boolean isBorrowed;//书是否已借出
- //boolean类型默认是false,默认未被借出
- //构造方法
-
- public Book(String name, String author, int price, String type) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.type = type;
- }
- //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)?" 已借出":" 未借出 ")+
- '}';
- }
- }
- package com.book;
-
-
- public class BookList {
- public Book[] books=new Book[10];
- public int usedSize;//计数器,记录当前书的本数
-
-
-
- //先存几本书
- public BookList() {
- books[0]=new Book("狼王梦","沈石溪",35,"小说");
- books[1]=new Book("活着","余华",40,"小说");
- books[2]=new Book("三体","刘慈欣",38,"小说");
- this.usedSize=3;
-
- }
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
- //获取指定位置
- public Book getPos(int pos) {
- return books[pos];
- }
-
- //存放在指定位置,指定位置为,当前可以存放书籍的书架上最后一个位置
- public void setBooks(Book book,int pos) {
- books[pos]=book;
- }
- }
- package com.user;
-
- import com.book.BookList;
- import com.operations.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);
- }
- }
- package com.user;
-
- import com.operations.*;
-
- 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("hello " + name + " 欢迎进入图书系统");
- 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);
- int choice = scanner.nextInt();
- return choice;
- }
- }
- package com.user;
-
- import com.operations.*;
-
- import java.util.Scanner;
-
-
- public class NormalUser extends User {
-
- public NormalUser(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("hello "+name+" 欢迎进入图书系统");
- 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);
- int choice = scanner.nextInt();
- return choice;
- }
- }
- package com.operations;
-
- import com.book.BookList;
-
-
- public interface IOperation {
- void work(BookList bookList);
- }
- package com.operations;
-
- import com.book.Book;
- import com.book.BookList;
-
- import java.util.Scanner;
-
-
- //添加图书
- public class AddOperation implements IOperation{
- public void work(BookList bookList) {
- System.out.println("请输入要新增的书名:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- System.out.println("请输入图书作者:");
- String author=scanner.nextLine();
- System.out.println("图书价格:");
- int price=scanner.nextInt();
- scanner.nextLine();//吸收换行符
- System.out.println("图书类型:");
- String type=scanner.nextLine();
-
-
- Book book=new Book(name,author,price,type);
- //获取当前可以存放书的位置
- int currentSize= bookList.getUsedSize();
- //将书放入指定位置
- bookList.setBooks(book,currentSize);
- //属的有效个数加一
- bookList.setUsedSize(currentSize+1);
-
- }
- }
- package com.operations;
-
- import com.book.Book;
- import com.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);
- String name=scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())) {
- if(book.isBorrowed()) {
- System.out.println("已被借出!");
- } else {
- book.setBorrowed(true);
- System.out.println("借阅成功");
- }
- return ;
- }
- }
- System.out.println("查无此书");
- }
- }
- package com.operations;
-
- import com.book.Book;
- import com.book.BookList;
- import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
-
- import java.util.Scanner;
-
-
- //删除图书
- public class DelOperation implements IOperation {
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入你要删除的书名:");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- //遍历数组,查找是否有要删除的书,如果有,则记录下标
- int index=-1;
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())) {
- index=i;
- break;
- }
- }
- //
- if(index==-1) {
- System.out.println("查无此书");
- return ;
- }
- //删除
- for (int i = index; i < currentSize-1; i++) {
- //后面使用的是i+1,所以这里是Size-1
- Book book=bookList.getPos(i+1);
- bookList.setBooks(book,i);
- }
- //删除后,最后一个需要置空,否则一直有人引用,内存不会释放
- bookList.setBooks(null,currentSize-1);
- bookList.setUsedSize(currentSize-1);
- System.out.println("删除成功!");
- }
- }
- package com.operations;
-
- import com.book.BookList;
-
-
- //退出
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.exit(0);
- }
- }
-
- package com.operations;
-
- import com.book.Book;
- import com.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);
- String name=scanner.nextLine();
- int currentSize= bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book=bookList.getPos(i);
- if(name.equals(book.getName())) {
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有这本书");
- }
- }
- package com.operations;
-
- import com.book.Book;
- import com.book.BookList;
-
- import java.util.Scanner;
-
-
- public class ReturnOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- System.out.println("归还图书!");
- 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.getPos(i);
- if(name.equals(book.getName())) {
- book.setBorrowed(false);
- System.out.println("归还图书成功!");
- return;
- }
- }
- System.out.println("没有你要归还的图书!");
- }
- }
- package com.operations;
-
- import com.book.Book;
- import com.book.BookList;
-
-
- //显示图书
- public class ShowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- int currentSize= bookList.getUsedSize();
- for(int i=0;i
- Book book=bookList.getPos(i);
- System.out.println(book);
- }
- }
- }
Main
- package com;
-
- import com.book.BookList;
- import com.user.AdminUser;
- import com.user.NormalUser;
- import com.user.User;
-
- import java.util.Scanner;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User:龙宝
- * Date:2022-08-10
- * Time:15:20
- */
- public class Main {
- public static void main(String[] args) {
- //准备数据
- BookList bookList=new BookList();
- //登录
- User user=login();//user引用的对象与你的选择有关
- while(true) {
- int choice=user.menu();//根据引用的对象不同,调用相应的菜单,多态绑定
- user.doOperation(choice,bookList);
- }
- }
- public static User login() {
- System.out.println("请输入姓名:");
- Scanner scanner=new Scanner(System.in);
- String userName=scanner.nextLine();
- System.out.println("请输入身份:1、管理员 2、普通用户");
- int choice= scanner.nextInt();
- if(choice==1) {
- return new AdminUser(userName);
- } else {
- return new NormalUser(userName);
- }
- }
- }
运行结果的部分展示:
本期结束啦,下期见!!!