这里写目录标题
一 创建一个Goods类
主要封装了订单号,商品名,购买数量,商品价格四个变量
public class Goods {
private int id;
private String name;
private int buyNumber;
private double price;
public Goods() {
super();
}
public Goods(int id,String name,int buyNumber,double price) {
super();
this.id=id;
this.name=name;
this.buyNumber=buyNumber;
this.price=price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBuyNumber() {
return buyNumber;
}
public void setBuyNumber(int buyNumber) {
this.buyNumber = buyNumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
二 定义一个功能类
实现购物车商品总价的计算,商品的增删改查操作,主要实现一些逻辑功能
import java.util.Scanner;
public class TestShopCar {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("==============欢迎登录购物车==================");
Goods ShopCar[]=new Goods[100];//自定义类的数组被定义出来
Scanner sc=new Scanner(System.in);
while(true) {
System.out.println("请输入列表中的选项");
System.out.println("1,添加商品请输入:add");
System.out.println("2,查询购物车现存商品请输入:querry");
System.out.println("3,更新购物车中商品的购买数量请输入:update");
System.out.println("4,结算购物车中商品的价值总额:pay");
String command=sc.next();
switch (command) {
case "add":
addGoods(ShopCar,sc);
break;
case "querry":
querryGoods(ShopCar);
break;
case "update":
updateGoods(ShopCar,sc);
break;
case "pay":
payGoods(ShopCar);
break;
default:
System.out.println("输入内容不符合题目要求");
break;
}
}
}
public static void querryGoods(Goods[] shopCar) {
// TODO Auto-generated method stub
for (int i = 0; i < shopCar.length; i++) {
Goods g=new Goods();
g=shopCar[i];
if (g!=null) {
System.out.println(g.getId()+" "+g.getName()+" "+g.getPrice()+" "+g.getBuyNumber());
} else {
break;
}
}
}
public static void updateGoods(Goods[] shopCar, Scanner sc) {
// TODO Auto-generated method stub
while (true) {
//请输入需要修改的商品编号
System.out.println("请输入要修改的货物编号 ");
int id=sc.nextInt();
Goods g=getGoodsById(id,shopCar);
if(g==null) {
System.out.println("对不起,购物车里并没有购买该商品");
}else {
//说明商品存在
System.out.println("请输入您要修改"+g.getId()+"的预期数量");
int buynumber=sc.nextInt();
g.setBuyNumber(buynumber);
System.out.println("修改完成");
querryGoods(shopCar);
}
}
}
public static Goods getGoodsById(int id, Goods[] shopCar) {
// TODO Auto-generated method stub
for (int i= 0; i< shopCar.length; i++) {
Goods g=shopCar[i];
if(g!=null) {
if(g.getId()==id) {
return g;
}else {
return null;//都没有找到
}
}
}
return null;
}
public static void payGoods(Goods[] shopCar) {
// TODO Auto-generated method stub
double money=0;
for (int i = 0; i < shopCar.length; i++) {
Goods g=shopCar[i];
if (g!=null) {
money=money+(g.getBuyNumber()*g.getPrice());
} else {
break;
}
}
System.out.println("购物车中商品的价值总和是:"+money);
}
public static void addGoods(Goods[] shopCar, Scanner sc) {
// 你不会有像现在这样写代码的机会
//1 依次从键盘输入各项信息
System.out.println("请输入商品编号:");
int id=sc.nextInt();
System.out.println("请输入商品名称:");
String name=sc.next();
System.out.println("请输入商品单价:");
double price=sc.nextDouble();
System.out.println("请输入商品的购买数量:");
int buyNumber=sc.nextInt();
//2 将零散信息封装起来
Goods g=new Goods();
g.setId(id);
g.setName(name);
g.setPrice(price);
g.setBuyNumber(buyNumber);
//3. 将信息赋值给自定义类型数组
for (int j = 0; j < shopCar.length; j++) {
if (shopCar[j]==null) {
shopCar[j]=g;
break;
}
}
}
}