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

Java入门第二季第六章作业

严北
关注TA
已关注
手记 1
粉丝 6
获赞 3

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();
        }

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