用java实现一个库存管理系统
时间: 2024-04-23 13:22:40 浏览: 143
这里提供一个简单的Java库存管理系统示例,主要包括库存管理、入库、出库和查询功能:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class InventoryManagementSystem {
private static Map<String, Integer> inventory = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1-库存管理,2-入库,3-出库,4-查询,0-退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
manageInventory();
break;
case 2:
stockIn();
break;
case 3:
stockOut();
break;
case 4:
checkInventory();
break;
case 0:
System.out.println("感谢使用,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private static void manageInventory() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1-添加商品,2-删除商品,0-返回上级菜单");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入商品名称:");
String name = scanner.next();
System.out.println("请输入商品数量:");
int quantity = scanner.nextInt();
if (inventory.containsKey(name)) {
System.out.println("该商品已存在,无法添加!");
} else {
inventory.put(name, quantity);
System.out.println("添加成功!");
}
break;
case 2:
System.out.println("请输入商品名称:");
name = scanner.next();
if (inventory.containsKey(name)) {
inventory.remove(name);
System.out.println("删除成功!");
} else {
System.out.println("该商品不存在,无法删除!");
}
break;
case 0:
return;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private static void stockIn() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品名称:");
String name = scanner.next();
if (inventory.containsKey(name)) {
System.out.println("请输入入库数量:");
int quantity = scanner.nextInt();
inventory.put(name, inventory.get(name) + quantity);
System.out.println("入库成功!");
} else {
System.out.println("该商品不存在,无法入库!");
}
}
private static void stockOut() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品名称:");
String name = scanner.next();
if (inventory.containsKey(name)) {
System.out.println("请输入出库数量:");
int quantity = scanner.nextInt();
int currentQuantity = inventory.get(name);
if (quantity > currentQuantity) {
System.out.println("出库失败,库存不足!");
} else {
inventory.put(name, currentQuantity - quantity);
System.out.println("出库成功!");
}
} else {
System.out.println("该商品不存在,无法出库!");
}
}
private static void checkInventory() {
if (inventory.isEmpty()) {
System.out.println("库存为空!");
} else {
System.out.println("当前库存如下:");
for (String name : inventory.keySet()) {
int quantity = inventory.get(name);
System.out.println(name + ":" + quantity);
}
}
}
}
```
这个示例中,使用了Java中的Map来存储商品名称和数量信息,实现了基本的库存管理、入库、出库和查询功能,具体操作可以根据需要进行修改和扩展。
阅读全文