零基础 “入坑” Java--- 十四、【练习】图书小系统
文章目录
- 一、呈现效果
- 二、实现
- 1.书籍
- 2.书架
- 3.身份
- 4.操作
- 4.1 显示图书
- 4.2 删除图书
- 4.3 借阅图书
- 4.4 新增图书
- 4.5 查找图书
- 4.6 归还图书
- 4.7 退出系统
- 三、交互
到目前为止,在本专栏中我们已经学习了Java基础语法的大部分内容,为了更好的理解和掌握学过的知识,我们就编写一个图书小系统,将知识运用起来。
一、呈现效果
对于这样的一个小系统,我们需要输入用户的姓名和身份,根据用户选择的身份不同,呈现不一样的界面,给予用户不同的操作,再根据用户的选择执行对应的操作即可。
管理员操作界面:
普通用户操作界面:
二、实现
1.书籍
图书小系统一定和书相关,所以我们需要对书进行一些操作:
public class Book {//一本书的属性private String name; //书名private String author; //作者private double price; //价格private String type; //类型private boolean isBorrowed; //是否被借出//提供构造方法,方便实例化public Book(String name, String author, double price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;//不实例化isBorrowed原因:boolean默认值为false,未被借出}//书的属性被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 double 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) ? ", 已被借出" : ", 未被借出") +
// ", isBorrowed=" + isBorrowed +'}';}
}
在Book这个类中,我们对书的一些属性(如:书名、作者等)通过private进行定义,为了方便在类外使用这些属性,我们提供get 和 set方法;在管理员的"显示图书"操作中,需要显示书的属性,所以在Book类中重写toString方法。
2.书架
将书籍定义好之后,我们就考虑如何才能存放书。想要存放一组相同类型的数据,就可以使用数组。
public class BookList {//书架存放书的最大数量(数组存放书)private Book[] books = new Book[10];//记录目前实际存放的 书的个数private int used;public int getUsed() {return used;}public void setUsed(int used) {this.used = used;}//获取书的属性public Book getBooks(int pos) {return books[pos];}//设置书的属性public void setBooks(int pos, Book book) {books[pos] = book;}//书架上原有书籍public BookList() {//构造方法,初始化成员this.books[0] = new Book("三国演义", "罗贯中", 25.8, "文学著作");this.books[1] = new Book("西游记", "吴承恩", 32.6, "文学著作");this.books[2] = new Book("水浒传", "施耐庵、罗贯中", 36.4, "文学著作");this.books[3] = new Book("红楼梦", "曹雪芹", 28.6, "文学著作");this.used = 4;}
}
在BookList这个类中,我们定义一个used变量,用于记录当前书架上存在几本书;在构造方法中,我们提供了四本书籍,只要调用构造方法,就会初始化这四本书。
3.身份
在身份选择上,我们提供了两种,一种是管理员,一种是普通用户;根据选择的身份不同,看到的界面也就不同。无论是管理员还是普通用户,都有许多相似的操作,因此我们使用继承思想来实现这两种身份。
public abstract class User {public String name;public IOperation[] iOperations;public User(String name) {this.name = name;}public abstract int menu();public void doOperation(int choice, BookList bookList) {iOperations[choice].work(bookList);}
}
管理员:
public class Admin extends User {public Admin(String name) {super(name);//根据下标存放操作this.iOperations = new IOperation[]{new Exit(),new Find(),new Add(),new Del(),new Show()};}public int menu() {System.out.println("****************************************");System.out.println("您好," + this.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;}
}
普通用户:
public class Normal extends User {public Normal(String name) {super(name);this.iOperations = new IOperation[]{new Exit(),new Find(),new Borrowed(),new Return(),};}public int menu() {System.out.println("****************************************");System.out.println("您好," + this.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;}
}
在调用menu方法时,使用了动态绑定的思想,因为子类中都需要对menu方法进行重写,所以父类中不用实现menu方法,所以将User设置为抽象类。
4.操作
对于不同身份的各种操作,我们定义一个接口,通过实现接口完成类的定义:
public interface IOperation {void work(BookList bookList);
}
4.1 显示图书
使用for循环获取书架上的书即可:
public class Show implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("显示图书");for (int i = 0; i < bookList.getUsed(); i++) {Book book = bookList.getBooks(i);//打印 引用的信息,重写toString方法System.out.println(book);}}
}
4.2 删除图书
public class Del implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("删除图书");int current = bookList.getUsed();if (current == 0) {System.out.println("书架为空!");return;}Scanner scanner = new Scanner(System.in);System.out.println("请输入书名:");String name = scanner.nextLine();//寻找书架上是否有此书int index = -1;for (int i = 0; i < current; i++) {Book book1 = bookList.getBooks(i);if (book1.getName().equals(name)) {index = i;break;}}if (index == -1) {System.out.println("您要删除的书不在书架上!");return;}//current - 1:防止越界for (int i = index; i < current - 1; i++) {Book book = bookList.getBooks(i + 1);bookList.setBooks(i, book);}System.out.println("删除成功");bookList.setUsed(current - 1);}
}
对于删除图书这个操作,我们需要注意:书架上是否还有书,所要删除的书是否为书架上的书,以及删除完书之后将used的值进行更新。
4.3 借阅图书
public class Borrowed implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("借阅图书");Scanner scanner = new Scanner(System.in);System.out.println("请输入书名:");String name = scanner.nextLine();int current = bookList.getUsed();for (int i = 0; i < current; i++) {Book book1 = bookList.getBooks(i);if (book1.getName().equals(name)) {if (!book1.isBorrowed()) {book1.setBorrowed(true);System.out.println("借阅成功");} else {System.out.println("此书已借出");}return;}}System.out.println("查无此书");}
}
在编写借阅图书的代码时,我们需要考虑到书已经被借出的情况,以及需要借阅的书是否为书架上的书。
4.4 新增图书
public class Add implements IOperation {public void work(BookList bookList) {System.out.println("新增图书");Scanner scanner = new Scanner(System.in);System.out.println("请输入书名:");String name = scanner.nextLine();int current = bookList.getUsed();//判断书架是否已存在此书for (int i = 0; i < current; i++) {Book book1 = bookList.getBooks(i);if (book1.getName().equals(name)) {System.out.println("书架已存在此书,请勿重复存放!");return;}}System.out.println("请输入作者:");String author = scanner.nextLine();System.out.println("请输入价格:");double price = scanner.nextDouble();scanner.nextLine();System.out.println("请输入类型:");String type = scanner.nextLine();Book book = new Book(name, author, price, type);bookList.setBooks(current, book);bookList.setUsed(current+1);System.out.println("新增成功");}
}
在新增图书时,我们需要先判断书架上是否存在此书,如果存在,则无需添加此书(以书名为判断标准)。
4.5 查找图书
public class Find implements IOperation {public void work(BookList bookList) {System.out.println("查找图书");Scanner scanner = new Scanner(System.in);System.out.println("请输入您要查找的图书:");String name = scanner.nextLine();for (int i = 0; i < bookList.getUsed(); i++) {Book book = bookList.getBooks(i);if (book.getName().equals(name)) {System.out.println("存在此书:");System.out.println(book);return;}}System.out.println("查无此书");}
}
在查找图书时,我们根据书名进行查找;使用for循环遍历书架上的书,判断书架上书的名字和我们要查找的书名是否相同。
4.6 归还图书
public class Return implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("归还图书");Scanner scanner = new Scanner(System.in);System.out.println("请输入书名:");String name = scanner.nextLine();int current = bookList.getUsed();for (int i = 0; i < current; i++) {Book book1 = bookList.getBooks(i);if (book1.getName().equals(name)) {book1.setBorrowed(false);System.out.println("归还成功");return;}}System.out.println("此书不属于书架,无需归还!");}
}
归还图书时,需判断此书是否为书架上的书,如果不是,则不用归还。依旧使用for循环遍历书架上的书,判断书架上书的名字和我们要归还的书名是否相同,如果相同,则此书归还成功,就将此书设置为未被借出。
4.7 退出系统
public class Exit implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("退出系统");System.exit(0);}
}
三、交互
编写完各个部分的代码之后,将它们联系起来才是重点:
public class Main {//利用向上转型public static User login() {Scanner scanner = new Scanner(System.in);System.out.println("您好,请输入您的姓名:");String name = scanner.nextLine();System.out.println("请输入您的身份:1.图书管理员/0.普通用户");int choice = scanner.nextInt();if (choice == 1) {Admin admin = new Admin(name);return admin;} else {Normal normal = new Normal(name);return normal;}}public static void main(String[] args) {BookList bookList = new BookList(); //书架上原有书籍被初始化好了User user = login();while (true) {//动态绑定int choice = user.menu();//以上两条语句,可以判断:身份和选择的操作//System.out.println(choice); //正常接收返回值//System.out.println(user); //通过动态绑定确定身份/*** 获取到身份和操作之后,怎么建立联系呢?*/user.doOperation(choice, bookList);}}
}
Ending。