编写一个Java 应用程序,实现使用集合存储数据的图书销售管理子系统。
时间: 2023-12-03 12:45:35 浏览: 102
好的,下面是一个简单的Java应用程序,实现了使用集合存储数据的图书销售管理子系统。这个程序可以添加、删除和修改图书信息,以及按照图书名称、作者、出版社或价格排序并输出所有图书信息。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Book {
private String name;
private String author;
private String publisher;
private double price;
public Book(String name, String author, String publisher, double price) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "书名:" + name + " 作者:" + author + " 出版社:" + publisher + " 价格:" + price;
}
}
class BookManager {
private ArrayList<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public void updateBook(Book book, String name, String author, String publisher, double price) {
book.setName(name);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPrice(price);
}
public void displayBooks() {
for (Book book : books) {
System.out.println(book.toString());
}
}
public void sortByName() {
Collections.sort(books, Comparator.comparing(Book::getName));
}
public void sortByAuthor() {
Collections.sort(books, Comparator.comparing(Book::getAuthor));
}
public void sortByPublisher() {
Collections.sort(books, Comparator.comparing(Book::getPublisher));
}
public void sortByPrice() {
Collections.sort(books, Comparator.comparing(Book::getPrice));
}
}
public class BookSalesManagement {
public static void main(String[] args) {
BookManager bookManager = new BookManager();
bookManager.addBook(new Book("Java编程思想", "Bruce Eckel", "机械工业出版社", 99.0));
bookManager.addBook(new Book("Effective Java", "Joshua Bloch", "机械工业出版社", 89.0));
bookManager.addBook(new Book("Head First 设计模式", "Eric Freeman", "中国电力出版社", 59.0));
Scanner scanner = new Scanner(System.in);
while (true) {
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. 按价格排序");
System.out.println("8. 显示所有图书");
System.out.println("0. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入图书信息:");
System.out.print("书名:");
String name = scanner.next();
System.out.print("作者:");
String author = scanner.next();
System.out.print("出版社:");
String publisher = scanner.next();
System.out.print("价格:");
double price = scanner.nextDouble();
bookManager.addBook(new Book(name, author, publisher, price));
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的图书名称:");
String bookName = scanner.next();
for (Book book : bookManager.getBooks()) {
if (book.getName().equals(bookName)) {
bookManager.removeBook(book);
System.out.println("删除成功!");
break;
}
}
break;
case 3:
System.out.println("请输入要修改的图书名称:");
String bookName2 = scanner.next();
for (Book book : bookManager.getBooks()) {
if (book.getName().equals(bookName2)) {
System.out.println("请输入新的图书信息:");
System.out.print("书名:");
String newName = scanner.next();
System.out.print("作者:");
String newAuthor = scanner.next();
System.out.print("出版社:");
String newPublisher = scanner.next();
System.out.print("价格:");
double newPrice = scanner.nextDouble();
bookManager.updateBook(book, newName, newAuthor, newPublisher, newPrice);
System.out.println("修改成功!");
break;
}
}
break;
case 4:
bookManager.sortByName();
System.out.println("按书名排序:");
bookManager.displayBooks();
break;
case 5:
bookManager.sortByAuthor();
System.out.println("按作者排序:");
bookManager.displayBooks();
break;
case 6:
bookManager.sortByPublisher();
System.out.println("按出版社排序:");
bookManager.displayBooks();
break;
case 7:
bookManager.sortByPrice();
System.out.println("按价格排序:");
bookManager.displayBooks();
break;
case 8:
System.out.println("所有图书:");
bookManager.displayBooks();
break;
case 0:
System.out.println("程序已退出!");
System.exit(0);
default:
System.out.println("选择错误,请重新选择!");
break;
}
}
}
}
```
这个程序使用了Book类来存储图书信息,BookManager类来管理图书集合,并提供了添加、删除、修改、排序和显示图书信息的功能。在main函数中,我们首先手动添加了三本图书,并使用一个循环来等待用户输入选择。用户可以选择添加、删除、修改、排序或显示所有图书信息。程序将根据用户的选择执行相应的操作,并输出结果。
阅读全文