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

慕课答答租车作业

Shy_Man
关注TA
已关注
手记 2
粉丝 0
获赞 2
package 面向对象.慕课作业嗒嗒租车;

import java.util.Scanner;

public class Car {

    int id;
    int rent;
    int seat;
    int weight;
    String brand;
    // 构造器
    public Car(int id, String brand, int rent, int seat, int weight){
        this.id = id;
        this.brand = brand;
        this.rent = rent;
        this.seat = seat;
        this.weight = weight;
    }

    // 规范打印输出格式,使信息对齐
    public void showInfo(){
        if (this.weight == 0){
            if (this.rent < 800)
                System.out.println(this.id + ".\t\t" + this.brand + "\t\t" + this.rent + "元/天" + "\t\t载人:" + seat);
            else
                System.out.println(this.id + ".\t\t" + this.brand + "\t\t\t" + this.rent + "元/天" + "\t\t载人:" + seat);
        }
        else if (this.seat == 0)
            if (this.rent < 1000)
                System.out.println(this.id + ".\t\t" + this.brand + "\t\t" + this.rent + "元/天" + "\t\t载货:" + weight);
            else
                System.out.println(this.id + ".\t\t" + this.brand + "\t\t" + this.rent + "元/天" + "\t载货:" + weight);
        else
            System.out.println(this.id + ".\t\t" + this.brand + "\t\t" + this.rent + "元/天" + "\t\t载人:" + seat + " 载货:" + weight);
    }
    // main 方法入口
    public static void main(String[] args) {
        // 向上转型表现多态
        Car[] cars = {new PassengerCar(1, "奥迪A4", 500, 4),
                      new PassengerCar(2, "马自达6", 400, 4),
                      new Pickup(3, "皮卡雪6", 450, 4, 4),
                      new PassengerCar(4, "金龙", 800, 20),
                      new Truck(5, "松花江", 400, 4),
                      new Truck(6, "依维柯", 1000, 20)
        };

        Scanner scan = new Scanner(System.in);

        System.out.println("- - - - -欢迎使用答答租车系统- - - - -");
        System.out.println("您是否要租车:1、是  2、否");

        String s = scan.nextLine();
        if (s.equals("1")){
            // 打印所有车型
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号 \t汽车名称\t\t租金\t\t\t容量");
            for (Car car : cars)
                car.showInfo();

            // 询问租车具体情况
            int totalRentNumber;
            int rentDays;
            int totalSeat = 0;
            int totalWeight = 0;
            int cost = 0;
            int rentId;
            String infoForSeat = "";
            String infoForWeight = "";

            System.out.println("请输入您要租车的数量");
            totalRentNumber = scan.nextInt();

            for (int i = 1; i <= totalRentNumber; i++){
                System.out.println("请输入第" + i + "辆车的序号:");
                rentId = scan.nextInt();

                if (cars[rentId-1].seat != 0 && cars[rentId-1].weight == 0)
                    infoForSeat += cars[rentId-1].brand + " ";
                else
                    infoForWeight += cars[rentId-1].brand + " ";

                totalSeat += cars[rentId-1].seat;
                totalWeight += cars[rentId-1].weight;
                cost += cars[rentId-1].rent;
            }

            System.out.println("请输入租车天数:");
            rentDays = scan.nextInt();

            // 打印所有结果
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:\n" + infoForSeat + "共载人:" + totalSeat + "个");
            System.out.println("***可载货的车有:\n" + infoForWeight + "共载货:" + totalWeight + "吨");
            System.out.println("租车总价格:" + cost*rentDays + "元");

            scan.close();
            System.out.println("欢迎您的使用!下次再见!");

        }else
            System.out.println("欢迎您的使用!下次再见!");

    }
}

class  PassengerCar extends Car{

    public PassengerCar(int id, String brand, int rent, int seat){
        super(id, brand, rent, seat, 0);
    }
}

class Truck extends Car{

    public Truck(int id, String brand, int rent, int weight){
        super(id, brand, rent, 0, weight);
    }
}

class Pickup extends Car{

    public Pickup(int id, String brand, int rent, int seat, int weight){
        super(id, brand, rent, seat, weight);
    }
}


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