写一个超市商品管理系统,使用java代码,在控制台打印出来,功能包含商品的录入,删除,修改,查询
时间: 2024-01-13 20:04:23 浏览: 28
好的,以下是一个简单的超市商品管理系统的Java代码,可以在控制台上运行:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class SupermarketManagementSystem {
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<Product>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.录入商品 2.删除商品 3.修改商品 4.查询商品 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入商品名称:");
String name = scanner.next();
System.out.println("请输入商品价格:");
double price = scanner.nextDouble();
Product product = new Product(name, price);
products.add(product);
System.out.println("商品录入成功!");
break;
case 2:
System.out.println("请输入要删除的商品名称:");
String deleteName = scanner.next();
boolean isDeleted = false;
for (Product p : products) {
if (p.getName().equals(deleteName)) {
products.remove(p);
System.out.println("商品删除成功!");
isDeleted = true;
break;
}
}
if (!isDeleted) {
System.out.println("找不到要删除的商品!");
}
break;
case 3:
System.out.println("请输入要修改的商品名称:");
String modifyName = scanner.next();
boolean isModified = false;
for (Product p : products) {
if (p.getName().equals(modifyName)) {
System.out.println("请输入修改后的商品名称:");
String newName = scanner.next();
System.out.println("请输入修改后的商品价格:");
double newPrice = scanner.nextDouble();
p.setName(newName);
p.setPrice(newPrice);
System.out.println("商品修改成功!");
isModified = true;
break;
}
}
if (!isModified) {
System.out.println("找不到要修改的商品!");
}
break;
case 4:
System.out.println("请输入要查询的商品名称:");
String queryName = scanner.next();
boolean isFound = false;
for (Product p : products) {
if (p.getName().equals(queryName)) {
System.out.println("商品名称:" + p.getName() + ",商品价格:" + p.getPrice());
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("找不到要查询的商品!");
}
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
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;
}
}
```
该程序包含了商品的录入、删除、修改、查询等功能,使用ArrayList来存储商品信息,每次操作完后都会提示用户继续操作或退出程序。