超市库存管理系统

项目:【超市库存管理系统】

请详细看一下三张图片,明白其中的大概框架
一个包中俩个类
一个包中俩个类
第一个类 -------(自定义类)有多个货物的属性
在这里插入图片描述
第二个类----------有多个方法(8)
在这里插入图片描述

================================================================

package Day_08;
/*自定义类 Goods_Attribute(商品属性)
 *  定义属性
 *  编号 ,Int
 *  商品名称,String
 *  商品单价,double
 *  商品数量,int
 *  商品金额,double    
 */
public class Goods_Attribute {
 int ID;
 String name;
 double price;
 int count;
 double money;
}

================================================================

package Day_08;

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

/*
 * 【超市库存管理系统】
 * 步骤:
 *    1.实现数据的初始化----创建集合(自定义类)
 *    2.显示主菜单
 *    3.键盘键入序号,执行相对应的功能
 *     3.1 查看超市库存清单
 *     3.2 添加新货物
 *     3.3 删除货物
 *     3.4 修改货物
 *     3.5 退出系统
 */
public class Supermarket {

	public static void main(String[] args) {
		ArrayList<Goods_Attribute> arr = new ArrayList<Goods_Attribute>();
		// 调用 :1.商品信息的初始化
		attribute_Value(arr);

		// 死循环
		while (true) {
			// 调用:2.显示主菜单
			showMainMenu(arr);
			// 调用:3.键盘键入序号,执行相对应的功能
			int choose = chooseFunction(arr);

			switch (choose) {
			case 1:
				// 3.1 查看超市库存清单
				lookoverList(arr);
				break;
			case 2:
				// 3.2 添加新货物
				addGoods(arr);
				break;
			case 3:
				// 3.3 删除货物
				delete(arr);
				break;
			case 4:
				// 3.4 修改货物
				update(arr);
				break;
			case 5:
				// 3.5 退出系统
				exit();
				return;
			default:
				System.out.println("您输入的操作序号有误!");
			}
		}
	}

	/*
	 * 【1】定义方法 :实现数据的初始化 商品的信息用集合存储,因为数组定长,而集合只能存引用数据类型(类) 为自定义类中的属性赋值
	 */

	public static void attribute_Value(ArrayList<Goods_Attribute> arr) {
		Goods_Attribute e1 = new Goods_Attribute();
		e1.ID = 0001;
		e1.name = "可口可乐";
		e1.price = 3;
		e1.count = 20;

		Goods_Attribute e2 = new Goods_Attribute();
		e2.ID = 0002;
		e2.name = "纯甄";
		e2.price = 6;
		e2.count = 10;

		Goods_Attribute e3 = new Goods_Attribute();
		e3.ID = 0003;
		e3.name = "红烧牛肉面";
		e3.price = 4.5;
		e3.count = 15;

		// 存储到集合
		arr.add(e1);
		arr.add(e2);
		arr.add(e3);
	}

	/*
	 * 【2】定义方法:显示主菜单 ———打印即可 返回值? 无 参数? 集合
	 */
	public static void showMainMenu(ArrayList<Goods_Attribute> arr) {
		System.out.println();
		System.out.println("===================超市库存管理====================");
		System.out.println("1.查看货物清单" + "  " + "2.添加新货物" + "  " + "3.删除货物"
				+ "  " + "4.修改货物" + "  " + "5.退出");
		System.out.println("请您输入要操作的功能序号:");
	}

	/*
	 * 【3】定义方法:3.键盘键入序号,执行相对应的功能 返回值? 有(序号) 参数?集合
	 */
	public static int chooseFunction(ArrayList<Goods_Attribute> arr) {
		// 键盘键入功能序号
		Scanner sc = new Scanner(System.in);
		int number = sc.nextInt();
		return number;
	}

