package test2;
import java.sql.Array;
public class test21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
sortCosmeticManger cos=new sortCosmeticManger();
cos.addcosmetic(new cosmetic("香奈儿","进口",10000));
cos.addcosmetic(new cosmetic("YSL","进口",8000));
cos.addcosmetic(new cosmetic("大宝","国产",9000));
cos.addcosmetic(new cosmetic("dior","国产",900));
cos.print();
}
}
class sortCosmeticManger extends cosmeticManger{
public void print()
{
cosmetic [] temp=java.util.Arrays.copyOf(cs, count);//拷贝父类数组到新数组;count是继承自父类的,可以直接拿过来用
cosmetic c=null;
for(int i=0;i<temp.length;i++)//冒泡
{
for(int j=0;j<temp.length-1;j++)
{
if(temp[j].getPrice()>temp[j+1].getPrice())
{
c=temp[j];
temp[j]=temp[j+1];
temp[j+1]=c;
}
}
}
for(cosmetic cosmetic11:temp)//foreach循环,cosmetic11是名称,cosmetic是类型,temp是数组名。
{
System.out.println(cosmetic11.getInfo());
}
}
}
class cosmeticManger{
protected cosmetic[] cs=new cosmetic[4];
protected int count;
public void addcosmetic(cosmetic c) {
int size=cs.length;
if(size<=count)
{
int newlen=size*2;
cs=java.util.Arrays.copyOf(cs, newlen);
}
cs[count]=c;
count++;
}
public void printInfo() {
for(int i=0;i<count;i++)
{
System.out.println(cs[i].getInfo());
}
}
}
class cosmetic{
public cosmetic() {}
public cosmetic(String name,String type,int price) {
this.name=name;
this.typeString=type;
this.price=price;
}
public String getInfo() {
return "name="+name+"type="+typeString+"price="+price;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getTypeString() {
return typeString;
}
public void setTypeString(String typeString) {
this.typeString = typeString;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int price;
private String typeString;
private String name;
}