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

Java入门第二季第六章作业

标签:
Java

Car.java

package com.imooc;

public class Car {
    public String name;
    public int cost;
    public int passengerLoad = 0;
    public int cargoCapacity = 0;
}

DadaCarRental.java

package com.imooc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DadaCarRental {
    public static int charToNum(int ch){
        int num = ch - 48;
        return num;
    }

    public static void chooseCar(){
        Car od = new Car();
        od.name = "奥迪A4";
        od.cost = 500;
        od.passengerLoad = 4;

        Car mzd = new Car();
        mzd.name = "马自达6";
        mzd.cost = 400;
        mzd.passengerLoad = 4;

        Car pkx = new Car();
        pkx.name = "皮卡雪6";
        pkx.cost = 450;
        pkx.passengerLoad = 4;
        pkx.cargoCapacity = 2;

        Car jl = new Car();
        jl.name = "金龙";
        jl.cost = 800;
        jl.passengerLoad = 20;

        Car shj = new Car();
        shj.name = "松花江";
        shj.cost = 400;
        shj.cargoCapacity = 4;

        Car ywk = new Car();
        ywk.name = "依维柯";
        ywk.cost = 1000;
        ywk.cargoCapacity = 20;

        ArrayList<Car> list = new ArrayList<>();

        list.add(od);
        list.add(mzd);
        list.add(pkx);
        list.add(jl);
        list.add(shj);
        list.add(ywk);

        System.out.println("您可租车的类型及价目表:");
        System.out.println("序号\t汽车名称\t租金\t容量");
        for(int i = 0; i < list.size(); i++) {
            Car nowCar = list.get(i);
//            System.out.println(nowCar.name);
            System.out.print((i+1) + ".\t" +nowCar.name + "\t" + nowCar.cost + "元/天\t");
            if(nowCar.passengerLoad != 0) {
                System.out.print("载人:"+nowCar.passengerLoad+"\t");
            }
            if(nowCar.cargoCapacity != 0){
                System.out.print("载货:"+nowCar.cargoCapacity);
            }
            System.out.println();
        }
        System.out.println("请输入您要租汽车的数量:");

        try{
            int num = System.in.read();
            int ch = System.in.read();
            int rentNum = charToNum(num);
            ArrayList<Car> rentList = new ArrayList<>();
            for(int x = 0; x < rentNum; x++){
                System.out.println("请输入第"+(x+1)+"辆车的序号:");
                int indexCh = System.in.read();
                int ch2 = System.in.read();
                int index = charToNum(indexCh) - 1;
                Car nowCar = list.get(index);
                rentList.add(nowCar);
            }
            int passengerLoad = 0;
            int cargoCapacity = 0;
            int totalCost = 0;
            System.out.print("您的账单:\n***可载人的车有:\n");
            for(int y = 0; y < rentList.size(); y++){
                Car nowCar = list.get(y);
                if(nowCar.passengerLoad != 0){
                    passengerLoad += nowCar.passengerLoad;
                }
                if(nowCar.cargoCapacity != 0){
                    cargoCapacity += nowCar.cargoCapacity;
                }
                totalCost += nowCar.cost;
                System.out.print(nowCar.name + "\t");
            }
            System.out.println("共载人:" + passengerLoad + "人");
            System.out.println("***载货的车有:\n共载货:" + cargoCapacity + "吨");
            System.out.println("***租车总价格:" + totalCost + "元");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        try{
            System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是,0否");
            int isToRent = System.in.read();
            int ch = System.in.read();
            if(charToNum(isToRent) == 1){
                chooseCar();
            }else {
                System.exit(1);
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消