编写一个Java 应用程序,实现使用集合存储数据的图书销售管理子系统。
时间: 2023-06-17 13:08:56 浏览: 94
好的,下面是一个简单的Java应用程序,它使用集合存储数据的图书销售管理子系统。具体实现如下:
1. Book类:表示一本书,包含以下属性和方法:
```java
public class Book {
private String title;
private String author;
private String publisher;
private double price;
private int quantity;
public Book(String title, String author, String publisher, double price, int quantity) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
this.quantity = quantity;
}
// getters and setters
// toString method
}
```
2. BookStore类:表示一个图书店,包含以下属性和方法:
```java
import java.util.ArrayList;
import java.util.List;
public class BookStore {
private List<Book> books;
public BookStore() {
books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public List<Book> searchBooksByTitle(String title) {
List<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getTitle().equals(title)) {
result.add(book);
}
}
return result;
}
public List<Book> searchBooksByAuthor(String author) {
List<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
}
public List<Book> searchBooksByPublisher(String publisher) {
List<Book> result = new ArrayList<Book>();
for (Book book : books) {
if (book.getPublisher().equals(publisher)) {
result.add(book);
}
}
return result;
}
public List<Book> getAllBooks() {
return books;
}
}
```
3. BookStoreApp类:表示一个图书销售管理子系统的应用程序,包含以下方法:
```java
import java.util.List;
import java.util.Scanner;
public class BookStoreApp {
private static Scanner scanner = new Scanner(System.in);
private static BookStore bookStore = new BookStore();
public static void main(String[] args) {
boolean isRunning = true;
while (isRunning) {
System.out.println("请选择操作:");
System.out.println("1. 添加书籍");
System.out.println("2. 删除书籍");
System.out.println("3. 按书名查询");
System.out.println("4. 按作者查询");
System.out.println("5. 按出版社查询");
System.out.println("6. 查看全部");
System.out.println("7. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (choice) {
case 1:
addBook();
break;
case 2:
removeBook();
break;
case 3:
searchBooksByTitle();
break;
case 4:
searchBooksByAuthor();
break;
case 5:
searchBooksByPublisher();
break;
case 6:
getAllBooks();
break;
case 7:
isRunning = false;
break;
default:
System.out.println("无效的选择!");
break;
}
}
}
private static void addBook() {
System.out.println("请输入书名:");
String title = scanner.nextLine();
System.out.println("请输入作者名:");
String author = scanner.nextLine();
System.out.println("请输入出版社:");
String publisher = scanner.nextLine();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
System.out.println("请输入数量:");
int quantity = scanner.nextInt();
scanner.nextLine(); // consume the newline character
Book book = new Book(title, author, publisher, price, quantity);
bookStore.addBook(book);
System.out.println("添加成功!");
}
private static void removeBook() {
System.out.println("请输入书名:");
String title = scanner.nextLine();
List<Book> booksToRemove = bookStore.searchBooksByTitle(title);
if (booksToRemove.isEmpty()) {
System.out.println("没有找到该书!");
} else {
System.out.println("找到以下书籍:");
for (int i = 0; i < booksToRemove.size(); i++) {
System.out.println((i+1) + ". " + booksToRemove.get(i).toString());
}
System.out.println("请选择要删除的书籍编号:");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
if (choice < 1 || choice > booksToRemove.size()) {
System.out.println("无效的选择!");
} else {
Book bookToRemove = booksToRemove.get(choice-1);
bookStore.removeBook(bookToRemove);
System.out.println("删除成功!");
}
}
}
private static void searchBooksByTitle() {
System.out.println("请输入书名:");
String title = scanner.nextLine();
List<Book> books = bookStore.searchBooksByTitle(title);
if (books.isEmpty()) {
System.out.println("没有找到该书!");
} else {
System.out.println("找到以下书籍:");
for (Book book : books) {
System.out.println(book.toString());
}
}
}
private static void searchBooksByAuthor() {
System.out.println("请输入作者名:");
String author = scanner.nextLine();
List<Book> books = bookStore.searchBooksByAuthor(author);
if (books.isEmpty()) {
System.out.println("没有找到该作者的书籍!");
} else {
System.out.println("找到以下书籍:");
for (Book book : books) {
System.out.println(book.toString());
}
}
}
private static void searchBooksByPublisher() {
System.out.println("请输入出版社:");
String publisher = scanner.nextLine();
List<Book> books = bookStore.searchBooksByPublisher(publisher);
if (books.isEmpty()) {
System.out.println("没有找到该出版社的书籍!");
} else {
System.out.println("找到以下书籍:");
for (Book book : books) {
System.out.println(book.toString());
}
}
}
private static void getAllBooks() {
List<Book> books = bookStore.getAllBooks();
if (books.isEmpty()) {
System.out.println("暂无书籍!");
} else {
System.out.println("当前库存:");
for (Book book : books) {
System.out.println(book.toString());
}
}
}
}
```
这个程序提供了以下功能:
1. 添加书籍
2. 删除书籍
3. 按书名查询
4. 按作者查询
5. 按出版社查询
6. 查看全部
7. 退出
用户可以选择相应的操作。当用户选择添加书籍时,程序会要求用户输入书籍信息,并将其添加到图书店的库存中。当用户选择删除书籍时,程序会先按书名查询出相应的书籍,然后让用户选择要删除的书籍。当用户选择查询时,程序会按指定的条件查询出相应的书籍并显示出来。当用户选择查看全部时,程序会列出当前库存中的所有书籍。当用户选择退出时,程序会结束运行。
阅读全文