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

Java入门第二季——项目实战

qq_山_2
关注TA
已关注
手记 1
粉丝 0
获赞 48

car.java

package com.imooc.car;

public class Car {
    private String name;
    private int rent;
    private int cargoCapacity;
    private int peopleCapacity;
    public Car(String name,int rent,int cargoCapacity,int peopleCapacity){
        this.name = name;
        this.rent = rent;
        this.cargoCapacity = cargoCapacity;
        this.peopleCapacity = peopleCapacity;
    }
    public String getName() {
        return name;
    }
    public int getRent() {
        return rent;
    }
    public int getCargoCapacity() {
        return cargoCapacity;
    }
    public int getPeopleCapacity() {
        return peopleCapacity;
    }
}

PassengerCar.java

package com.imooc.car;

public class PassengerCar extends Car {
    public PassengerCar(String name, int rent, int cargoCapacity, int peopleCapacity) {
        super(name, rent, cargoCapacity, peopleCapacity);
    }
}

PickUp.java

package com.imooc.car;

public class PickUp extends Car{
    public PickUp(String name, int rent, int cargoCapacity, int peopleCapacity) {
        super(name, rent, cargoCapacity, peopleCapacity);
    }
}

Trunk.java

package com.imooc.car;

public class Trunk extends Car {
    public Trunk(String name, int rent, int cargoCapacity, int peopleCapacity) {
        super(name, rent, cargoCapacity, peopleCapacity);
    }
}

RentCars.java

package com.imooc.car;
import java.util.Scanner;

public class RentCars {

    public static void main(String[] args) {
        Car[] cars = {new PassengerCar("奥迪A4",500,0,4),
                      new PassengerCar("马自达6",400,0,4),
                      new PickUp("皮卡雪6",450,2,4),
                      new PassengerCar("金龙",800,0,20),
                      new Trunk("松花江",400,4,0),
                      new Trunk("依维柯",1000,20,0)};
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车?1-是,0-否");
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        if(a==1){
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号\t汽车名称\t租金\t\t容量");
            for(int i=0;i<cars.length;i++){
                System.out.println((i+1) + "\t" + cars[i].getName() + "\t" + cars[i].getRent()+"元/天"+
                "\t\t" + "载人:" +cars[i].getPeopleCapacity() +"人  "+ "载货:" + cars[i].getCargoCapacity() +"吨");
            }
            System.out.println("请输入您要租车的数量:");
            int n = scan.nextInt();
            int[] input = new int[n];
            for(int i=0;i<n;i++){
                System.out.println("请输入第" + (i+1) + "辆车的序号:");
                input[i] = scan.nextInt();
            }   

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

            int people = 0;
            int cargo = 0;
            int sum = 0;
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            for(int i=0;i<input.length;i++){             //筛选可载人的车辆,并计算可载人的总数
                if(cars[input[i]-1].getPeopleCapacity()>0){
                    System.out.print(cars[input[i]-1].getName()+" ");
                }
                people += cars[input[i]-1].getPeopleCapacity();
            }
            System.out.println("共载人:" + people + "人");      

            System.out.println("***可载货的车有:");
            for(int i=0;i<input.length;i++){              //筛选可载货的车辆,并计算可载货物总量
                if(cars[input[i]-1].getCargoCapacity()>0){
                    System.out.print(cars[input[i]-1].getName()+" ");
                }
                cargo += cars[input[i]-1].getCargoCapacity();
            }
            System.out.println("共载货:" + cargo + "吨");

            for(int i=0;i<input.length;i++){
                sum += cars[input[i]-1].getRent()*day;
            }
            System.out.println("租车总价格:"+ sum + "元");
        }
        scan.close();                                     //关闭扫描器
    }
}

结果如下:
运行结果:

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