要求设计三个类,Book类、BookSysMain类、BookManagement类,输出如图
望采纳!
Book类
package com.book.model;
public class Book {
//书名
private String title;
//作者
private String authors;
//出版社
private String publisher;
//出版日期
private String publishDate;
//ISBN
private String isbn;
//价格
private Double price;
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthors() {
return authors;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", authors='" + authors + '\'' +
", publisher='" + publisher + '\'' +
", publishDate='" + publishDate + '\'' +
", isbn='" + isbn + '\'' +
", price=" + price +
'}';
}
}
BookManagement类
package com.book.model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManagement {
public static final List<Book> BOOK_LIST = new ArrayList<Book>();
/**
* 创建一本数据
*/
public void newBook(Scanner scan) {
Book newBook = new Book();
//输入 ISBN
System.out.println("Input ISBN:");
newBook.setIsbn(scan.nextLine());
//输入 标题
System.out.println("Input Title:");
newBook.setTitle(scan.nextLine());
//输入 作者
System.out.println("Input Authors:");
newBook.setAuthors(scan.nextLine());
//输入 发布日期
System.out.println("Input Pub Date:");
newBook.setPublishDate(scan.nextLine());
//输入 价格
System.out.println("Input Price:");
newBook.setPrice(scan.nextDouble());
BOOK_LIST.add(newBook);
System.out.println("The new book is added:");
System.out.println(newBook.toString());
}
/**
* 删除
*/
public void deleteBook(Scanner scan) {
System.out.println("请输入书名>>");
String title = scan.nextLine();
int tag = 0;
for (int i = 0, len = BOOK_LIST.size(); i <= len; i++) {
if (BOOK_LIST.get(i).getTitle().equalsIgnoreCase(title)) {
tag = i;
}
}
BOOK_LIST.remove(tag);
}
public void init() {
System.out.println("读取存量图书中");
readBookTxt("/admin/home/project/qa-demo/mini-book-manage/src/main/java/com/book/model/bookList.txt");
System.out.println("读取存量图书完成");
String desc = "-------------------------------------\n" +
"| Welcome to Book Management System |\n" +
"-------------------------------------\n" +
"| 1 New Book(N) |\n" +
"| 2 Delete Book(D) |\n" +
"| 3 Query Book(Q) |\n" +
"| 4 List All(A) |\n" +
"| 5 Save(S) |\n" +
"| 6 Exit(E) |\n" +
"-------------------------------------";
System.out.println(desc);
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Input Command>>");
String command = scan.nextLine();
if ("N".equalsIgnoreCase(command)) {
newBook(scan);
} else if ("D".equalsIgnoreCase(command)) {
deleteBook(scan);
} else if ("A".equalsIgnoreCase(command)) {
System.out.println(BOOK_LIST);
} else if ("E".equalsIgnoreCase(command)) {
System.out.println("感谢使用!");
break;
} else {
System.out.println("请输入正确的指令");
}
}
}
private void readBookTxt(String fileName) {
try {
File myFile = new File(fileName);
//判断文件是否存在
if (myFile.isFile() && myFile.exists()) {
InputStreamReader Reader = new InputStreamReader(new FileInputStream(myFile), "UTF-8");
//考虑到编码格式,new FileInputStream(myFile)文件字节输入流,以字节为单位对文件中的数据进行读取
//new InputStreamReader(FileInputStream a, "编码类型")
//将文件字节输入流转换为文件字符输入流并给定编码格式
BufferedReader bufferedReader = new BufferedReader(Reader);
//BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
//通过BuffereReader包装实现高效读取
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
//buffereReader.readLine()按行读取写成字符串
System.out.println(lineTxt);
String[] split = lineTxt.split(",");
Book book = new Book();
book.setTitle(split[0]);
book.setAuthors(split[1]);
book.setPublisher(split[2]);
book.setPublishDate(split[3]);
book.setIsbn(split[4]);
book.setPrice(Double.valueOf(split[5]));
BOOK_LIST.add(book);
}
Reader.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
BookSysMain类
```java
package com.book.model;
public class BookSysMain {
public static void main(String[] args) {
//创建管理器
BookManagement management = new BookManagement();
//初始化管理器
management.init();
}
}
bookList.txt
明朝那些事儿,当前明月,新华出版社,2022-06-02,dns-23ms-23,50.00
三体,刘慈欣,人民出版社,2022-06-01,2932-219k-nss,35.55
public class Book implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private Person author;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getAuthor() {
return author;
}
public void setAuthor(Person author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Book() {
}
public Book(String name,Person author,int price) {
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "name: " + name + "\nauthor: " + author + "\nprice: " + price ;
}
}
Console系统嗦