需求:
1.列出生活用品的属性(编号,名称,价格,卖了多少件)
2.求出销售总额
3.求出销售最高的商品`
package exe_ListDemo;
public class exe_product {
public String pId; //商品编号
public String pName; //商品名称
public float pPrice; //商品价格
public int Number; //商品数量
//含参构造
public exe_product(String pId,String pName,float string,int Number) {
this.pId = pId;
this.pName = pName;
this.Number = Number;
this.pPrice = string;
}
public String toString() {
String tmp = this.pId+","+this.pName+","+this.pPrice+","+this.Number;
return tmp;
}
//无参构造,个人觉得用含参构造方法简单
public exe_product() {
}
}
`
package exe_ListDemo;
import java.util.ArrayList;
public class exe_listDemo {
public static void main(String[] args) {
exe_product p1 = new exe_product("1", "牙刷", 10.5f, 20);
exe_product p2 = new exe_product("2", "电脑", 5000.0f, 3);
exe_product p3 = new exe_product("3", "洗衣机", 3000.0f, 10);
exe_product p4 = new exe_product("4", "毛巾", 20.0f, 8);
//new一个集合来装product的信息
ArrayList<exe_product> ps = new ArrayList<exe_product>();
//用add方法将这些商品信息放入集合中
ps.add(p1);
ps.add(p2);
ps.add(p3);
ps.add(p4);
System.out.println(ps);
//计算商品的总金额
float sum = 0;
for(int i = 0;i<ps.size();i++) {
sum += ps.get(i).pPrice * ps.get(i).Number;
}
System.out.println("商品总金额是:"+sum);
//计算销售总额最高的商品
exe_product tmp= ps.get(0);
for(int i = 0;i < ps.size();i++) {
if(tmp.pPrice * tmp.Number < ps.get(i).pPrice * ps.get(i).Number) {
tmp = ps.get(i);
}
}
System.out.println("销售总额最高的商品是:"+tmp);
}
}