继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java第二季:答答租车系统实现

ajjrx丶
关注TA
已关注
手记 2
粉丝 2
获赞 13
package com.imooc2_6_1;
//基类car
public class car {
    private String carName;// 车名
    private double burden;// 载货量
    private int mannedVolume;// 载人量
    private int price;// 价格/日

    public car(String carName, double burden, int mannedVolume, int price) {
        this.carName = carName;
        this.burden = burden;
        this.mannedVolume = mannedVolume;
        this.price = price;
        System.out.println("carName:" + carName + ' ' + "burden:" + burden + "kg" + ' ' + "mannedVolume:" + mannedVolume + "人" + ' ' + "price:" + price + "元" + '\n');
    }
}
package com.imooc2_6_1;
//载货车类
public class Truck extends car {
    public Truck(String carName, double burden, int mannedVolume, int price) {
        super(carName, burden, mannedVolume, price);
    }
}
package com.imooc2_6_1;
//载人车
public class passengerCar extends car {
    public passengerCar(String carName, double burden, int mannedVolume, int price) {
        super(carName, burden, mannedVolume, price);
    }
}
package com.imooc2_6_1;
//两用车
public class pickUp extends car{
    public pickUp(String carName, double burden, int mannedVolume, int price) {
        super(carName, burden, mannedVolume, price);
    }
}
package com.imooc2_6_1;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome!");
        System.out.println("是否租车:1是, 0否");
        Scanner choice = new Scanner(System.in);
        int q = choice.nextInt();
        if(q == 1) {
            System.out.println("您可租车的类型及价目表:");
            car[] type = {new Truck("0货车", 1000.0, 2, 3000), 
                        new passengerCar("1客车", 0.0, 18, 2000), 
                        new pickUp("2两用车", 200.0, 4, 1000)};
            System.out.println("请选择需要几种车辆类型");
            int x = choice.nextInt();
            int[] n = new int[x];
            int[] m = new int[x];
            for(int i = 0; i < x; ++i) {
                System.out.println("请选择需要的车辆类型及其数量:");
                n[i] = choice.nextInt();
                m[i] = choice.nextInt();
            }
            int sum = 0;
            for(int i = 0; i < x; ++i) {
                if(n[i] == 0) {
                    sum += (3000 * m[i]);
                }
                else if(n[i] == 1) {
                    sum += (2000 * m[i]);
                } else {
                    sum += (1000 * m[i]);
                }
            }
            System.out.println("总共需支付金额" + sum + "元, 请支付!");
        }
        System.out.println("谢谢使用!");
    }
}

欢迎大家指出错误与不足 谢谢~

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP

热门评论

用类的构造方法编的,虽然行,但我感觉不合理,个人观点,勿怪。

查看全部评论