java 写一个进销存系统
时间: 2024-05-29 15:03:18 浏览: 8
进销存系统是一个比较复杂的系统,需要考虑到多个方面,包括货物的进出、库存管理、销售管理等。在Java中,可以使用面向对象编程的思想来实现进销存系统。下面是一个简单的进销存系统的代码实现,供参考:
```java
import java.util.Scanner;
public class InventorySystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Inventory inventory = new Inventory();
while (true) {
System.out.println("请选择操作:1.进货 2.销售 3.查看库存 4.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入货物名称:");
String name = scanner.next();
System.out.println("请输入货物数量:");
int quantity = scanner.nextInt();
inventory.addGoods(name, quantity);
System.out.println("进货成功!");
break;
case 2:
System.out.println("请输入货物名称:");
name = scanner.next();
System.out.println("请输入货物数量:");
quantity = scanner.nextInt();
if (inventory.sellGoods(name, quantity)) {
System.out.println("销售成功!");
} else {
System.out.println("库存不足,销售失败!");
}
break;
case 3:
inventory.showInventory();
break;
case 4:
System.exit(0);
}
}
}
}
class Goods {
private String name;
private int quantity;
public Goods(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;
}
public void addQuantity(int quantity) {
this.quantity += quantity;
}
public boolean reduceQuantity(int quantity) {
if (this.quantity >= quantity) {
this.quantity -= quantity;
return true;
} else {
return false;
}
}
}
class Inventory {
private Goods[] goodsList = new Goods[100];
public void addGoods(String name, int quantity) {
for (int i = 0; i < goodsList.length; i++) {
if (goodsList[i] == null) {
goodsList[i] = new Goods(name, quantity);
break;
} else if (goodsList[i].getName().equals(name)) {
goodsList[i].addQuantity(quantity);
break;
}
}
}
public boolean sellGoods(String name, int quantity) {
for (int i = 0; i < goodsList.length; i++) {
if (goodsList[i] != null && goodsList[i].getName().equals(name)) {
return goodsList[i].reduceQuantity(quantity);
}
}
return false;
}
public void showInventory() {
System.out.println("货物名称\t货物数量");
for (int i = 0; i < goodsList.length; i++) {
if (goodsList[i] != null) {
System.out.println(goodsList[i].getName() + "\t" + goodsList[i].getQuantity());
}
}
}
}
```
以上代码实现了一个简单的进销存系统,用户可以选择进货、销售、查看库存等操作。系统会根据用户的选择调用相应的方法进行处理。具体实现过程可以参考代码中的注释。