商品类
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();
}
}