ArrayList创建集合添加对象并遍历
ArrayList<Product> array= new ArrayList<Product>();
Product s1 = new Product("1.遥控飞机", 168);
Product s2 = new Product("2.变形金刚", 999);
Product s3 = new Product("3.芭比娃娃", 136);
Product s4 = new Product("4.玩具手枪", 129);
Product s5 = new Product("5.趣味拼图", 99);
Product s6 = new Product("6.玩具积木", 100);
public Shopping() {
}
public void shops(){//显示所有的商品
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
array.add(s6);
System.out.println("-----------东京玩具汇总-----------");
for (int x = 0; x < array.size(); x++) {
Product s = (Product) array.get(x);
System.out.println("\t"+s.getProduct() + "\t\t" + s.getPrice());
}
}
调对象:
Product s = (Product) array.get(x);
System.out.println("\t"+s.getProduct() + "\t\t" + s.getPrice());
跨包调方法:
方法名 s = new 方法名();
s.方法名;
模拟购物车删除商品的代码:
do{
System.out.println("请输入要删除的商品");
int cproduct =sc.nextInt();
if(count[cproduct-1]!=0){
System.out.println("请输入要删除的数量");
int ccount =sc.nextInt();
reduce(cproduct, ccount);
if(count[cproduct-1]==0){
product[cproduct-1]=null;
}else{
System.out.println("成功删除"+ccount+"件"+product[cproduct-1]);
}
}else{
System.out.println("该商品不在您的购物车");
}
System.out.println("是否继续删除:请输入y/n");
cdelet=sc.next();
}while(cdelet.equals("y"));