栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

慕课答答租车作业

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

慕课答答租车作业

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 + ".tt" + this.brand + "tt" + this.rent + "元/天" + "tt载人:" + seat);
            else
                System.out.println(this.id + ".tt" + this.brand + "ttt" + this.rent + "元/天" + "tt载人:" + seat);
        }
        else if (this.seat == 0)
            if (this.rent < 1000)
                System.out.println(this.id + ".tt" + this.brand + "tt" + this.rent + "元/天" + "tt载货:" + weight);
            else
                System.out.println(this.id + ".tt" + this.brand + "tt" + this.rent + "元/天" + "t载货:" + weight);
        else
            System.out.println(this.id + ".tt" + this.brand + "tt" + this.rent + "元/天" + "tt载人:" + 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汽车名称tt租金ttt容量");
            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);
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/237666.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号