java中编写一个基于控制台的购书系统,实现购书功能
时间: 2024-05-02 12:13:10 浏览: 281
开发基于控制台的购书系统_java源代码.doc
5星 · 资源好评率100%
Java中编写一个基于控制台的购书系统,实现购书功能可以分为以下几个步骤:
1. 定义Book类,包括编号、书名、单价、库存等属性,并提供相应的get和set方法。
2. 定义Order类,包括订单号、订单明细、订单总额等属性,并提供相应的get和set方法。
3. 定义购书系统类,包括输出所有图书信息的方法、购买图书的方法、生成订单的方法等。
4. 在购买图书的方法中,根据用户输入的图书编号和数量,判断库存是否充足,如果充足则更新库存信息,并生成订单。
5. 在生成订单的方法中,根据购买的图书信息计算订单总额,并生成订单号和订单明细。
6. 最后输出顾客的订单信息,包括订单号、订单明细、订单总额。
下面是一个简单的示例代码:
```
public class Book {
private int id;
private String name;
private double price;
private int stock;
public Book(int id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
public class Order {
private int orderId;
private String orderDetail;
private double orderTotal;
public Order(int orderId, String orderDetail, double orderTotal) {
this.orderId = orderId;
this.orderDetail = orderDetail;
this.orderTotal = orderTotal;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderDetail() {
return orderDetail;
}
public void setOrderDetail(String orderDetail) {
this.orderDetail = orderDetail;
}
public double getOrderTotal() {
return orderTotal;
}
public void setOrderTotal(double orderTotal) {
this.orderTotal = orderTotal;
}
}
public class BookStore {
private static Book[] books = {
new Book(1, "Java编程思想", 79.0, 100),
new Book(2, "Java核心技术", 89.0, 50),
new Book(3, "Java并发编程实战", 69.0, 80)
};
private static int orderId = 1000;
public static void main(String[] args) {
displayBooks();
buyBook();
}
public static void displayBooks() {
System.out.println("所有图书信息如下:");
System.out.println("编号\t书名\t单价\t库存");
for (Book book : books) {
System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice() + "\t" + book.getStock());
}
}
public static void buyBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要购买的图书编号:");
int bookId = scanner.nextInt();
System.out.println("请输入要购买的数量:");
int quantity = scanner.nextInt();
Book book = findBookById(bookId);
if (book == null) {
System.out.println("该图书不存在!");
return;
}
if (book.getStock() < quantity) {
System.out.println("库存不足!");
return;
}
book.setStock(book.getStock() - quantity);
double total = book.getPrice() * quantity;
String detail = book.getName() + " x " + quantity;
int id = generateOrderId();
Order order = new Order(id, detail, total);
System.out.println("订单信息如下:");
System.out.println("订单号:" + order.getOrderId());
System.out.println("订单明细:" + order.getOrderDetail());
System.out.println("订单总额:" + order.getOrderTotal());
}
private static Book findBookById(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
private static int generateOrderId() {
return ++orderId;
}
}
```
阅读全文