package com.itheima.demo;
public class Goods{
private int id;//编号
private String name;//名称;
private double price;
private int buyNumber;//购买数量
public Goods() {
}
public Goods(int id, String name, double price, int buyNumber) {
this.id = id;
this.name = name;
this.price = price;
this.buyNumber = buyNumber;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getBuyNumber() {
return buyNumber;
}
public void setBuyNumber(int buyNumber) {
this.buyNumber = buyNumber;
}
}``
ublic class ShopCarTest {
public static void main(String[] args) {
//定义商品类,用于后面创建商品对象
//定义购物车对象,使用一个数组对象表示
Goods[] shopCar = new Goods[100];
//3.搭建操作架构
while (true) {
System.out.println("请您输入如下命令进行操作:");
System.out.println("添加商品到购物车:add");
System.out.println("查询商品到购物车:query");
System.out.println("修改商品购买数量:update");
System.out.println("结算购买商品的金额:pay");
Scanner sc = new Scanner(System.in);
System.out.println("请您输入命令: ");
String command = sc.next();
switch (command) {
case "add"://添加商品到购物车
addGoods(shopCar, sc);
break;
case "query"://查询购物车商品展示
queryGoods(shopCar);
break;
case "update"://修改商品的购买数量
updateGoods(shopCar,sc);
break;
case "pay"://结算金额
pay(shopCar);
break;
default:
System.out.println("没有该功能");
}
}
}
public static void pay(Goods[] shopCar) {
//1.定义一个求和变量累加金额
double money=0;
//2.遍历购物车数组全部对象 累加单价+数量
for(int i=0;i<shopCar.length;i++)
{
Goods g=shopCar[i];
if(g!=null)
{
money+=(g.getPrice() * g.getBuyNumber());
}
else
{
break;
}
}
System.out.println("订单金额:"+money);
}
public static void updateGoods(Goods[] shopCar,Scanner sc) {
//让用户输入要修改商品的id 根据id查询要修改的商品对象
while (true) {
System.out.println("请您输入要修改商品的id");
int id=sc.nextInt();
Goods g = getGoodById(shopCar ,id);
if(g==null)
{
System.out.println("对不起,没有购买商品");
}else {
System.out.println("请您输入:" + g.getName()+ "商品新购买数量");
int buyNumber = sc.nextInt();
g.setBuyNumber(buyNumber);
System.out.println("修改完成!");
queryGoods(shopCar);
break;
}
}
}
public static Goods getGoodById(Goods[] shopCar,int id)
{//shopCar =[g1,g2,null ....]
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 queryGoods(Goods[] shopCar) {
System.out.println(">>>>>>>>查询购物车信息如下>>>>>>>>>");
System.out.println("编号\t\t名称\t\t\t价格\t\t\t购买数量");
//shopCar={g1,g2,g3,null ,null}
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if (g != null) {
//展示这个商品对象
System.out.println(g.getId() +
"\t\t" + g.getName() + "\t\t\t" + g.getPrice() + "\t\t\t" + g.getBuyNumber());
} else {
break;
}
}
}
//1.录入用户输入的购买商品的信息
public static void addGoods(Goods[] shopCar, Scanner sc) {
System.out.println("请您输入购买商品的编号(不重复:");
int id = sc.nextInt();
System.out.println("请您输入购买商品的名称");
String name = sc.next();
System.out.println("请您输入购买商品的数量");
int buyNumber = sc.nextInt();
System.out.println("请您输入购买商品的价格");
double price = sc.nextDouble();
//2.把这个购买商品的信息分装成一个商品对象
Goods g = new Goods();
g.setId(id);
g.setName(name);
g.setBuyNumber(buyNumber);
g.setPrice(price);
//3.把这个商品对象添加到购物车数组中去
for (int i = 0; i < shopCar.length; i++) {
if (shopCar[i] == null) {
//说明此位置没有元素存入,把我们新买的商品添加到此处即可
shopCar[i] = g;
break;
}
}
System.out.println("您的商品:" + g.getName() + "添加到购物车完成");
}
}
购物车修改学习笔记
最新推荐文章于 2022-05-31 17:18:24 发布