用 ArrayList 集合调用商品类

时间:2023-08-05 19:02:50

public class  Commodity{   //定义商品类

   String name;  //定义商品名字

   double  size;    //定义商品尺寸

   double  price;  //定义商品价格

   int  count;   // 定义商品总数量;

}

import java.util.*;

public class CommodityTest{

  public static void main(String[]args){

  

  

     //创建对象

    Commodity C=new Commodity();

    Commodity C1=new Commodity();

    Commodity C2=new Commodity();

  

      //创建Arraylist 集合

    ArrayList<Commodity> arr=new Arr<Commodity>();

    C.name="小米";

    C.size=15.6;

    C.price=9999;

    C.count=10;

    

      //为对象赋值

    C1.name="华为";

    C1.size=15.6;

    C1.price=9998;

    C1.count=100;

    C2.name="联想";

    C2.size=15.6;

    C2.price=9998;

    C2.count=100;

    //打印商品的属性

    System.out,.println("商品名称             商品尺寸           商品价格             商品数量");

    

      

    //便利ArrayLis集合打印输出

    for(i=0;i<arr.size();i++){

      System.out.print(arr,get(i).name+"           ");

      System.out.print(arr.get(i).Size+"             ");

      System.out.print(arr.get(i).price+"              ");

      System.out.println(arr.get(i).count+"             ");

}

  

}

}

用 ArrayList  集合调用商品类