m0_73740280 2022-11-18 23:30 采纳率: 86.8%
浏览 2
已结题

请问可以帮我看看这个接口的代码怎么完善吗

问题遇到的现象和发生背景

img


第二张图片是输出格式样例

用代码块功能插入代码,请勿粘贴截图
运行结果及报错内容
package Demo;
interface CarRent{
    int day=0;
    void carRent(int carfee, int day);
}
interface truckRent extends CarRent{
    int day=0;
     void truckRent(double ton,int day);
}
class Rent implements truckRent{
    public String number;
    public String brand;
    public int day;
    public int carfee;
    public int dailyfee;
    public double ton;
    public double truckfee;
    public double fee;
    public String client;
    Rent(String number,String brand){
        this.number=number;
        this.brand=brand;
    }
    @Override
    public void carRent(int carfee,int day) {
        // TODO Auto-generated method stub
        carfee=day*dailyfee;
    }
    @Override
    public void truckRent(double ton,int day) {
        // TODO Auto-generated method stub
        truckfee=day*dailyfee*ton;
        fee=carfee+truckfee;
    }
    public  String info() {
        String info=this.number+this.brand;
        return info;
    }
    public String clientinfo(String client) {
        String clientinfo = client;
        return clientinfo;
    }
    public int dayinfo(int day) {
        int dayinfo=day;
        return dayinfo;
    }
    public double getfee(int carfee,double truckfee) {
        fee=truckfee+carfee;
        return fee;
    }
}

public class App_4 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CarRent[] cr=new CarRent[6];
        cr[0]=new Rent("京A123","本田CRV");
        cr[1]=new Rent("京B123","福特");
        cr[2]=new Rent("京C123","本田HRV");
        cr[3]=new Rent("京D123","小客车");
        cr[4]=new Rent("京E123","大客车");
        cr[5]=new Rent("京F123","卡车");
        for(CarRent c:cr)
            System.out.println(((Rent) c).info());
        Rent c=null;
        System.out.print("客户名:"+c.clientinfo("沈伟")+"天数:"+c.dayinfo(1));
        
        
        

    }

}


我的解答思路和尝试过的方法

我想的是写一个汽车父接口,有轿车和客车;再加一个卡车的子接口;然后实现这两个接口。
输出的时候用数组输出车牌号和型号,但是我这样参数介绍车牌号和型号,
1.不知道应该在哪里给租金赋值了,
2.构造方法应该写几个参数呢
3.像客户,天数这两个参数应该在哪赋值呢

  • 写回答

1条回答 默认 最新

  • nssnail 2022-11-19 00:56
    关注
    
    public class Test2 {
    
    
        public static void main(String[] args) {
            List<Car> list = new ArrayList<>();
            list.add(new BenTian("粤A123465", 5));
            list.add(new BenTian("粤A123477", 5));
            list.add(new FuTe("京A123465", 4));
            list.add(new KeChe("湘A155555", 5, false));
            list.add(new KeChe("湘A150000", 2, true));
            list.add(new KaChe("粤B123465", 5, 50));
            list.add(new KaChe("粤C123465", 5, 70));
            User user=new User("张三",list);
            user.costAll();
    
        }
    
    
    }
    
    class User {
        private String name;//姓名
        private List<Car> cars;//租赁汽车集合
    
        public User(String name, List<Car> cars) {
            this.name = name;
            this.cars = cars;
        }
    
        /**
         * 计算价格
         *
         * @param list 租赁汽车集合
         * @param day  租赁天数
         * @return
         */
        public int costAll() {
            int price = 0;
            for (Car car : cars) {
                price += car.cost();
            }
            System.out.println("客户名:"+name+",租赁费用:"+price+"元");
            return price;
        }
    }
    
    //汽车抽象类
    abstract class Car {
    
        //车牌号
        private String number;
    
        private String name;
    
        //租赁天数
        private int day;
    
        public Car(String number, int day) {
            this.number = number;
            this.day = day;
        }
    
        //获取日单价
        abstract int getPrice();
    
        protected void setName(String name) {
            this.name = name;
        }
    
        int cost() {
            int  total=getPrice() * day;
            String log = "车牌号:"+number+",汽车类型:"+name+",日单价:"+getPrice()+"元,租赁天数:"+day+",总价:"+total+"元";
            System.out.println(log);
            return total;
        }
    
    
    }
    
    //本田
    class BenTian extends Car {
    
        //日单价
        private static final int UNIT_PRICE = 500;
    
        public BenTian(String number, int day) {
            super(number, day);
            this.setName("本田");
    
        }
    
    
        @Override
        int getPrice() {
            return UNIT_PRICE;
        }
    }
    
    //福特
    class FuTe extends Car {
    
        //日单价
        private static final int UNIT_PRICE = 600;
    
        public FuTe(String number, int day) {
            super(number, day);
            this.setName("福特");
        }
    
    
        @Override
        int getPrice() {
            return UNIT_PRICE;
        }
    }
    
    
    //客车
    class KeChe extends Car {
    
        //是否小于16座
        private boolean isLt16;
        //小于16座日单价
        private static final int LT_16_UNIT_PRICE = 800;
    
        //大于16座日单价
        private static final int GT_16_UNIT_PRICE = 1000;
    
        public KeChe(String number, int day, boolean isLt16) {
            super(number, day);
            this.isLt16 = isLt16;
            if (isLt16) {
                this.setName("客车(小于16座)");
            } else {
                this.setName("客车(大于等于16座)");
            }
        }
    
        @Override
        int getPrice() {
            return isLt16 ? LT_16_UNIT_PRICE : GT_16_UNIT_PRICE;
        }
    }
    
    
    //卡车
    class KaChe extends Car {
    
        //吨
        private int ton;
    
        //日单价
        private static final int UNIT_PRICE = 50;
    
        public KaChe(String number, int day, int ton) {
            super(number, day);
            this.ton = ton;
            this.setName("客车(" + ton + "吨)");
        }
    
        @Override
        int getPrice() {
            return ton * UNIT_PRICE;
        }
    }
    

    img

    简单写了下,可优化,不一定要用这种方式

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月7日
  • 已采纳回答 1月30日
  • 创建了问题 11月18日

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图