编写一个书店应用程序。实现书籍入库,查看书籍列表,根据作者或者书名查找书籍,卖出书籍,书籍下架功能。书籍信息包含:编码,名称,库存,简介,出版社,价格,作者。注:同一本书编号相同。基本要求] 使用List和String。 [测试数据] 自定义
4条回答 默认 最新
- 刘逸晖 2021-05-21 10:53关注
package com.sxt.pojo; import java.util.Objects; /** * 书籍信息 * * @author 刘逸晖 */ public class Book { /** * id */ private int id = 0; /** * 编码 */ private String coding = ""; /** * 名称 */ private String name = ""; /** * 库存 */ private int inventory = 0; /** * 简介 */ private String describe = ""; /** * 出版社 */ private String press = ""; /** * 价格 */ private int price = 0; /** * 作者 */ private String author = ""; /** * 0为下架,1为上架 */ private int status = 0; public Book() { } public Book(int id, String coding, String name, int inventory, String describe, String press, int price, String author, int status) { this.id = id; this.coding = coding; this.name = name; this.inventory = inventory; this.describe = describe; this.press = press; this.price = price; this.author = author; this.status = status; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return this.id == book.getId(); } @Override public int hashCode() { return Objects.hash(name); } @Override public String toString() { return "Book{" + "id=" + id + ", coding='" + coding + '\'' + ", name='" + name + '\'' + ", inventory=" + inventory + ", describe='" + describe + '\'' + ", press='" + press + '\'' + ", price=" + price + ", author='" + author + '\'' + ", status=" + status + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCoding() { return coding; } public void setCoding(String coding) { this.coding = coding; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getInventory() { return inventory; } public void setInventory(int inventory) { this.inventory = inventory; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
package com.sxt.pojo; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * 书店 * * @author 刘逸晖 */ public class BookStore { /** * 书籍列表 */ private List<Book> bookList = new ArrayList<>(); public BookStore() { } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; BookStore bookStore = (BookStore) o; return Objects.equals(bookList, bookStore.bookList); } @Override public int hashCode() { return Objects.hash(bookList); } @Override public String toString() { return "BookStore{" + "bookList=" + bookList + '}'; } public List<Book> getBookList() { return bookList; } public void setBookList(List<Book> bookList) { this.bookList = bookList; } }
package com.sxt.application; import com.sxt.pojo.Book; import com.sxt.pojo.BookStore; import java.util.ArrayList; import java.util.List; /** * 处理书籍相关业务 * * @author 刘逸晖 */ public class BookApplication { /** * 书店 */ public static BookStore bookStore = new BookStore(); public static void main(String[] args) { List<Book> bookList = new ArrayList<>(); Book book1 = new Book(1001, "utf-8", "书籍1", 100, "第1种书", "出版社1", 999, "作者1", 1); Book book2 = new Book(1002, "utf-8", "书籍2", 100, "第2种书", "出版社2", 999, "作者2", 1); Book book3 = new Book(1003, "utf-8", "书籍3", 100, "第3种书", "出版社3", 999, "作者3", 1); Book book4 = new Book(1004, "utf-8", "书籍4", 100, "第4种书", "出版社4", 999, "作者4", 1); Book book5 = new Book(1005, "utf-8", "书籍5", 100, "第5种书", "出版社5", 999, "作者5", 1); bookList.add(book1); bookList.add(book2); bookList.add(book3); bookList.add(book4); bookList.add(book5); bookStore.setBookList(bookList); Book book6 = new Book(1006, "utf-8", "书籍6", 100, "第种书", "出版社6", 999, "作者6", 1); System.out.println("向书店里新增第6种书:"); System.out.println(insertBook(book6)); System.out.println("向书店里新增第6种书:"); System.out.println(insertBook(book6)); System.out.println("查询所有书籍:"); System.out.println(selectBookAll()); System.out.println("根据书名查询店内的书籍:"); System.out.println(selectBookByName("书籍1")); System.out.println("根据作者查询店内的书籍:"); System.out.println(selectBookByAuthor("作者1")); System.out.println("购买第一种书籍:"); System.out.println(buyBook(1001, 1)); System.out.println("下架第一种书"); System.out.println(updateBookStatus(1001, 0)); System.out.println("购买第一种书籍:"); System.out.println(buyBook(1001, 1)); System.out.println("根据id查询店内的书籍:"); System.out.println(selectBookById(1001)); } /** * 像书店内新增一种书籍 * * @param book 预入库的书籍 */ public static boolean insertBook(Book book) { if (book == null || bookStore.getBookList().contains(book)) { return false; } else { bookStore.getBookList().add(book); return true; } } /** * 查询书店内的所有书籍 * * @return */ public static List<Book> selectBookAll() { return bookStore.getBookList(); } /** * 根据书名查询店内的书籍 * * @param bookName 书籍名称 * @return */ public static List<Book> selectBookByName(String bookName) { if (bookName == null || bookName.equals("")) { return null; } List<Book> resultList = new ArrayList<>(); for (Book book : bookStore.getBookList()) { if (bookName.equals(book.getName())) { resultList.add(book); } } return resultList; } /** * 根据作者查询书店内的书籍 * * @param bookAuthor 书籍作者 * @return */ public static List<Book> selectBookByAuthor(String bookAuthor) { if (bookAuthor == null || bookAuthor.equals("")) { return null; } List<Book> resultBook = new ArrayList<>(); for (Book book : bookStore.getBookList()) { if (bookAuthor.equals(book.getAuthor())) { resultBook.add(book); } } return resultBook; } /** * 根据id查询书店内的书籍 * * @param bookId 书籍id * @return */ public static Book selectBookById(int bookId) { for (Book book : bookStore.getBookList()) { if (bookId == book.getId()) { return book; } } return null; } /** * 购买店内的书籍 * * @param bookId 书籍的id * @param number 购买数量 * @return */ public static boolean buyBook(int bookId, int number) { for (Book book : bookStore.getBookList()) { if (bookId == book.getId()) { if (book.getStatus() == 1 && book.getInventory() - number >= 0) { book.setInventory(book.getInventory() - number); return true; } else { return false; } } } return false; } /** * 修改书籍的状态 * * @param bookId 书籍的id * @param status 书籍的状态,0为下架,1为上架 * @return */ public static boolean updateBookStatus(int bookId, int status) { for (Book book : bookStore.getBookList()) { if (bookId == book.getId()) { book.setStatus(status); return true; } } return false; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 关于#hadoop#的问题
- ¥15 (标签-Python|关键词-socket)
- ¥15 keil里为什么main.c定义的函数在it.c调用不了
- ¥50 切换TabTip键盘的输入法
- ¥15 可否在不同线程中调用封装数据库操作的类
- ¥15 微带串馈天线阵列每个阵元宽度计算
- ¥15 keil的map文件中Image component sizes各项意思
- ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
- ¥15 划分vlan后,链路不通了?
- ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据