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

Java入门第二季第六章练习题

不知疲倦的旅者
关注TA
已关注
手记 2
粉丝 10
获赞 41
//父类
package imooc;

public class Car {
    public String getName() {
        return "";
    }

    public int getPerson() {
        return 0;
    }

    public int getTon() {
        return 0;
    }

    public int getMoney() {
        return 0;
    }
}
//子类皮卡车
package imooc;

public class Pickup extends Car {
    private String name;
    private int person;
    private int ton;
    private int money;

    public Pickup(String name, int person, int ton, int money){
        this.name = name;
        this.person = person;
        this.ton = ton;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public int getPerson() {
        return person;
    }

    public int getTon() {
        return ton;
    }

    public int getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return name + "  " + money +"元/天     " + "载人:" + person + " 载货:" + ton + "吨" ;
    }
}
//子类货车
package imooc;

public class Truck extends Car {
    private String name;
    private int ton;
    private int money;

    public Truck(String name, int ton ,int money){
        this.name = name;
        this.ton = ton;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public int getTon() {
        return ton;
    }

    public int getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return name +"    " + money + "元/天   " + " 载货:" + ton + "吨";
    }
}
//子类小轿车
package imooc;

public class Dedan extends Car {
    private String name;
    private int person;
    private int money;

    public Dedan(String name, int person, int money){
        this.name = name;
        this.person = person;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public int getPerson() {
        return person;
    }

    public int getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return name + "  " + money + "元/天    " + "载人: " + person + "人";
    }
}
//子类客车
package imooc;

public class Bus extends Car {
    private String name;
    private int person;
    private int money;

    public Bus(String name, int person, int money){
        this.name = name;
        this.person = person;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public int getPerson() {
        return person;
    }

    public int getMoney() {
        return money;
    }

    @Override
    public String toString() {
        return name + "           " + money + "元/天     " + "载人:" + person + "人";
    }
}
//主函数
package imooc;

import java.util.Scanner;

public class Initial {

    public static void main(String[] args) {
        Car[] cars = {new Dedan("奥迪A4", 4, 500), new Dedan("马自达6", 4, 400), new Pickup("皮卡雪6", 4, 2, 450), new Bus("金龙", 20, 800),
                new Truck("松花江", 4, 400), new Truck("依维柯", 20, 1000)};
        while(true){
            System.out.println("欢迎使用答答租车系统:\n" + "您是否要租车:1是  0否");
            Scanner in = new Scanner(System.in);
            int i = in.nextInt();
            if(i == 1){
                System.out.println("您可租车的类型及其价目表:\n" + "序号  汽车名称     租金                  容量");
                System.out.println("1. " + cars[0].toString());
                System.out.println("2. " + cars[1].toString());
                System.out.println("3. " + cars[2].toString());
                System.out.println("4. " + cars[3].toString());
                System.out.println("5. " + cars[4].toString());
                System.out.println("6. " + cars[5].toString());

                System.out.println("请输入您要租的汽车的数量");
                i = in.nextInt();     //i是租汽车的数量
                Car[] currentCars = new Car[i];
                for(int k = 1; k <= i; k++){
                    System.out.println("请输入第" + k + "辆车的序号");
                    int j = in.nextInt();
                    currentCars[k-1] = cars[j-1];
                }       

                System.out.println("请输入租车天数:");
                int day = in.nextInt(); //day是租车的天数

                System.out.println("您的账单:");
                System.out.println("***可载人的车有:");
                int nPerson = 0;
                int nTon = 0;
                int nMoney = 0;
                for(Car car: currentCars){
                    if(car instanceof Dedan){
                        nPerson += car.getPerson();
                        System.out.print(car.getName() + "  ");
                    }
                }
                for(Car car: currentCars){
                    if(car instanceof Pickup){
                        nPerson += car.getPerson();
                        System.out.print(car.getName() + "  ");
                    }
                }
                for(Car car: currentCars){
                    if(car instanceof Bus){
                        nPerson += car.getPerson();
                        System.out.print(car.getName() + "  ");
                    }
                }
                System.out.println("共载人:" + nPerson + "人");

                System.out.println("***可载货的车有:");           
                for(Car car: currentCars){
                    if(car instanceof Truck){
                        nTon += car.getTon();   
                        System.out.print(car.getName() + "  ");
                    }
                }   
                for(Car car: currentCars){
                    if(car instanceof Pickup){
                        nTon += car.getTon();   
                        System.out.print(car.getName() + "  ");
                    }
                }           
                System.out.println("共载货:" + nTon + "吨");

                for(Car car: currentCars){
                    nMoney += car.getMoney();
                }
                nMoney = nMoney * day;
                System.out.println("***租车总价格为:" + nMoney + "元");
            }else{
                System.out.println("请重新输入(~_~)");
            }
        }
    }

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