java:商品管理系统

目录

分析:

 源代码:


分析:

 

 

 源代码:

package j118;
/**
 * 商品类
 * @author 惊蛰
 *
 */
public class products {
		private int id;//编号
		private int stock;//库存
		private String name;//名称
		private String category;//种类
		private String description;//描述
		private double price;//价格
		private int state;//状态
		
		public products() {
			super();
		}
/**
 * 有参的构造方法,用于初始化
 * @param id
 * @param stock
 * @param name
 * @param category
 * @param description
 * @param price
 * @param state
 */
		public products(int id, int stock, String name, String category, String description, double price, int state) {
			super();
			this.id = id;
			this.stock = stock;
			this.name = name;
			this.category = category;
			this.description = description;
			this.price = price;
			this.state = state;
		}
/**
 * 获取编号
 * @return
 */
		public int getId() {
			return id;
		}
/**
 * 设置编号
 * @param id
 */
		public void setId(int id) {
			this.id = id;
		}
/**
 * 获取库存
 * @return
 */
		public int getStock() {
			return stock;
		}
/**
 * 设置库存
 * @param stock
 */
		public void setStock(int stock) {
			this.stock = stock;
		}
/**
 * 获取商品名
 * @return
 */
		public String getName() {
			return name;
		}
/**
 * 设置商品名
 * @param name
 */
		public void setName(String name) {
			this.name = name;
		}
/**
 * 获取商品种类
 * @return
 */
		public String getCategory() {
			return category;
		}
/**
 * 设置商品种类
 * @param category
 */
		public void setCategory(String category) {
			this.category = category;
		}
/**
 * 获取商品描述
 * @return
 */
		public String getDescription() {
			return description;
		}
/**
 * 设置商品描述
 * @param description
 */
		public void setDescription(String description) {
			this.description = description;
		}
/**
 * 获取商品价格
 * @return
 */
		public double getPrice() {
			return price;
		}
/**
 * 设置商品价格
 * @param price
 */
		public void setPrice(double price) {
			this.price = price;
		}
/**
 * 获取商品状态
 * @return
 */
		public int getState() {
			return state;
		}
/**
 * 设置商品状态
 * @param state
 */
		public void setState(int state) {
			this.state = state;
		}
/**
 * 打印全部商品信息
 */
		@Override
		public String toString() {
			return "products [id=" + id + ", stock=" + stock + ", name=" + name + ", category=" + category
					+ ", description=" + description + ", price=" + price + ", state=" + state + "]";
		}
		
		
}
package j118;

import java.util.Scanner;

/**
 * 商品管理类
 * @author 惊蛰
 *
 */
public class ProductsManage {
	public static products product[]=new products[100];
	public static int maxIndex=0;
	public static Scanner reader=new Scanner(System.in);
	public static void main(String[] args) {
		System.out.println("1.增加 2.删除 3.修改 4.查询 5.打印全部商品 6.退出");
		System.out.println("请选择");
		int select=reader.nextInt();
		while(true) {
		switch(select) {
		case 1: save();break;
		case 2: delete();break;
		case 3: update();break;
		case 4: query();break;
		case 5: printAll();break;
		case 6: System.exit(0);
		default: System.out.println("请输入正确操作");
		}
		System.out.println("1.增加 2.删除 3.修改 4.查询 5.打印全部商品 6.退出");
		System.out.println("请选择");
		select=reader.nextInt();
		}
	}
		
/**
 * 打印全部正常的商品
 */
	private static void printAll() {
		System.out.println("全部的商品信息为:");
		for(int i=0;i<maxIndex;i++) {
			if(product[i].getState()==1) {
				System.out.println(product[i]);
			}
		}
	}
/**
 * 查询
 */
	private static void query() {
		System.out.println("请选择查找方式");
		System.out.println("1.按商品名查询 2.按商品类型查询");
		int select=reader.nextInt();
		int flag=0;
		if(select==1) {
			//按商品名查询
			System.out.println("请输入名称关键字");
			String s1=reader.next();
			for(int i=0;i<maxIndex;i++) {
				if(product[i].getName().indexOf(s1)!=-1&&product[i].getState()==1){
					flag=1;
					System.out.println(product[i]);
				}
			}
		}
		else if(select==2) {
			//按商品类型查询
			System.out.println("请输入类别关键字");
			String s1=reader.next();
			for(int i=0;i<maxIndex;i++) {
				if(product[i].getCategory().indexOf(s1)!=-1&&product[i].getState()==1){
					flag=1;
					System.out.println(product[i]);
				}
			}
		}
		else {
			System.out.println("请选择正确的查找方式");
		}
		if(flag==0) {
			System.out.println("未找到");
		}
	}
/**
 * 修改
 */
	private static void update() {
		System.out.println("请输入要修改商品的编号");
		int id=reader.nextInt();
		int flag=0;
		for(int i=0;i<maxIndex;i++) {
			if(product[i].getId()==id&&product[i].getState()==1) {
				flag=1;
				System.out.println("请重新输入商品信息");
				System.out.println("名称");
				String name=reader.next();
				System.out.println("类别");
				String category=reader.next();
				System.out.println("价格");
				double price=reader.nextDouble();
				System.out.println("库存");
				int stock=reader.nextInt();
				System.out.println("描述");
				String description=reader.next();
				//组合对象
				products p=new products(id,stock,name,category,description,price,1);
				//存入数组
				product[i]=p;
				System.out.println("修改完成");
				System.out.println(product[i]);
				break;
			}
		}
		if(flag==0) {
			System.out.println("您输入了无效的商品编号");
		}
		
	}
/**
 * 删除
 */
	private static void delete() {
		System.out.println("请输入要删除对象的编号");
		int id=reader.nextInt();
		int flag=0;
		for(int i=0;i<maxIndex;i++) {
			if(product[i].getId()==id&&product[i].getState()==1) {
				flag=1;
				System.out.println(product[i]);
				System.out.println("您是否要删除(1是,0否)");
				int select=reader.nextInt();
				if(select==1) {
					product[i].setState(0);
					System.out.println("删除完成");
					break;
				}
				else if(select==2){
					System.out.println("取消删除");
					break;
				}
				else {
					System.out.println("请输入正确的操作");
					break;
				}
			}
		}
		if(flag==0) {
			System.out.println("您输入了无效的编号");
		}
		
	}
/**
 * 增加
 */
	private static void save() {
		//生成编号
		int id=crateNo();
		System.out.println("名称");
		String name=reader.next();
		System.out.println("类别");
		String category=reader.next();
		System.out.println("价格");
		double price=reader.nextDouble();
		System.out.println("库存");
		int stock=reader.nextInt();
		System.out.println("描述");
		String description=reader.next();
		
		//组合对象
		products p=new products(id,stock,name,category,description,price,1);
		//存入数组
		product[maxIndex]=p;
		maxIndex++;
	}
	/**
	 * 自动生成编号
	 */
	public static int  crateNo() {
		if(maxIndex==0) {
			return 1;
		}
		else {
			//上一个商品的编号-1
			return product[maxIndex-1].getId()+1;
		}
	}
	/**
	 * 根据商品编号查找
	 * @param id 商品编号
	 * @return 商品在数组中的引用,如果找到,返回数组编号,找不到返回-1
	 */
	public static int findIndex(int id) {
		int index=-1;
		for(int i=0;i<=maxIndex;i++) {
			if(id==product[i].getId()) {
				index=i;
				break;
			}
		}
		return index;
	}
	/**
	 * 打印指定索引的商品
	 * @param index 指定的索引
	 */
	public static void printProducts(int index) {
		System.out.println(product[index]);
	}
	}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值