为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门 第二季 第六章练习

标签:
Java
差不多一个下午的时间,将老师最后要求的那个练习进行了实现,使用eclipse测试结果界面完全与测试用例一致!
还希望大家有兴趣的一起讨论。

/**

  • 父类 Car
    */
public class Car {
    public String name;
    public int rent;

    public String getName() {
        return name;
    }

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

    public int getRent() {
        return rent;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public void display() {
        System.out.println("输出自己的相关信息");
    }
}

/**

  • 子类 Manned
    */
public class Manned extends Car {
    public int peopleCapacity;

    public Manned(String name, int rent, int peoplecapacity) {
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peoplecapacity;
    }

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    @Override
    public void display() {
        System.out.println(name + " " + rent + "元/天" + "    " + "载人:"
                + peopleCapacity + "人");
    }

}

/**

  • 子类 Pickup
    */
public class Pickup extends Car {
    private int peopleCapacity;
    private int load;

    public Pickup(String name, int rent, int peoplecapcaity, int load) {
        this.name = name;
        this.rent = rent;
        this.peopleCapacity = peoplecapcaity;
        this.load = load;
    }

    public int getPeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

    public int getLoad() {
        return load;
    }

    public void setLoad(int load) {
        this.load = load;
    }

    @Override
    public void display() {
        System.out.println(name + " " + rent + "元/天" + "    " + "载人:"
                + peopleCapacity + "人" +" "+ "载货:" + load + "吨");
    }

}

/**

  • 子类 Truck
    */
public class Truck extends Car {
    private int load;

    public Truck(String name, int rent, int load) {
        this.name = name;
        this.rent = rent;
        this.load = load;
    }

    public int getLoad() {
        return load;
    }

    public void setLoad(int load) {
        this.load = load;
    }

    @Override
    public void display() {
        System.out
                .println(name + "   " + rent + "元/天" + "    " + "载货:" + load + "吨");
    }

}

/**

  • 测试类 DadaCarTest
    */
import java.util.Scanner;

public class DadaCarTest {
    public void display(Car[] cars) {
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号  汽车名称    租金  容量");
        for (int i = 0; i < cars.length; i++) {
            System.out.print(i + 1 + "  ");
            cars[i].display();
        }
    }

    public static void main(String[] args) {
        Car[] cars = { new Manned("奥迪A4", 500, 4), new Manned("马自达6", 400, 4),
                new Pickup("皮卡雪6", 450, 4, 2), new Manned("金龙", 800, 20),
                new Truck("松花江", 400, 4), new Truck("依维柯", 1000, 20) };
        DadaCarTest dct = new DadaCarTest();
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1是 0否");
        int selectNum = sc.nextInt();
        if (selectNum == 1) {
            dct.display(cars);
            System.out.println("请输入您要租汽车的数量:");
            int num = sc.nextInt();
            int[] carNum = new int[num];
            for (int i = 1; i < num + 1; i++) {
                System.out.println("请输入第" + i + "辆车的序号:");
                carNum[i - 1] = sc.nextInt();
            }
            System.out.println("请输入租车天数:");
            int days = sc.nextInt();
            System.out.println("您的账单:");            
            int totalPeopleNum = 0;
            int totalLoadNum = 0;
            double totalRent = 0.0;
            System.out.println("***可载人的车有:");
            System.out.print("  ");
            for (int i = 0; i < num; i++) {
                Car car = cars[carNum[i] - 1];
                if (car instanceof Manned) {
                    Manned manned = (Manned) car;
                    System.out.print(manned.name + "    ");
                    totalPeopleNum += manned.getPeopleCapacity();
                    totalRent += manned.getRent() * days;
                } else if (car instanceof Pickup) {
                    Pickup pickup = (Pickup) car;
                    System.out.print(pickup.name + "    ");
                    totalPeopleNum += pickup.getPeopleCapacity();
                    totalLoadNum += pickup.getLoad();
                    //totalRent += pickup.getRent();
                }
            }           
            System.out.println("共载人:" + totalPeopleNum + "人");
            System.out.println("***载货的车有:");
            System.out.print("  ");
            for (int i = 0; i < num; i++) {
                Car car = cars[carNum[i] - 1];
                if (car instanceof Truck) {
                    Truck truck = (Truck) car;
                    System.out.print(truck.name + " ");
                    totalLoadNum += truck.getLoad();
                    totalRent += truck.getRent() * days;
                } else if (car instanceof Pickup) {
                    Pickup pickup = (Pickup) car;
                    System.out.print(pickup.name + "    ");
                    totalLoadNum += pickup.getLoad();
                    totalRent += pickup.getRent() * days;
                }
            }
            System.out.println("共载货:" + totalLoadNum + "吨");
            System.out.println("***租车总价格:" + totalRent + "元");
        }
    }
}
点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消