欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用集合实现一个简单的购物商城

程序员文章站 2022-04-24 23:12:29
...
//运行展示在最后

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import hncu.cn.baseCode.homeWork.shop.bean.Prodect;
import hncu.cn.baseCode.homeWork.shop.bean.ShoppingCar;
//本例没使用到泛型,所以全部的警告都盖掉了
@SuppressWarnings("all")
public class Main {
	private static List listIni = new ArrayList();// 初始化购物商城 集合
	private static ShoppingCar shoppingCar = new ShoppingCar();// 购物车
	private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		// 初始化
		init();
		System.out.println("****************欢迎来到老干爹商城*****************");
		while (true) {
			System.out.print("请输入操作序号:< 1.查看商品  2.查看购物车  3.退出系统 >");
			int i = scanner.nextInt();
			switch (i) {
			case 1:
				showPro();// 查看商品,是否购买
				break;
			case 2:
				// 查看购物车,是否结算
				showCar();
				break;
			case 3:
				// 退出
				System.exit(0);
				break;
			default:
				break;
			}
		}
	}

	private static void showCar() {
		if (shoppingCar.getList().size() == 0) {
			System.out.println("你的购物车为空");
		} else {
			shoppingCar.print();//展示购物车
			shoppingCar.settleAccounts();//是否结算
		}
	}

	public static void showPro() {
		// 显示商品
		print(listIni);
		System.out.print("是否购买商品<1.是   2.否>");
		int i = scanner.nextInt();
		if (i == 1) {
			int c = shoppingCar.shop(listIni);
			Prodect p = (Prodect) listIni.get(c);
			int count = p.getCount();
			if(count>0){
				((Prodect) listIni.get(c)).setCount(count--);// (如果没了呢....)
			}else {
				System.out.println("该商品库存为 0,再看看其他商品吧~");
			}
		}
	}

	public static void print(List list) {
		System.out.println("编号\t品名\t价格\t描述\t存货量");
		for (Object object : list) {
			System.out.println(object.toString()+((Prodect)(object)).getCount());
		}
	}

	public static void init() {
		for (int i = 0; i < 5; i++) {
			Prodect prodect = new Prodect(i, "VIVO", 500 * (i + 1), ("VIVO" + i + " 代"), i * i + 10);
			listIni.add(prodect);
		}
	}
}



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.text.StyledEditorKit.ForegroundAction;

@SuppressWarnings("all")
public class ShoppingCar {
	private List list = new ArrayList();

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	private int count;

	public ShoppingCar() {
		super();
	}

	public ShoppingCar(int count) {
		super();
		this.count = count;
	}

	public int shop(List li) {
		System.out.print("请输入需要购买的商品编号:");
		int i = new Scanner(System.in).nextInt();
		Prodect po = (Prodect) li.get(i);
		list.add(po);
		return i;// 返回需要购买的编号
	}

	public  void print() {
		System.out.println("编号\t品名\t价格\t描述");
		for (Object object : list) {
			System.out.println(object);
		}
	}
	public void settleAccounts(){
		System.out.println("是否结算?< 1.是  2.否  >");
		int i = new Scanner(System.in).nextInt();
		if(i == 1){
			System.out.println("总金额是:"+countMoney());
		}
	}
	public double countMoney(){
		double s = 0;
		for (Object object : list) {
			double price = ((Prodect)(object)).getPrice();
			s += price;
		}
		//清空购物车
		list.removeAll(list);
		return s;
	}
}

@SuppressWarnings("all")
public class Prodect {
	private int id;
	private String name;
	private double price;
	private String diString;// 描述
	private int count;// 总数

	public Prodect(int id, String name, double price, String diString, int count) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.diString = diString;
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Prodect() {
		super();
	}

	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 String getDiString() {
		return diString;
	}

	public void setDiString(String diString) {
		this.diString = diString;
	}

	@Override
	public String toString() {
		return id + "\t" + name + "\t" + price + "\t" + diString + "\t";
	}

}

下面是运行结果:

利用集合实现一个简单的购物商城