用Java语言写一个仓库管理系统
时间: 2024-06-08 13:08:46 浏览: 9
好的,我可以给你提供一个简单的仓库管理系统的Java代码示例,你可以根据自己的需求进行修改和完善。
首先,我们需要定义一个仓库类,其中包含仓库的基本信息和库存物品信息:
```java
public class Warehouse {
private String name; // 仓库名称
private String address; // 仓库地址
private List<Item> items; // 库存物品列表
public Warehouse(String name, String address) {
this.name = name;
this.address = address;
this.items = new ArrayList<Item>();
}
// 添加库存物品
public void addItem(Item item) {
items.add(item);
}
// 删除库存物品
public void removeItem(Item item) {
items.remove(item);
}
// 根据物品名称查找库存物品
public Item findItem(String itemName) {
for (Item item : items) {
if (item.getName().equals(itemName)) {
return item;
}
}
return null;
}
// 展示仓库信息
public void display() {
System.out.println("仓库名称:" + name);
System.out.println("仓库地址:" + address);
System.out.println("库存物品列表:");
for (Item item : items) {
item.display();
}
}
}
```
接下来,我们需要定义一个物品类,其中包含物品的基本信息和库存数量:
```java
public class Item {
private String name; // 物品名称
private String description; // 物品描述
private int quantity; // 库存数量
public Item(String name, String description, int quantity) {
this.name = name;
this.description = description;
this.quantity = quantity;
}
// 展示物品信息
public void display() {
System.out.println("物品名称:" + name);
System.out.println("物品描述:" + description);
System.out.println("库存数量:" + quantity);
}
// 获取物品名称
public String getName() {
return name;
}
// 获取库存数量
public int getQuantity() {
return quantity;
}
// 增加库存数量
public void addQuantity(int quantity) {
this.quantity += quantity;
}
// 减少库存数量
public void removeQuantity(int quantity) {
this.quantity -= quantity;
}
}
```
最后,我们需要定义一个主类,用于测试和运行仓库管理系统:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Warehouse warehouse = new Warehouse("仓库A", "地址A");
while (true) {
System.out.println("请输入操作编号:");
System.out.println("1. 添加物品");
System.out.println("2. 删除物品");
System.out.println("3. 查找物品");
System.out.println("4. 展示仓库信息");
System.out.println("5. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入物品名称:");
String name = scanner.nextLine();
System.out.println("请输入物品描述:");
String description = scanner.nextLine();
System.out.println("请输入物品数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
Item item = new Item(name, description, quantity);
warehouse.addItem(item);
System.out.println("添加物品成功!");
break;
case 2:
System.out.println("请输入物品名称:");
String itemName = scanner.nextLine();
Item itemToRemove = warehouse.findItem(itemName);
if (itemToRemove != null) {
warehouse.removeItem(itemToRemove);
System.out.println("删除物品成功!");
} else {
System.out.println("未找到该物品,请检查输入是否正确!");
}
break;
case 3:
System.out.println("请输入物品名称:");
String itemNameToFind = scanner.nextLine();
Item itemToFind = warehouse.findItem(itemNameToFind);
if (itemToFind != null) {
itemToFind.display();
} else {
System.out.println("未找到该物品,请检查输入是否正确!");
}
break;
case 4:
warehouse.display();
break;
case 5:
System.out.println("退出程序!");
return;
default:
System.out.println("无效的操作编号,请重新输入!");
break;
}
}
}
}
```
以上就是一个简单的仓库管理系统的Java代码示例,你可以根据自己的需求进行修改和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)