java库存管理系统代码
时间: 2024-05-06 22:13:34 浏览: 26
Java库存管理系统是一个用于管理商品库存和销售的软件系统。它可以帮助企业实时跟踪库存情况、管理供应链、生成报表等。下面是一个简单的Java库存管理系统的代码示例:
```java
import java.util.HashMap;
import java.util.Map;
public class InventoryManagementSystem {
private Map<String, Integer> inventory;
public InventoryManagementSystem() {
inventory = new HashMap<>();
}
public void addProduct(String product, int quantity) {
if (inventory.containsKey(product)) {
int currentQuantity = inventory.get(product);
inventory.put(product, currentQuantity + quantity);
} else {
inventory.put(product, quantity);
}
}
public void sellProduct(String product, int quantity) {
if (inventory.containsKey(product)) {
int currentQuantity = inventory.get(product);
if (currentQuantity >= quantity) {
inventory.put(product, currentQuantity - quantity);
System.out.println("Successfully sold " + quantity + " " + product);
} else {
System.out.println("Insufficient quantity of " + product);
}
} else {
System.out.println("Product not found");
}
}
public void displayInventory() {
System.out.println("Current Inventory:");
for (Map.Entry<String, Integer> entry : inventory.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
public static void main(String[] args) {
InventoryManagementSystem ims = new InventoryManagementSystem();
ims.addProduct("Apple", 10);
ims.addProduct("Banana", 5);
ims.displayInventory();
ims.sellProduct("Apple", 3);
ims.displayInventory();
}
}
```
这个代码示例实现了一个简单的库存管理系统,包括添加商品、销售商品和显示库存等功能。你可以根据实际需求进行扩展和修改。