构建商品类:
package ShopCar;
public class Goods {
private int id;
private String name;//名字
private double price;//价格
private int restnum;//剩余数量
public Goods clone()
{
Goods newGood=new Goods();
newGood.setId(Goods.this.getId());
newGood.setName(Goods.this.getName());
newGood.setPrice(Goods.this.getPrice());
newGood.setRestnum(Goods.this.getRestnum());
return newGood;
}
public Goods(){}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getRestnum() {
return restnum;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setRestnum(int restnum) {
this.restnum = restnum;
}
public Goods(int id,String name,double price,int restnum) {
this.id=id;
this.name=name;
this.price=price;
this.restnum=restnum;
}
}
购物车实现:
package ShopCar;
import java.util.Scanner;
public class ShopCar {
public static void main(String[] args) {
Goods[] ShopCar=new Goods[50];
Goods[] allGoods=new Goods[10];
creatGoods(allGoods);
while (true)
{
System.out.println("商品展示:");
showGoods(allGoods);
System.out.println("请选择操作:");
System.out.println("添加商品:add");
System.out.println("删除商品:delete");
System.out.println("查询购物车商品:show");
System.out.println("修改商品数量:update");
System.out.println("结算金额:pay");
System.out.println("退出:exit");
Scanner sc=new Scanner(System.in);
System.out.println("输入命令:");
String command=sc.next();
switch (command)
{
case"add"->add(ShopCar,allGoods);
case"delete"->delete(ShopCar,allGoods);
case"show"->show(ShopCar);
case"update"->update(ShopCar,allGoods);
case"pay"->pay(ShopCar);
case"exit"->System.exit(0);
default -> System.out.println("无效命令");
}
}
}
private static void pay(Goods[] shopCar) {
double pay=0;
for (int i = 0; i < shopCar.length; i++)
{
if(shopCar[i]==null)
continue;
else
{
pay+=shopCar[i].getPrice()*shopCar[i].getRestnum();
}
}
System.out.println("共需支付"+pay+"元");
System.out.println("操作结束");
}
private static void update(Goods[] shopCar,Goods[] allGoods) {
Scanner sc=new Scanner(System.in);
System.out.println("请选择商品序号");
int id=sc.nextInt();
if(id<0||id>= allGoods.length)
{
System.out.println("无此商品!");
return;
}
for (int i=0;i< shopCar.length;i++)
{
if(shopCar[i]==null)
continue;
if(shopCar[i].getId()==id)
{
allGoods[id].setRestnum(allGoods[id].getRestnum()+shopCar[i].getRestnum());
System.out.println("请重新输入数量:");
int num=sc.nextInt();
if(num<=0||num>allGoods[id].getRestnum())
{
System.out.println("数量选择错误!");
return;
}
shopCar[i].setRestnum(num);
allGoods[id].setRestnum(allGoods[id].getRestnum()-num);
System.out.println("操作结束");
return;
}
}
System.out.println("购物车中无此商品!");
}
private static void show(Goods[] shopCar) {
for (int i = 0; i < shopCar.length; i++) {
if(shopCar[i]==null)
continue;
System.out.println(shopCar[i].getId()+".名称:"+shopCar[i].getName()+",价格:"+shopCar[i].getPrice()+",数量:"+shopCar[i].getRestnum());
}
System.out.println("操作结束");
}
private static void delete(Goods[] shopCar,Goods[] allGoods) {
Scanner sc=new Scanner(System.in);
System.out.println("请选择商品序号");
int id=sc.nextInt();
if(id<0||id>= allGoods.length)
{
System.out.println("无此商品!");
return;
}
for (int i=0;i< shopCar.length;i++)
{
if(shopCar[i]==null)
continue;
if(shopCar[i].getId()==id)
{
allGoods[id].setRestnum(shopCar[i].getRestnum()+allGoods[id].getRestnum());
shopCar[i]=null;
System.out.println("操作结束");
return;
}
}
System.out.println("购物车中无此商品!");
}
private static void add(Goods[] shopCar, Goods[] allGoods) {
Scanner sc=new Scanner(System.in);
System.out.println("请选择商品序号:");
int id=sc.nextInt();
if(id<0||id>= allGoods.length)
{
System.out.println("无此商品!");
return;
}
//查询购物车中是否已经存在该商品
for(int i=0;i< shopCar.length;i++)
{
if(shopCar[i]==null)
continue;
if(shopCar[i].getId()==id)
{
System.out.println("该商品已经放入购物车,请修改数量");
return;
}
}
//查找空位并添加
for (int pos = 0; pos < shopCar.length; pos++)
{
if(shopCar[pos]==null)
{
shopCar[pos]=allGoods[id].clone();//需要克隆
System.out.println("请输入购买数量:");
int num=sc.nextInt();
if(num<=0||num>allGoods[id].getRestnum())
{
System.out.println("数量选择错误!");
return;
}
shopCar[pos].setRestnum(num);
allGoods[id].setRestnum(allGoods[id].getRestnum()-num);
System.out.println("操作结束");
return;
}
}
System.out.println("购物车已满!");
}
private static void creatGoods(Goods[] allGoods) {
allGoods[0]=new Goods(0, "笔记本", 5.00, 6);
allGoods[1]=new Goods(1,"水杯",7.30,9);
allGoods[2] =new Goods(2,"零食",6.00,4);
allGoods[3] =new Goods(3,"橘子",5.00,15);
allGoods[4]=new Goods(4,"鞋",32.50,100);
allGoods[5]=new Goods(5,"羽绒服",65.80,28);
allGoods[6]=new Goods(6,"vivo手机",1580.00,1020);
allGoods[7]=new Goods(7,"电饭煲",388.00,50);
allGoods[8]=new Goods(8,"床上用品",98.00,540);
allGoods[9] =new Goods(9,"伞",9.00,2346);
}
private static void showGoods(Goods[] allGoods) {
for(int i=0;i<10;i++)
{
System.out.println(allGoods[i].getId()+".名称:"+allGoods[i].getName()+",价格:"+allGoods[i].getPrice()+",库存:"+allGoods[i].getRestnum());
}
}
}