商品的CRUD(增加删除修改查找)

商品类


package com.softeem.oop1;

/**
 * 
 * 商品类
 */
public class Product {

	private int id;			//id
	private String pname;		//名称
	private String type;		//类型
	private double price;		//价格
	private int count;		//库存量	
	
	public Product() {
		
	}
	
	public Product(int id, String pname, String type, double price, int count) {
		super();
		this.id = id;
		this.pname = pname;
		this.type = type;
		this.price = price;
		this.count = count;
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	
}

商品的管理(CRUD)


package com.softeem.oop1;

import java.util.ArrayList;

import com.softeem.oop1.MyArrayList;

public class ProductManage {

	private MyArrayList db = new MyArrayList();//自定义动态数组
	private int index;//索引
	
	/**
	 * 添加商品
	 */
	public void add(Product p){
		db.add(p);
	}
	
	/**
	 * 查询所有商品
	 */
	public void findAll(){
		for(int i = 0;i<db.size();i++)
		{
			System.out.println(db.get(i).getId()+"----"+db.get(i).getPname()+"----"+db.get(i).getPrice());
		}
	}
	
	/**
	 * 删除
	 */
	public void delete(int id)
	{
		int del = -1;
		for(int i = 0;i<db.size();i++)
		{
			if(id == db.get(i).getId())
			{
				del = i;//记录需要被删除的项
				break;
			}
		}
		db.remove(del);//删除指定项
	}
	
	/**
	 * 修改
	 * @param id
	 * @param price
	 */
	public void update(int id,double price)
	{
		for(int i = 0;i<db.size();i++)
		{
			if(id == db.get(i).getId())
			{
				db.get(i).setPrice(price);
				break;
			}
		}
	}
	
	public static void main(String[] args) {
		ProductManage pm = new ProductManage();
		Product p1 = new Product(1, "小米自行车", "户外", 99, 100);
		Product p2 = new Product(2, "小米大西瓜", "水果", 19, 1000);
		Product p3 = new Product(3, "旺仔牛奶", "饮料", 5, 1010);
		pm.add(p1);
		pm.add(p2);
		pm.add(p3);
//		pm.delete(2);
//		pm.update(1, 199);
//		pm.findAll();
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值