用java编写水果超市管理系统
时间: 2023-11-27 20:02:38 浏览: 72
好的,以下是一个简单的水果超市管理系统的Java代码示例,包括添加、删除、查询、修改水果信息等功能:
```
import java.util.ArrayList;
import java.util.Scanner;
public class FruitMarketManagementSystem {
// 定义一个静态的 ArrayList 来存储水果信息
private static ArrayList<Fruit> fruitList = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用水果超市管理系统!");
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();
switch (choice) {
case 1:
addFruit();
break;
case 2:
deleteFruit();
break;
case 3:
queryFruit();
break;
case 4:
modifyFruit();
break;
case 5:
System.out.println("谢谢使用水果超市管理系统,再见!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
// 添加水果
private static void addFruit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入水果名称:");
String name = scanner.nextLine();
System.out.println("请输入水果单价:");
double price = scanner.nextDouble();
System.out.println("请输入水果库存数量:");
int stock = scanner.nextInt();
Fruit fruit = new Fruit(name, price, stock);
fruitList.add(fruit);
System.out.println("添加成功!");
}
// 删除水果
private static void deleteFruit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的水果名称:");
String name = scanner.nextLine();
for (Fruit fruit : fruitList) {
if (fruit.getName().equals(name)) {
fruitList.remove(fruit);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该水果,删除失败!");
}
// 查询水果
private static void queryFruit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的水果名称:");
String name = scanner.nextLine();
for (Fruit fruit : fruitList) {
if (fruit.getName().equals(name)) {
System.out.println("水果信息如下:");
System.out.println(fruit.toString());
return;
}
}
System.out.println("未找到该水果,查询失败!");
}
// 修改水果信息
private static void modifyFruit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的水果名称:");
String name = scanner.nextLine();
for (Fruit fruit : fruitList) {
if (fruit.getName().equals(name)) {
System.out.println("原水果信息如下:");
System.out.println(fruit.toString());
System.out.println("请输入新的水果名称(不修改请输入“0”):");
String newName = scanner.nextLine();
if (!newName.equals("0")) {
fruit.setName(newName);
}
System.out.println("请输入新的水果单价(不修改请输入“0”):");
double newPrice = scanner.nextDouble();
if (newPrice != 0) {
fruit.setPrice(newPrice);
}
System.out.println("请输入新的水果库存数量(不修改请输入“0”):");
int newStock = scanner.nextInt();
if (newStock != 0) {
fruit.setStock(newStock);
}
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该水果,修改失败!");
}
}
class Fruit {
private String name;
private double price;
private int stock;
public Fruit(String name, double price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "名称:" + name + ",单价:" + price + "元,库存数量:" + stock;
}
}
```
注意:这只是一个简单的示例代码,实际的水果超市管理系统需要考虑更多的功能和细节,如数据持久化、权限管理、报表生成等。
阅读全文