java实现超市管理系统
作者:气死的笨喵 时间:2022-06-23 13:19:06
本文实例为大家分享了java实现超市管理系统的具体代码,供大家参考,具体内容如下
实现功能
使用选择结构,循环结构,数组的知识实现一个超市管理系统
运行结果:货物清单:
添加商品功能:
删除商品功能:
修改商品:
商品货物实体类
import java.util.Arrays;
public class Goods {
private int id;
private double price;
private String name;
public Goods(int id, double price, String name) {
this.id = id;
this.price = price;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Goods() {
}
//增加商品
public Goods[] add(Goods[]goods,Goods newGood){
goods= Arrays.copyOf(goods,goods.length+1);
goods[goods.length-1]=newGood;
return goods;
}
//删除商品
public static Goods[] del(Goods[]goods,int id){
int i=0;
while(true){
if(goods[i].getId()==id){
goods[i]=null;
return goods;
}
i++;
if(i>=goods.length){
return goods;
}
}
}
//添加商品
public static Goods[] change(Goods[]goods,int id,int newId,double newPrice,String newName){
int i=0;
while (true){
if(goods[i].getId()==id){
goods[i].setId(newId);
goods[i].setPrice(newPrice);
goods[i].setName(newName);
return goods;
}
i++;
if(i>=goods.length){
return goods;
}
}
}
}
超市管理系统类
import java.util.Scanner;
public class marketManager {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Goods g1=new Goods(1000,10,"笔记本");
Goods g2=new Goods(1001,2,"西红柿");
Goods g3=new Goods(1002,5,"辣条");
Goods []goods={g1,g2,g3};
while (true) {
System.out.println("========超市管理系统=======");
System.out.println("1.货物清单 2.增加商品 3.删除商品 4.修改商品 5.退出");
System.out.println("请输入你要操作的编号:");
int i = sc.nextInt();
switch (i){
case 1:
System.out.println("=======商品清单=======");
System.out.println("商品编号"+"\t\t"+"商品单价"+"\t\t"+"商品名称");
for (Goods a:goods) {
if(a==null){
continue;
}
System.out.println(a.getId()+"\t\t"+a.getPrice()+"\t\t"+a.getName());
}
continue;
case 2:
System.out.println("你选择的是增加商品的功能");
System.out.println("请输入你要添加的编号:");
int Id = sc.nextInt();
System.out.println("请输入你要添加的商品价格:");
double price = sc.nextDouble();
System.out.println("请输入你要添加的商品名称");
String name = sc.next();
Goods good=new Goods(Id,price,name);
goods = good.add(goods, good);
System.out.println("添加成功!");
continue;
case 3:
System.err.println("你选择的是删除商品功能");
System.out.println("请输入你要操作的编号:");
Id = sc.nextInt();
goods=Goods.del(goods,Id);
System.out.println("删除成功!");
continue;
case 4:
System.out.println("你选择的是修改商品功能");
System.out.println("请输入你要操作的编号:");
Id=sc.nextInt();
System.out.println("请输入修改后的编号:");
int newId = sc.nextInt();
System.out.println("请输入修改后的价格:");
double newPrice = sc.nextDouble();
System.out.println("请输入修改后的商品名称:");
String newName=sc.next();
goods=Goods.change(goods,Id,newId,newPrice,newName);
continue;
case 5:
return;
}
}
}
}
来源:https://blog.csdn.net/qq_40459545/article/details/109366379
标签:java,超市,管理系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot日志开启SLF4J过程解析
2022-04-23 01:29:57
Spring注解驱动之BeanPostProcessor后置处理器讲解
2023-01-04 17:13:26
聊聊Java并发中的Synchronized
2022-07-26 03:19:24
![](https://img.aspxhome.com/file/2023/7/89567_0s.jpg)
Android实现悬浮对话框代码
2022-11-23 16:12:45
![](https://img.aspxhome.com/file/2023/4/88664_0s.png)
浅析Spring和MyBatis整合及逆向工程
2022-07-09 08:27:11
![](https://img.aspxhome.com/file/2023/9/88579_0s.png)
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)
springboot读取resources下文件的方式详解
2022-05-21 16:14:48
![](https://img.aspxhome.com/file/2023/9/63389_0s.png)
elasticsearch分布式及数据的功能源码分析
2023-08-11 06:31:26
![](https://img.aspxhome.com/file/2023/7/58087_0s.png)
深入解析Java的Hibernate框架中的一对一关联映射
2022-08-07 22:23:39
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
Spark SQL的自定义函数UDF使用
2022-07-31 04:19:47
Java中实现在一个方法中调用另一个方法
2022-05-15 10:02:11
C#二进制序列化实例分析
2022-09-21 01:43:38
浅谈JSON的数据交换、缓存问题和同步问题
2021-08-08 09:31:25
Java编程之双重循环打印图形
2022-02-01 22:06:37
java设计模式之简单工厂模式详解
2021-12-21 22:16:34
详解Jvm中时区设置方式
2023-12-09 02:59:19
Java使用DFA算法实现敏感词过滤的示例代码
2022-10-21 00:56:13
![](https://img.aspxhome.com/file/2023/8/77138_0s.png)
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2022-02-28 02:43:26
![](https://img.aspxhome.com/file/2023/2/85542_0s.jpg)
C#中抽象方法与虚拟方法的区别
2021-10-30 07:30:15