将他们结合成完整的代码
时间: 2024-03-17 10:41:27 浏览: 19
好的,下面是结合你提供的要求的完整代码示例:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
class Book {
private String name; // 书名
private String author; // 作者
private double price; // 价格
private int num; // 库存数量
// 构造方法
public Book(String name, String author, double price, int num) {
this.name = name;
this.author = author;
this.price = price;
this.num = num;
}
// 获取书名
public String getName() {
return name;
}
// 获取作者
public String getAuthor() {
return author;
}
// 获取价格
public double getPrice() {
return price;
}
// 获取库存数量
public int getNum() {
return num;
}
// 设置库存数量
public void setNum(int num) {
this.num = num;
}
// 判断两本书是否相同
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Book other = (Book) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (author == null) {
if (other.author != null) {
return false;
}
} else if (!author.equals(other.author)) {
return false;
}
return true;
}
// 显示图书信息
@Override
public String toString() {
return "书名:" + name + ",作者:" + author + ",价格:" + price + ",库存数量:" + num;
}
}
class BookStore {
private ArrayList<Book> books = new ArrayList<Book>();
// 显示全部图书信息
public void displayAllBooks() {
System.out.println("所有图书信息如下:");
if (books.size() == 0) {
System.out.println("暂无图书信息!");
return;
}
Iterator<Book> it = books.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
// 根据书名查找图书
public void searchBookByName(String name) {
System.out.println("查找结果如下:");
boolean flag = false;
Iterator<Book> it = books.iterator();
while (it.hasNext()) {
Book b = it.next();
if (b.getName().equals(name)) {
System.out.println(b);
flag = true;
}
}
if (!flag) {
System.out.println("没有找到相关图书信息!");
}
}
// 卖书
public void sellBook(String name, int num) {
boolean flag = false;
Iterator<Book> it = books.iterator();
while (it.hasNext()) {
Book b = it.next();
if (b.getName().equals(name)) {
if (b.getNum() >= num) {
b.setNum(b.getNum() - num);
System.out.println("购买成功!");
} else {
System.out.println("库存不足,购买失败!");
}
flag = true;
break;
}
}
if (!flag) {
System.out.println("没有找到相关图书信息!");
}
}
// 新进书
public void addNewBook(Book book) {
if (books.contains(book)) {
Iterator<Book> it = books.iterator();
while (it.hasNext()) {
Book b = it.next();
if (b.equals(book)) {
b.setNum(b.getNum() + book.getNum());
System.out.println("图书已存在,库存已更新!");
return;
}
}
} else {
books.add(book);
System.out.println("图书已添加!");
}
}
}
public class BookStoreManagementSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BookStore bookStore = new BookStore();
while (true) {
System.out.println("欢迎使用图书管理系统!");
System.out.println("1. 显示全部图书信息");
System.out.println("2. 根据书名查找图书");
System.out.println("3. 卖书");
System.out.println("4. 新进书");
System.out.println("0. 退出系统");
System.out.print("请选择要进行的操作:");
int choice = input.nextInt();
switch (choice) {
case 1:
bookStore.displayAllBooks();
break;
case 2:
System.out.print("请输入要查找的书名:");
String name = input.next();
bookStore.searchBookByName(name);
break;
case 3:
System.out.print("请输入要卖出的书名:");
String name1 = input.next();
System.out.print("请输入要卖出的数量:");
int num = input.nextInt();
bookStore.sellBook(name1, num);
break;
case 4:
System.out.print("请输入新进的书名:");
String name2 = input.next();
System.out.print("请输入新进的作者:");
String author = input.next();
System.out.print("请输入新进的价格:");
double price = input.nextDouble();
System.out.print("请输入新进的数量:");
int num1 = input.nextInt();
Book book = new Book(name2, author, price, num1);
bookStore.addNewBook(book);
break;
case 0:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
请注意,以上代码仅作为示例,可能需要根据实际情况进行修改和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)