商品管理系统 -------更新

package com.ambow.product;

import java.util.Scanner;
/*

  • 30%注释

*/
public class ProductManager {
public static void main(String[] args) {

	//定义容器

// int[] arr = new int[45];
Product[] prtArr = new Product[100];
//计数
int index = 0;

	while(true) {
		System.out.println("***************************");
		System.out.println("  *****  商品管理系统      *****  ");
		System.out.println("  *****  1.添加商品        *****  ");
		System.out.println("  *****  2.查询商品        *****  ");
		System.out.println("  *****  3.修改商品        *****  ");
		System.out.println("  *****  4.删除商品        *****  ");
		System.out.println("  *****  0.退出系统        *****  ");
		System.out.println("***************************");
		System.out.println("请选择操作序号:");
		
		Scanner input = new Scanner(System.in);
		int choose = input.nextInt();
		
		if (choose == 1) {
			System.out.println("请输入商品名称:");
			String name = input.next();
			System.out.println("请输入商品价格:");
			float price = input.nextFloat();
			System.out.println("请输入商品制造商:");
			String factory = input.next();
			System.out.println("请输入商品生产日期:");
			String date = input.next();
			
			//把上面的4个属性,构建商品对象
			Product product = new Product();
			product.name = name;
			product.price = price;
			product.factory = factory;
			product.date = date;
			
			//把商品对象,放入容器(数组)
			/*
			prtArr[index] = product;
			index++;
			*/
			for (int i = 0; i < prtArr.length; i++) {
				if (prtArr[i] == null) {//遍历数组,找到第一个空位置
					prtArr[i] = product;//把要添加的商品,放入这个位置
					index++;
					break;
				}
			}
			
			System.out.println("---商品添加成功!---");
			
		}else if (choose == 2) {
			//遍历数组
			if (index == 0) {
				System.out.println("暂无商品信息!");
			}else {
				for(int i = 0;i < prtArr.length;i++) {
					if (prtArr[i] != null) {
						System.out.println("商品名称:" + prtArr[i].name + " 商品价格:" + prtArr[i].price 
								+ " 制造商:" + prtArr[i].factory +" 生产日期:" + prtArr[i].date);
					}
				}
			}
			
		}else if (choose == 3) {
			//修改商品
			System.out.println("请输入要修改的商品的名称:");
			String modifyName = input.next();
			
			int modifyIndex = -1;
			//定位要修改商品在数组中的位置(下标)
			for (int i = 0; i < index; i++) {
				if (prtArr[i].name.equals(modifyName)) {
					modifyIndex = i;
				}
			}
			if (modifyIndex == -1) {
				//不存在
				System.out.println("您要修改的商品不存在!");
			}else {
				//存在
				System.out.println("请输入新的商品名称:");
				String name = input.next();
				System.out.println("请输入新的商品价格:");
				float price = input.nextFloat();
				System.out.println("请输入新的商品制造商:");
				String factory = input.next();
				System.out.println("请输入新的商品生产日期:");
				String date = input.next();
				//构建新的商品对象
				Product product = new Product();
				product.name = name;
				product.price = price;
				product.factory = factory;
				product.date = date;
				
				prtArr[modifyIndex] = product;//用新的商品对象,覆盖原来位置上的商品
				System.out.println("商品修改成功!");
			}
		}else if (choose == 4) {
			//删除商品
			System.out.println("请输入要删除的商品名称:");
			String dropName = input.next();
			
			int dropIndex = -1;
			//定位要删除商品在数组中的位置(下标)
			for (int i = 0; i < index; i++) {
				if (prtArr[i].name.equals(dropName)) {
					dropIndex = i;
					index--;
				}
			}
			
			if (dropIndex == -1) {
				System.out.println("您要删除的商品不存在!");
			}else {
				prtArr[dropIndex] = null;
				System.out.println("商品删除成功!");
			}
			
		}else if (choose == 0) {
			System.out.println("系统退出!");
			//break;//退出循环
			System.exit(0);//系统退出
		}
	}
	
}

}

发布了33 篇原创文章 · 获赞 4 · 访问量 6029

猜你喜欢

转载自blog.csdn.net/amspony/article/details/88943991