2019-7-14 [JavaSE] [项目案例] List 购物车管理系统1.0

1.项目要求

在这里插入图片描述

2.实现代码

import java.util.ArrayList;
//1.先成立一个的商品类
class ShopCart{
	//封装
	private int num;
	private String goods;
	private double price;
	private int count;
	//初始化
	public ShopCart() {		
	}
	public ShopCart(int num, String goods, double price, int count) {
		super();
		this.num = num;
		this.goods = goods;
		this.price = price;
		this.count = count;
	}
	//访问器与修改器
	public String getGoods() {
		return goods;
	}
	public void setGoods(String goods) {
		this.goods = goods;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	//toString输出
	@Override
	public String toString() {
		return "ShopCart [num=" + num + ", goods=" + goods + ", price=" + price + ", count=" + count + "]";
	}
}
//2.再成立一个购物功能类
class shopping{
	List<ShopCart> list = new ArrayList<>();
	//增
	public void add(ShopCart sc) {
		list.add(sc);
		System.out.println("添加成功");
	}	
	//改
	public void update (int gnum,int gshu) {
		for (ShopCart sc : list) {
			if (sc.getNum() == gnum) {
				sc.setNum(gnum);
				sc.setCount(gshu);
				System.out.println("修改成功");
				break;
			}
		}	
	}
	//删
	public void delete(int snum) {
		for (ShopCart sc : list) {
			if (sc.getNum() == snum) {
				list.remove(sc);
				System.out.println("删除成功");
				break;
			}
		}
	}
	//查
	public void showshopcart() {
			for (ShopCart shopCart : list) {
				System.err.println(shopCart);
		}
	}	
}
//主函数
public class Teat {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		shopping sp	  = new shopping();
		System.out.println("*********菜单*********");
		System.out.println("1.添加;2.修改数量;3.删除商品;4.查询全部;5.退出");
		System.out.println("*******************");
		
		while (true) {
			int i = input.nextInt();
			switch (i) {
			case 1:
				System.out.println("输入编号:");
				int hao =input.nextInt();
				System.out.println("输入名称:");
				String ming =input.next();
				System.out.println("输入价格:");
				double jia =input.nextDouble();
				System.out.println("输入数量:");
				int shu =input.nextInt();
				ShopCart sc = new ShopCart();
				sc.setNum(hao);
				sc.setGoods(ming);
				sc.setCount(shu);
				sc.setPrice(jia);
				sp.add(sc);
				break;
			case 2:
				System.out.println("输入要修改的编号:");
				int gnum =input.nextInt();
				System.out.println("输入新的数量:");
				int gshu =input.nextInt();
				sp.update(gnum, gshu);
				System.out.println("修改后的购物车为:");
				sp.showshopcart();
				break;
			case 3:
				System.out.println("输入要删除的商品编号:");
				int snum =input.nextInt();
				sp.delete(snum);
				System.out.println("删除后购物车如下所示:");
				sp.showshopcart();
				break;
			case 4:
				System.out.println("当前购物车如下所示:");
				sp.showshopcart();
				break;
			case 5:
				System.out.println("退出成功");
				System.exit(0);
		break;
			}
		}	
	}
}

3.输出效果

*********菜单*********
1.添加;2.修改数量;3.删除商品;4.查询全部;5.退出
*******************
1
输入编号:
1
输入名称:
1
输入价格:
1
输入数量:
1
添加成功
1
输入编号:
2
输入名称:
2
输入价格:
2
输入数量:
2
添加成功
2
输入要修改的编号:
1
输入新的数量:
14
修改成功
修改后的购物车为:
ShopCart [num=1, goods=1, price=1.0, count=14]
ShopCart [num=2, goods=2, price=2.0, count=2]
3
输入要删除的商品编号:
2
修改成功
删除后购物车如下所示:
ShopCart [num=1, goods=1, price=1.0, count=14]
4
当前购物车如下所示:
ShopCart [num=1, goods=1, price=1.0, count=14]
5
退出成功
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值