java 模拟库存管理系统
时间: 2023-11-04 12:52:00 浏览: 189
java版 库存管理系统
一个简单的 Java 模拟库存管理系统可能包括以下基本功能:
1. 商品信息管理:包括商品名称、编号、进价、售价、库存数量等。
2. 订单管理:可以添加、修改、查询订单信息,包括订单号、下单时间、客户信息、商品信息、订单金额等。
3. 库存管理:可以实时查询某个商品的库存数量,当订单完成后,需要更新库存数量。
4. 报表管理:可以生成各种报表,比如销售报表、进货报表、库存报表等,用于帮助管理员掌握经营情况。
下面是一个简单的库存管理系统的示例代码:
```
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class InventoryManagementSystem {
private List<Product> products = new ArrayList<>();
private List<Order> orders = new ArrayList<>();
public void addProduct(Product product) {
products.add(product);
}
public void addOrder(Order order) {
orders.add(order);
}
public void printInventoryReport() {
System.out.println("Inventory Report:");
for (Product product : products) {
System.out.println(product.getName() + ", " + product.getQuantity());
}
}
public void printSalesReport(Date startDate, Date endDate) {
System.out.println("Sales Report from " + startDate + " to " + endDate + ":");
double totalSales = 0;
for (Order order : orders) {
if (order.getOrderDate().after(startDate) && order.getOrderDate().before(endDate)) {
System.out.println(order.getOrderNumber() + ", " + order.getOrderDate() + ", " + order.getCustomerName() + ", " + order.getTotalPrice());
totalSales += order.getTotalPrice();
}
}
System.out.println("Total sales: " + totalSales);
}
public static void main(String[] args) {
InventoryManagementSystem system = new InventoryManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add product");
System.out.println("2. Add order");
System.out.println("3. Print inventory report");
System.out.println("4. Print sales report");
System.out.println("5. Exit");
System.out.print("Select an option: ");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("Enter product name: ");
String name = scanner.next();
System.out.print("Enter product quantity: ");
int quantity = scanner.nextInt();
Product product = new Product(name, quantity);
system.addProduct(product);
System.out.println("Product added successfully.");
break;
case 2:
System.out.print("Enter order number: ");
int orderNumber = scanner.nextInt();
System.out.print("Enter customer name: ");
String customerName = scanner.next();
Order order = new Order(orderNumber, new Date(), customerName);
while (true) {
System.out.print("Enter product name (or 'q' to quit): ");
String productName = scanner.next();
if (productName.equals("q")) {
break;
}
System.out.print("Enter quantity: ");
int orderQuantity = scanner.nextInt();
Product orderedProduct = system.products.stream().filter(p -> p.getName().equals(productName)).findFirst().orElse(null);
if (orderedProduct == null) {
System.out.println("Product not found.");
} else if (orderedProduct.getQuantity() < orderQuantity) {
System.out.println("Insufficient quantity in stock.");
} else {
order.addOrderItem(new OrderItem(orderedProduct, orderQuantity));
orderedProduct.setQuantity(orderedProduct.getQuantity() - orderQuantity);
}
}
system.addOrder(order);
System.out.println("Order added successfully.");
break;
case 3:
system.printInventoryReport();
break;
case 4:
System.out.print("Enter start date (yyyy-mm-dd): ");
String startDateString = scanner.next();
System.out.print("Enter end date (yyyy-mm-dd): ");
String endDateString = scanner.next();
Date startDate = java.sql.Date.valueOf(startDateString);
Date endDate = java.sql.Date.valueOf(endDateString);
system.printSalesReport(startDate, endDate);
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option.");
}
}
}
}
class Product {
private String name;
private int quantity;
public Product(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
class Order {
private int orderNumber;
private Date orderDate;
private String customerName;
private List<OrderItem> orderItems = new ArrayList<>();
public Order(int orderNumber, Date orderDate, String customerName) {
this.orderNumber = orderNumber;
this.orderDate = orderDate;
this.customerName = customerName;
}
public int getOrderNumber() {
return orderNumber;
}
public Date getOrderDate() {
return orderDate;
}
public String getCustomerName() {
return customerName;
}
public List<OrderItem> getOrderItems() {
return orderItems;
}
public double getTotalPrice() {
double totalPrice = 0;
for (OrderItem orderItem : orderItems) {
totalPrice += orderItem.getProduct().getPrice() * orderItem.getQuantity();
}
return totalPrice;
}
public void addOrderItem(OrderItem orderItem) {
orderItems.add(orderItem);
}
}
class OrderItem {
private Product product;
private int quantity;
public OrderItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
}
```
这个例子实现了一个简单的控制台界面,可以通过不同的选项添加商品、添加订单、打印库存报告、打印销售报告等。当用户添加订单时,系统会自动更新库存数量。
阅读全文