赞
踩
目录
0.用户登录
1.退出系统:关闭JVM
2.查找图书:通过图书的名称查找图书,若有,展示查到的图书信息;若没有,返回未查到
3.新增图书:新增图书名称、作者、价格、类型
4.删除图书:通过图书名称删除图书
5.展示图书:将所有图书的数据输出
0.用户登录
1.退出系统:关闭JVM
2.展示图书:将所有图书的数据输出
3.借阅图书:通过图书名称借出图书,并将图书状态改为已借出
4.归还图书:通过图书名称归还图书,并将图书状态改为未借出
方法:面向对象中的封装、继承、多态、接口、抽象,重写
工具:IDEA
Book类是保存图书信息的实体类
- public class Book {
- private String name;
- private String author;
- private double price;
- private String type;
- private boolean isBorrow;
-
- public Book() {
- }
-
- public Book(String name, String author, double 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 double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public boolean isBorrow() {
- return isBorrow;
- }
-
- public void setBorrow(boolean borrow) {
- isBorrow = borrow;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Book book = (Book) o;
- return Double.compare(book.price, price) == 0 &&
- isBorrow == book.isBorrow &&
- Objects.equals(name, book.name) &&
- Objects.equals(author, book.author) &&
- Objects.equals(type, book.type);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(name, author, price, type, isBorrow);
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- ", isBorrow=" +
- (isBorrow == true ? "已被借出" : "未被借出") +
- '}';
- }
- }

BookList是一个书架类,里面有两个属性,分别是书架的长度和书。
通过构造方法初始化三本书
注意:Book类型的属性的get和set方法需要自己按照需求指定
- public class BookList {
- private static final int DEFAULT_SIZE = 10;
- private Book[] book = new Book[DEFAULT_SIZE];
-
- private int bookSize; //记录当前book数组中有多少本书
-
- public BookList() {
- book[0] = new Book("红楼梦","曹雪芹", 20.3, "四大名著");
- book[1] = new Book("西游记","吴承恩",45.8,"四大名著");
- book[2] = new Book("水浒传","许恬芯",113.3,"四大名著");
- this.bookSize = 3;
- }
-
- public Book getBook(int pos) {
- return book[pos];
- }
-
- public void setBook(Book book) {
- this.book[bookSize] = book;
- }
- public void setBook(int index,Book book) {
- this.book[index] = book;
- }
-
- public int getBookSize() {
- return bookSize;
- }
-
- public void setBookSize(int bookSize) {
- this.bookSize = bookSize;
- }
- }

User类是一个父类,被AdminUser和NomalUser继承,同时也是一个抽象类
- public abstract class User {
- protected String name;
- protected IOperation[] iOperations;
-
- public User(String name) {
- this.name = name;
- }
-
- public abstract int menu();
-
- public void doWork(int choice, BookList booklist) {
- this.iOperations[choice].work(booklist);
- }
- }
AdminUser类
- public class AdmainUser extends User{
- public AdmainUser(String name) {
- super(name);
- //父类和子类有同名属性或方法,要访问父类的时,super不能省略.所以这里可以用this
- this.iOperations = new IOperation[]{ //初始化,给他分配空间
- new ExitOperation(),
- new FindOperation(),
- new AddOperation(),
- new DeleteOperation(),
- 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("************************");
- System.out.println("请输入你的操作:");
- Scanner sc = new Scanner(System.in);
- int choice = sc.nextInt();
- return choice;
- }
- }

NomalUser类
- public class NomalUser extends User{
- public NomalUser (String name) {
- super(name);
- this.iOperations = new IOperation[] {
- new ExitOperation(),
- new ShowOperation(),
- new BrrowOperation(),
- 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("************************");
-
- System.out.println("请输入你的操作:");
- Scanner sc = new Scanner(System.in);
- int choice = sc.nextInt();
- return choice;
- }
- }

IOperation接口
- public interface IOperation {
- void work(BookList bookList);
- }
AddOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description 新增图书
- */
- public class AddOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入要新增的图书名称:");
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入书名:");
- String name = sc.nextLine();
- System.out.println("请输入作者:");
- String author = sc.nextLine();
- System.out.println("请输入类型:");
- String type = sc.nextLine();
- System.out.println("请输入价格:");
- double price = sc.nextDouble();
-
- Book book = new Book(name,author,price,type);
- int currentBookSize = bookList.getBookSize();
- for (int i = 0; i < currentBookSize; i++) {
- Book books = bookList.getBook(i);
- if (name.equals(books.getName())) {
- System.out.println("已经存在这本书了,不可以重复添加!");
- return;
- }
- }
- bookList.setBook(book);
- bookList.setBookSize(currentBookSize + 1);
- System.out.println("新增成功,增添了:" + book);
- }
- }

BorrowOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description
- */
- public class BrrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入你要借阅的图书的名称:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int currentBookSize = bookList.getBookSize();
- for (int i = 0; i < currentBookSize; i++) {
- Book book = bookList.getBook(i);
- if (name.equals(book.getName()) && !book.isBorrow()) { //找到了这本书
- //修改书的状态
- book.setBorrow(true);
- System.out.println("借阅成功");
- return;
- }
- }
- System.out.println("没有你要找的书!");
- }
- }

DeleteOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description 删除图书
- */
- public class DeleteOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入你想删除的图书名称:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int currentBookSize = bookList.getBookSize();
- //判断有没有这个图书
- int index = -1;
- for (int i = 0; i < currentBookSize; i++) {
- Book book = bookList.getBook(i);
- if (name.equals(book.getName())) {
- index = i; //得到要删除的这个图书的下标
- break;
- }
- }
- //走到这说明我已经找到了这本书
- //现在要做的就是删除这本书,后面的覆盖前面的
- for (int j = 0; j < currentBookSize - 1; j++) {
- Book book = bookList.getBook(j + 1); //得到被删除对象的下一个对象
- bookList.setBook(j,book); //利用set重新赋值,把book给j位置
- }
- System.out.println("删除成功!");
- bookList.setBookSize(currentBookSize - 1);
- //将最后位置的对象置为NULL
- bookList.setBook(currentBookSize - 1,null);
- }
- }

ExitOperation
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出系统");
- System.exit(0);
- }
- }
FindOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description 查找图书
- */
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入要查找的图书名称:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int currentBookSize = bookList.getBookSize();
- for (int i = 0; i < currentBookSize; i++) {
- Book book = bookList.getBook(i);
- if (name.equals(book.getName())) {
- System.out.println("找到了这本书:" + book);
- return;
- }
- }
- System.out.println("很遗憾,没有这本书。如有需要,请联系管理员添加");
- }
- }

ReturnOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description 查找图书
- */
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("请输入要查找的图书名称:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- int currentBookSize = bookList.getBookSize();
- for (int i = 0; i < currentBookSize; i++) {
- Book book = bookList.getBook(i);
- if (name.equals(book.getName())) {
- System.out.println("找到了这本书:" + book);
- return;
- }
- }
- System.out.println("很遗憾,没有这本书。如有需要,请联系管理员添加");
- }
- }

ShowOperation
- package com.xtx.oper;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description 展示所有图书
- */
- public class ShowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
-
- System.out.println("打印所有图书:");
- int currentBookSize = bookList.getBookSize(); //获取当前图书的个数
- System.out.println(currentBookSize);
- for (int i = 0; i < currentBookSize ; i++) {
- Book book = bookList.getBook(i);
- System.out.println(book);
- }
- }
-
- }

- package com.xtx;
-
- import com.xtx.book.Book;
- import com.xtx.book.BookList;
- import com.xtx.user.AdmainUser;
- import com.xtx.user.NomalUser;
- import com.xtx.user.User;
-
- import java.util.Scanner;
-
- /**
- * @author 小许喵喵
- * @date 2023/6/14
- * @Description
- */
- public class Main {
- public static User login() {
- System.out.println("请输入你的姓名:");
- Scanner sc = new Scanner(System.in);
- String name = sc.nextLine();
- System.out.println("请输入你的身份: 1-》管理员 2-》普通用户");
- int choice = sc.nextInt();
- if (choice == 1) {
- return new AdmainUser(name);
- } else {
- return new NomalUser(name);
- }
- }
- public static void main(String[] args) {
- User user = login();
- BookList bookList = new BookList();
- while(true) {
- int choice = user.menu();
- user.doWork(choice,bookList);
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。