java练习题--商品管理系统

需求:
在这里插入图片描述
代码实现:

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

public class test {
	static Scanner sc = new Scanner(System.in);
	static List<Map<String, Object>> goods= new ArrayList<Map<String,Object>>();
	public static void main(String[] args) {
		int[] goods_id = {9001,9002,9003};
		String[] goods_name = {"商品A","商品B","商品C"};
		double[] goods_p = {23.45,56.0,9,9};
		initGoods(goods_id, goods_name, goods_p);

		System.out.println("===============欢迎光临================");
		System.out.println("1:货物清单,2:添加货物,3:删除货物,4修改货物,5:退出系统");
		System.out.println("=================================");
		
		while(true) {
			System.out.println("请输入功能编号:");
			int func_code = sc.nextInt();
			switch (func_code) {
			case 1:
				showAll();
				break;
			case 2:
				addGoods();
				break;
			case 3:
				deleteGoods();
				break;
			case 4:
				updateGoods();
				break;
			case 5:
				return;
			}
		}

		
	}
	
	public static void initGoods(int[] goods_id, String[] goods_name, double[] goods_p) {
		for(int i=0;i<goods_id.length;i++) {
			Map<String, Object> temp = new HashMap<String, Object>();
			temp.put("id", goods_id[i]);
			temp.put("name", goods_name[i]);
			temp.put("p", goods_p[i]);
			goods.add(temp);
			
		}
	}
	public static void showAll() {
		System.out.println("货物清单:");
		System.out.println("id\tname\tprice");
		for(Map<String, Object> temp:goods) {
			System.out.println(temp.get("id") + "\t" + temp.get("name") + "\t" +temp.get("p"));
		}
	}
	
	public static void addGoods() {
		System.out.println("货物编号(你可以输入5,然后返回主菜单):");
		int id = sc.nextInt();
		if(id==5) {
			return;
		}
		Map<String, Object> tag = checkGoodsExist(id);
		if(!tag.isEmpty()) {
			System.out.println("货物编号已存在,请输入其他的编号。");
			addGoods();
		}else {
			System.out.println("货物名称:");
			String name = sc.next();
			System.out.println("货物单价:");
			double p = sc.nextDouble();
			
			Map<String, Object> temp = new HashMap<String, Object>();
			temp.put("id", id);
			temp.put("name", name);
			temp.put("p", p);
			goods.add(temp);
		}

	}
	
	public static void deleteGoods() {
		System.out.println("请输入想要删除的货物编号(你可以输入5,然后返回主菜单):");
		int id = sc.nextInt();
		if(id==5) {
			return;
		}
		Map<String, Object> tag = checkGoodsExist(id);
		if(!tag.isEmpty()) {
			goods.remove(tag);
		}else {
			System.out.println("不存在的货物编号。");
			deleteGoods();
		}
		
	}
	
	public static void updateGoods() {
		System.out.println("请输入要修改的货物编号(你可以输入5,然后返回主菜单):");
		int id = sc.nextInt();
		if(id==5) {
			return;
		}
		Map<String, Object> tag = checkGoodsExist(id);
		if(!tag.isEmpty()) {
			goods.remove(tag);
			System.out.println("新的货物编号");
			int new_id = sc.nextInt();
			Map<String, Object> temp = checkGoodsExist(new_id);
			if(!temp.isEmpty()) {
				System.out.println("您输入的货物编号已经存在,请重新输入。");
				updateGoods();
			}else {
				System.out.println("新的货物名称");
				String new_name = sc.next();
				System.out.println("新的货物单价:");
				double new_p = sc.nextDouble();
				tag.put("id", new_id);
				tag.put("name", new_name);
				tag.put("p", new_p);
				goods.add(tag);
			}

		}else {
			System.out.println("不存在的货物。");
			updateGoods();
		}

	}
	
	public static Map<String, Object> checkGoodsExist(int goods_id) {
		Map<String, Object> tag = new HashMap<String, Object>();
		for(Map<String, Object> temp:goods) {
			if(goods_id==(int)temp.get("id")) {
				tag = temp;
			}
		}
		return tag;
	}
	
}


  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值