管理化妆品的小程序

 继承运用的题目 。

import java.util.Arrays;
public class HelloIDEA {
    public static void main(String[] args) {
        jiage cs=new jiage();//调用不同类使用不同的方法
        cs.add(new cosmetic("0数据结构","进口",1999));
        cs.add(new cosmetic("1数据结构","国产",999));
        cs.add(new cosmetic("2数据结构","进口",3999));
        cs.add(new cosmetic("3数据结构","进口",6999));
        cs.add(new cosmetic("4数据结构","国产",5999));
        cs.print1();
        System.out.println("-------------------------------------------------------------------");
        cs.print1();
        System.out.println("---------------------------------------------------------------");
        cs.print1();

    }
}
//进口化妆品的输出
class jiage extends cosmeticManager{//通过继承筛选
    public void print1() {
        cosmetic[] llo = null;
        llo = Arrays.copyOf(cs, s);
        System.out.println("进口的有");
        for (int i = 0; i < s; i++) {
            if ("进口" ==llo[i].getType()){

                System.out.println(llo[i].print());
            }
        }
    }



}
//价格排序
class paixv extends cosmeticManager{//继承排序
    @Override
    public void print1() {
     cosmetic[] llo=null;
     llo=Arrays.copyOf(cs,s);
     cosmetic tecp;
    for (int i=0;i<s;i++){
        for (int j=0;j<s-1;j++){
            if (llo[j].getPrice()>llo[j+1].getPrice()){
            tecp=llo[j];
            llo[j]=llo[j+1];
            llo[j+1]=tecp;
            }
        }
    }
    for (cosmetic z:llo){
        System.out.println(z.print());
    }
    }
}


class cosmeticManager{
    protected cosmetic[] cs=new cosmetic[5];
    protected int s=0;
    public cosmeticManager(){}
    public cosmeticManager(int ccl){
        if (ccl>0) {
            cs = new cosmetic[ccl];
        }else {
            cs=new cosmetic[3];
        }
    }
    public void add(cosmetic llo){
        if (s> cs.length){
            s= cs.length*3/2+1;
        }
        cs[s]=llo;
        s++;
    }
    public void print1(){
        for (int i=0;i<s;i++){
            System.out.println(cs[i].print());
        }
    }

}




//化妆品类
class cosmetic{
    private String name;
    private String type;
    private int price;
    public cosmetic(){}
    public cosmetic(String name,String type,int price){
        this.name=name;
        this.type=type;
        this.price=price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
    public String print(){

        return "产品名称:"+name+"  生产地方:"+type+" 价格:"+price;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值