import java.util.Random;
class commodity{ //商品private String type,size;public commodity(String s) { type=s;}public String getType() { return type; }public String getSize() { return size; }public double price() { return 0; }public void addNumber(int n) { ;}public void deleteNumber() { ;}public int getNumber() { return 0;}
}
class Usb extends commodity{private double p=100; private String size ="Inspire T10";public static int number;public void addNumber(int n) { number+=n;}public void deleteNumber() { number--;}public String getSize() { return size; }public int getNumber() { return number;}public Usb() { super("U盘"); }public double price() { return p; }public String toString() { return getType()+"\t"+getSize()+"\t"+getNumber()+"\t"+price()+"\t"+price()*getNumber(); }public static Usb Create() { return new Usb(); }
}
class Screen extends commodity{private double p=2000;private String size ="Inspire T11";public static int number;public void addNumber(int n) { number+=n;}public void deleteNumber() { number--;}public int getNumber() { return number;}public String getSize() { return size; }public Screen() { super("显示器"); }public double price() { return p; }public String toString() { return getType()+"\t"+getSize()+"\t"+getNumber()+"\t"+price()+"\t"+price()*getNumber(); }public static Screen Create() { return new Screen(); }
}
class Music extends commodity{private double p=300;private String size ="Inspire T12";public static int number;public void addNumber(int n) { number+=n;}public void deleteNumber() { number--;}public int getNumber() { return number;}public String getSize() { return size; }public Music() { super("一对音箱"); }public double price() { return p; }public String toString() { return getType()+"\t"+getSize()+"\t"+getNumber()+"\t"+price()+"\t"+price()*getNumber(); }public static Music Create() { return new Music(); }
}
class shoppingCart{private commodity[] commodities;private int totalnum;//商品总数量private int cartlen=30; //购物车最大容量public shoppingCart () {commodities = new commodity[cartlen];}public void filtrate() {//过滤掉重复的commodity[] com=new commodity[cartlen];com[0]=commodities[0];int i,j,k; i=0;j=0;while(i<totalnum) {k=0; i++;while(k<=j) {if(com[k].getType()==commodities[i].getType()){ i++; break;}else k++;}} }public commodity[] Filtrate() {commodity[] coms = new commodity[20];int comsTail = 0;int i=0;boolean isAdd = false;while(i<totalnum) {int j=0;while(j<comsTail) {if(commodities[i].getType()==coms[j].getType()) {isAdd = false;break;}isAdd = true;j++;}if(comsTail==0) {isAdd=true;}if(isAdd) {coms[comsTail] = commodities[i];comsTail++;}i++;}return coms;}public void add(commodity c,int n) {//增加if(totalnum+n > cartlen){System.out.print("购物车已满");return ;}for(int i=totalnum;i<totalnum+n;i++)commodities[i]=c;totalnum=totalnum+n;c.addNumber(n);}public void delete(commodity c) {//删去if(find(c)==-1) {System.out.print("购物车中没有这个商品!");return ;}for(int i =find(c);i<totalnum-1;i++) {commodities[i]=commodities[i+1];}totalnum--;c.deleteNumber();}public int find(commodity c) {for(int i=0;i<totalnum;i++) {if(commodities[i].getType()==c.getType()) //不能用对象直接去比较,要比较对象的属性return i;}return -1; }public void show() {commodity[] c = Filtrate();for(int i=0; i<c.length; i++){if(c[i]==null) continue;System.out.println(c[i]);}double s=0;for(int i=0; i<totalnum; i++){if(c[i]==null) continue;s=s+c[i].price()*c[i].getNumber();}System.out.println("总金额="+s);}public void printBill(String buyer) {Random random = new Random();int orderNum = random.nextInt(999999);System.out.print("发票抬头:"+buyer+"\n");System.out.print("订单号:"+orderNum+"\n");System.out.print("货物\t型号\t\t数量\t单价\t价格"+"\n");}
}
class App3{public static void main(String[] agrs) {shoppingCart sc =new shoppingCart();sc.add(Usb.Create(),1);sc.add(Screen.Create(), 1);sc.add(Music.Create(), 1);sc.printBill("小可爱");sc.show();//System.out.println("总金额="+sc.sum());System.out.println("增加2个U盘:");sc.add(Usb.Create(),2);//sc.printBill("小可爱");sc.show();//System.out.println("总金额="+sc.sum());System.out.println("删除1个U盘,1个音响:");sc.delete(Music.Create());sc.delete(Usb.Create());//sc.printBill("小可爱");sc.show();}}
运行结果: