ITPub博客

首页 > IT职业 > IT职场 > 租车的练习

租车的练习

IT职场 作者:sunflower 时间:2021-09-09 09:13:21 0 删除 编辑

参考了老师的内容。以下需一路正确操作才能执行成功,输入错误时的情况没有做处理。
图片描述

package com.imooc.zuche;

public class Car {
    public String name;
    public double rent;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getRent() {
        return rent;
    }
    public void setRent(double rent) {
        this.rent = rent;
    }

}
package com.imooc.zuche;

public class PassengerCar extends Car {
    private double peoplecapacity;
    //有参构造方法初始化
    public PassengerCar(String name,double rent,double peoplecapacity){
        this.name = name;
        this.rent = rent;
        this.peoplecapacity = peoplecapacity;
    }
    public double getPeoplecapacity() {
        return peoplecapacity;
    }
    public void setPeoplecapacity(double peoplecapacity) {
        this.peoplecapacity = peoplecapacity;
    }

}
package com.imooc.zuche;

public class Trunk extends Car {
    private double cargoCapacity;
    public Trunk(String name,double rent,double cargoCapacity){
        this.name = name;
        this.rent = rent;
        this.cargoCapacity = cargoCapacity;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }

}
package com.imooc.zuche;

public class PickUp extends Car {
    private double peoplecapacity;
    private double cargoCapacity;
    public PickUp(String name,double rent,double peoplecapacity,double cargoCapacity){
        this.name = name;
        this.rent = rent;
        this.peoplecapacity = peoplecapacity;
        this.cargoCapacity = cargoCapacity;
    }

    public double getPeoplecapacity() {
        return peoplecapacity;
    }

    public void setPeoplecapacity(double peoplecapacity) {
        this.peoplecapacity = peoplecapacity;
    }

    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }

}
package com.imooc.zuche;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] carsForRent = {
                new PassengerCar("奥迪A4",500,4) ,
                new PassengerCar("马自达6",400,4),
                new PickUp("皮卡雪6",450,4,2),
                new PassengerCar("金龙",800,20),
                new Trunk("松花江",400,4),
                new Trunk("依维柯",1000,20)
                };

        System.out.println("欢迎使用租车系统:");

        System.out.println("你是否需要租车:1是 0否");  //从控制台接收用户是否租车的指令
        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        if(input.equals("1")){  //确认要租车时执行模块
            System.out.println("你可以租车的类型及其价目表:");
            System.out.println("序号t汽车名称tt租金tt容量");
            int i = 1;
            for(Car currentCar:carsForRent){    //遍历数组显示租车类型及其价目
                if(currentCar instanceof PassengerCar){ //判断对象是否为PassengerCar类的一个实例
                    System.out.println(" "+i+"t"+currentCar.getName()+"tt"+currentCar.getRent()+"元/天t"+((PassengerCar)currentCar).getPeoplecapacity()+"人");
                }else if(currentCar instanceof Trunk){  //判断对象是否为Trunk类的一个实例
                    System.out.println(" "+i+"t"+currentCar.getName()+"tt"+currentCar.getRent()+"元/天t"+((Trunk)currentCar).getCargoCapacity()+"吨");
                }else if(currentCar instanceof PickUp){ //判断对象是否为PickUp类的一个实例
                    System.out.println(" "+i+"t"+currentCar.getName()+"tt"+currentCar.getRent()+"元/天t"+((PickUp)currentCar).getPeoplecapacity()+"人,"+((PickUp)currentCar).getCargoCapacity()+"吨");
                }
                i++;
            }

            System.out.println("请输入您要租车的数量:");
            //接收用户输入的数量
            int amount = scan.nextInt();
            //定义一个数组,存放所选择的车辆信息
            Car rent[] = new Car[amount];
            for(int n=0;n

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2904/viewspace-2808743/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-19

  • 博文量
    67
  • 访问量
    520