利用JavaBean改造的购物车案例
package zhtPractice.JavaBean改造购物车;
/**
* Created with IntelliJ IDEA.
*
* @Author: zht
* @Date: 2022年01月03日 21:43
* @Description:
*/
public class Goods {
private int id;
private String name;
private double price;
private int count;
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 getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package zhtPractice.JavaBean改造购物车;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Author: zht
* @Date: 2022年01月03日 21:45
* @Description:
*/
public class shopCarTest {
public static void main(String[] args) {
Goods[] shopCar = new Goods[100];
while (true) {
System.out.println("请选择你所需要的功能:");
System.out.println("添加商品到购物车:add");
System.out.println("查询商品到购物车: query");
System.out.println("修改商品购物数量:update");
System.out.println("结算购买商品的价格:count");
Scanner sc = new Scanner(System.in);
String choose = sc.nextLine();
switch (choose){
case "add":
addGoods(shopCar,sc);
break;
case "query":
queryGoods(shopCar);
break;
case "update":
updateGoods(shopCar);
break;
case "count":
countGoods(shopCar);
break;
}
}
}
private static void countGoods(Goods[] shopCar) {
double sum=0.00;
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if(g != null){
sum += g.getCount() * g.getPrice();
}else{
break;
}
}
System.out.println("商品的总价格是:" + sum);
}
private static void updateGoods(Goods[] shopCar) {
while (true) {
System.out.println("请选择需要修改数量的商品id:");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
Goods g = getGoodsById(shopCar,id);
if(g == null){
System.out.println("抱歉,没有该商品!");
}else{
System.out.println("请您输入新的商品数量");
int num = sc.nextInt();
g.setCount(num);
System.out.println("修改完成!");
queryGoods(shopCar);
break;
}
}
}
private static Goods getGoodsById(Goods[] shopCar , int id) {
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;//已经遍历了所有的id都没有找到
}
private static void queryGoods(Goods[] shopCar) {
System.out.println("=========查询商品信息如下=======");
System.out.println("编号\t\t名称\t\t价格\t\t数量");
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" + g.getPrice() + "\t\t" + g.getCount());
}else {
break;
}
}
}
private 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 count = sc.nextInt();
System.out.println("请输入商品价格:");
double price = sc.nextDouble();
//把这个商品封装成一个对象
Goods g = new Goods();
g.setId(id);
g.setName(name);
g.setPrice(price);
g.setCount(count);
//遍历购物车,看哪边有空的地方,加入商品到购物车
for (int i = 0; i < shopCar.length; i++) {
if(shopCar[i] == null){
shopCar[i] = g;
break;
}
}
System.out.println("商品已经添加完成!");
}
}