	/*
	 * 【3.1】 查看超市库存清单 返回值? 无 参数? 集合
	 */
	public static void lookoverList(ArrayList<Goods_Attribute> arr) {
		System.out.println("============超市库存清单============");
		System.out.println("商品编号" + "   " + "商品名称" + "   " + "商品单价" + "   "
				+ "商品库存");
		// 定义变量,保存好总库存数,总金额
		int totalCount = 0;
		double totalMoney = 0;
		// 遍历集合
		for (int i = 0; i < arr.size(); i++) {
			// *arr.get(索引)获取出集合中的元素(e1,e2,e3),存储的是自定义类Goods_Attribute类型获取的也应该是Goods_Attribute类
			// *使用Goods_Attribute类型变量,接受get方法获取的元素
			Goods_Attribute g = arr.get(i);
			System.out.println(g.ID + "   " + g.name + "  " + g.price + "   "
					+ g.count);
			totalCount += g.count;
			totalMoney += g.price * g.count;
		}
		System.out.println("总库存数:" + totalCount);
		System.out.println("商品库存总金额:" + totalMoney);
	}

	/*
	 * 【3.2】定义方法: 添加新货物 返回值? 无 参数? 无
	 */
	public static void addGoods(ArrayList<Goods_Attribute> arr) {
		// (1)创建获取新对象e4---new自定义类为新对象开辟一块新的空间
		Goods_Attribute e4 = new Goods_Attribute();
		// (2)让用户键盘键盘键入 新对象/元素e4(商品编号" + "商品名称"+"商品单价" + "商品库存")
		Scanner sc = new Scanner(System.in);
		// (3)提示用户输入信息----这样就是在为集合中的元素e4的属性 赋值啦!
		System.out.println("请输入新货物的编号:");// ************************
		e4.ID = sc.nextInt();
		System.out.println("请输入新货物的名称:");// ************************
		e4.name = sc.next();
		System.out.println("请输入新货物的单价:");// ************************
		e4.price = sc.nextDouble();
		System.out.println("请输入新货物的库存:");// ************************
		e4.count = sc.nextInt();
		// (4)*向集合中添加新的元素e4
		arr.add(e4);
	}

	/*
	 * 【3.3】定义方法:删除货物 返回值? 无 参数? 无
	 */
	public static void delete(ArrayList<Goods_Attribute> arr) {
		System.out.println("请输入您删除的货物的编号:");
		// 键盘键入
		Scanner sc = new Scanner(System.in);
		int goodsID = sc.nextInt();
		// 删除集合元素
		for (int i = 0; i < arr.size(); i++) {
			Goods_Attribute w = arr.get(i);
			if (w.ID == goodsID) {
				// 集合方法 remove 移除集合中的元素
				arr.remove(w);
				// 给用户提示
				System.out.println("您删除货物成功");
				// return这样遇到第一个就是删除的就不再遍历后面,而且只结束方法
				return;
			}
		}
		System.out.println("对不起,没有这个编号的货物!");

	}

	/*
	 * 【4.4】定义方法:修改货物 返回值? 无 参数? 无
	 */
	public static void update(ArrayList<Goods_Attribute> arr) {
		System.out.println();
		System.out.println("请输入您要修改的货物的编号:");
		// 键盘键入
		Scanner sc = new Scanner(System.in);
		int goodsID = sc.nextInt();
		// 更新集合元素
		for (int i = 0; i < arr.size(); i++) {
			Goods_Attribute k = arr.get(i);
			// 下面k.ID==goodsID这个是判断是否有个编号,就说明是否有这个货物,---有,才可以修改
			if (k.ID == goodsID) {
				System.out.println("请输入新的货物的编号ID:         ");
				k.ID = sc.nextInt();
				System.out.println("请输入新的货物的名称name:    ");
				k.name = "口香糖";
				System.out.println("请输入新的货物的单价price: ");
				k.price = 3;
				System.out.println("请输入新的货物的数量count: ");
				k.count = 10;
				// 给用户提示
				System.out.println("恭喜您,修改货物成功!");
			}
		}
	}

	/*
	 * 【3.5】定义方法:退出系统
	 */
	public static void exit() {
		System.out.println("---------您已退出系统----------");
	}

}

运行 1 的效果图:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值