//化妆品管理应用(继承)
import java.util.Arrays;
public class Test2{
public static void main(String[] args){
/*
CosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f));
cm.add("new Cosmetic("大宝",10.0f));
cm.add("new Cosmetic("郁美净",8.0f));
cm.add("new Cosmetic("玉兰油",100.0f));
cm.printInfo();
*/
/*
CosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f));
cm.add("new Cosmetic("大宝",10.0f));
cm.add("new Cosmetic("郁美净",8.0f));
cm.add("new Cosmetic("玉兰油",100.0f));
cm.printInfo();
*/
ImportCosmeticManager cm = new CosmeticManager();
cm.add("new Cosmetic("香奈儿",800.0f,"进口"));
cm.add("new Cosmetic("大宝",10.0f,"国产"));
cm.add("new Cosmetic("郁美净",8.0f,"国产"));
cm.add("new Cosmetic("玉兰油",100.0f,"进口"));
cm.printInfo();
}
}
//只输出进口化妆品
class ImportCosmeticManager extends CosmeticManager{
public void printInfo(){
String type = null;
for(int i=0;i<super.count;i++){
super.cosmetics[i].getType();
if("进口".equals(type)){
System.out.println(cosmetics[i].getInfo());
}
}
}
}
//可按单价排序
class SortCosmeticManager extends CosmeticManager{
//重写了父类的打印方法,实现了排序功能
public void println(){
Cosmetic[] cs = new Cosmetic[super.count];
for(int i=0;i<cs.length;i++){
cs[i] = super.cosmetics[i];
}
Cosmetic temp=null;
//冒泡排序
for(int i =0;i<cs.length-1;i++){
for(int j=0;j<cs.length-1-i;j++){
if(cs[j].getPrice()>cs[j+1].getPrice()){
temp = cs[j];
cs[j] = cs[j+1];
cs[j+1] = temp;
}
}
}
for(int i=0;i<count;i++){
System.out.println()cs[i].getInfo();
}
}
}
//化妆品管理类
class CosmeticManager{
private Cosmetic[] cosmetics = new Cosmetic[3];
prot int count = 0;
//化妆品进货
public void add(Cosmetic c){
if(count>=cosmetics.length){
int newLen = cosmetics.length*3/2+1;
cosmetics = Arrays.copyOf(cosmetics,newLen);
}
cosmetics[count]=c;
count++;
}
//输出所有化妆品
public void printInfo(){
for(int i=0;i<cosmetics;i++){
System.out.println(cosmetics[i].getInfo());
}
}
}
//化妆品类
class Cosmetic{
private String name;
private float price;
private String type; //国产或进口
public Cosmetic(){};
publi Cosmetic(String name,float price,String type){
this.name = name;
this.price = price;
this.type = type;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setPrice(){
this.price;
}
public void setName(String type){
this.type= type;
}
public String getType(){
return Type;
}
public float getPrice(){
return price;
}
public void getInfo(){
return "化妆品名称:"+name+"单价为:"+price+"类型为"+type;
}
}