编写一个程序,用于实现某书店图书的管理,具体功能如下 1) 定义一个Book类,用于描述图书的静态动态特征; 2) 定义一个集合,用于存储书店中所有的图书对象; 3) 提供全部图书信息的显示功能; 4) 提供根据书名的图书查找功能; 5) 提供买书功能,即买书后,集合中相应删除该图书对象; 6) 提供新进书功能,即书店购置了新的图书,相应的集合中应增加图书对象。 要求:系统有清晰的提示信息便于用户使用;应定义书店类,并恰当设计成员变量与成员方法。用Arraylist方法
时间: 2024-03-17 13:46:20 浏览: 80
以下是一个基本的实现示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public String toString() {
return "Title: " + title + " | Author: " + author + " | Price: " + price;
}
}
class BookStore {
private ArrayList<Book> books = new ArrayList<Book>();
private Scanner scanner = new Scanner(System.in);
public void addBook(Book book) {
books.add(book);
System.out.println("The book has been added to the store.");
}
public void removeBook(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
books.remove(book);
System.out.println("The book has been removed from the store.");
return;
}
}
System.out.println("The book was not found in the store.");
}
public void displayAllBooks() {
System.out.println("All Books in the Store:");
for (Book book : books) {
System.out.println(book);
}
}
public void searchBook(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
System.out.println("The book was found in the store:");
System.out.println(book);
return;
}
}
System.out.println("The book was not found in the store.");
}
public void buyBook(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
books.remove(book);
System.out.println("You have bought the book.");
return;
}
}
System.out.println("The book was not found in the store.");
}
public void addNewBook() {
System.out.print("Enter the title of the book: ");
String title = scanner.nextLine();
System.out.print("Enter the author of the book: ");
String author = scanner.nextLine();
System.out.print("Enter the price of the book: ");
double price = scanner.nextDouble();
scanner.nextLine();
Book book = new Book(title, author, price);
addBook(book);
}
}
public class BookStoreManagement {
public static void main(String[] args) {
BookStore store = new BookStore();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nBook Store Management System");
System.out.println("1. Display All Books");
System.out.println("2. Search Book");
System.out.println("3. Buy Book");
System.out.println("4. Add New Book");
System.out.println("5. Remove Book");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
store.displayAllBooks();
break;
case 2:
System.out.print("Enter the title of the book: ");
String searchTitle = scanner.nextLine();
store.searchBook(searchTitle);
break;
case 3:
System.out.print("Enter the title of the book to buy: ");
String buyTitle = scanner.nextLine();
store.buyBook(buyTitle);
break;
case 4:
store.addNewBook();
break;
case 5:
System.out.print("Enter the title of the book to remove: ");
String removeTitle = scanner.nextLine();
store.removeBook(removeTitle);
break;
case 6:
System.out.println("Thank you for using the Book Store Management System!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 6);
}
}
```
这个程序定义了一个 Book 类来描述图书的属性,BookStore 类用 ArrayList 存储所有的图书对象,并实现了显示全部图书信息、根据书名查找图书、买书、添加新进书和删除书的功能。主类 BookStoreManagement 提供了一个简单的文本界面,用户可以根据菜单选择相应的操作。
阅读全文