本篇文章给大家分享的是有关利用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;
}
}
}
}
以上就是利用java怎么编写一个超市管理系统,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。