编写一个程序,用于实现某书店图书的管理,具体功能如下 1)定义一个Book类,用于描述图书的静态动态特征; 2)定义一个集合,用于存储书店中所有的图书对象; 3)提供全部图书信息的显示功能; 4)提供根据书名的图书查找功能; 5)提供买书功能,即买书后,集合中相应删除该图书对象; 6)提供新进书功能,即书店购置了新的图书,相应的集合中应增加图书对象。 要求:系统有清晰的提示信息便于用户使用;应定义书店类,并恰当设计成员变量与成员方法。
时间: 2024-03-20 10:41:40 浏览: 54
一个图书管理系统程序
以下是一份Java代码示例,实现了上述要求:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String name;
private String author;
private double price;
private int stock;
public Book(String name, String author, double price, int stock) {
this.name = name;
this.author = author;
this.price = price;
this.stock = stock;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String toString() {
return name + "\t" + author + "\t" + price + "\t" + stock;
}
}
class Bookstore {
private ArrayList<Book> books;
public Bookstore() {
this.books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public void displayAllBooks() {
System.out.println("书名\t作者\t价格\t库存");
for (Book book : books) {
System.out.println(book.toString());
}
}
public void searchBookByName(String name) {
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.println(book.toString());
return;
}
}
System.out.println("未找到该书");
}
public void buyBook(String name) {
for (Book book : books) {
if (book.getName().equals(name)) {
if (book.getStock() > 0) {
book.setStock(book.getStock() - 1);
System.out.println("购买成功");
if (book.getStock() == 0) {
removeBook(book);
}
} else {
System.out.println("库存不足,购买失败");
}
return;
}
}
System.out.println("未找到该书");
}
}
public class Main {
public static void main(String[] args) {
Bookstore bookstore = new Bookstore();
bookstore.addBook(new Book("Java编程思想", "Bruce Eckel", 79.8, 10));
bookstore.addBook(new Book("算法导论", "Thomas H. Cormen", 98.0, 5));
bookstore.addBook(new Book("Effective Java", "Joshua Bloch", 59.9, 3));
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:\n1. 查看全部图书\n2. 根据书名查找图书\n3. 购买图书\n4. 新进书\n5. 退出");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
bookstore.displayAllBooks();
break;
case 2:
System.out.print("请输入书名:");
String name = scanner.nextLine();
bookstore.searchBookByName(name);
break;
case 3:
System.out.print("请输入书名:");
name = scanner.nextLine();
bookstore.buyBook(name);
break;
case 4:
System.out.print("请输入书名:");
name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
System.out.print("请输入库存:");
int stock = scanner.nextInt();
scanner.nextLine();
bookstore.addBook(new Book(name, author, price, stock));
System.out.println("新进书成功");
break;
case 5:
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("无效操作,请重新选择");
}
}
}
}
```
该程序中,Book类用于描述图书的静态动态特征,包括书名、作者、价格和库存;Bookstore类用于描述书店的特征,包括存储所有图书的集合以及各种操作,如添加、删除、显示、查找和购买等。在程序中,我们先手动添加了一些图书,然后通过一个无限循环的菜单来提供各种操作,根据用户的选择来执行相应的方法。
阅读全文