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

答答租车程序

<Car类>
package com.imooc;

public class Car {
    //车辆编号
    private int number;
    //车辆名称
    private String name;
    //车辆租金
    private float price;
    
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "序号:" + number + '\t'+ "名称:" + name + '\t' + "价格:" + price + '\t';
    }
}

<PassengerCar类>
package com.imooc;

public class PassengerCar extends Car{
    //载客量
    private int passenger;
    public int getPassenger() {
        return passenger;
    }
    public void setPassenger(int passenger) {
        this.passenger = passenger;
    }
    
    public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
        super.setName(newName);
        super.setNumber(newNumber);
        super.setPrice(newPrice);
        this.setPassenger(passenger);
    }
    @Override
    public String toString() {
        return super.toString()+"载客量:" + passenger;
    }
}

<TrunkCar类>
package com.imooc;

public class TruckCar extends Car{
    //载货量
    private double capacity;
    public double getCapacity() {
        return capacity;
    }
    public void setCapacity(double capacity) {
        this.capacity = capacity;
    }

    public TruckCar(int newNumber,String newName,float newPrice,double capacity) {
        super.setName(newName);
        super.setNumber(newNumber);
        super.setPrice(newPrice);
        this.setCapacity(capacity);
    }
    @Override
    public String toString() {
        return super.toString()+"载货量:" + capacity;
    }
}

<PickCar类>
package com.imooc;

public class PickCar extends Car {
    //载货量
    private double capacity;
    public double getCapacity() {
        return capacity;
    }
    public void setCapacity(double capacity) {
        this.capacity = capacity;
    }
    //载客量
    private int passenger;
    public int getPassenger() {
        return passenger;
    }
    public void setPassenger(int passenger) {
        this.passenger = passenger;
    }
    
    public PickCar(int newNumber,String newName,float newPrice,int passenger,double capacity) {
        super.setName(newName);
        super.setNumber(newNumber);
        super.setPrice(newPrice);
        this.setCapacity(capacity);
        this.setPassenger(passenger);
    }
    @Override
    public String toString() {
        return super.toString()+"载货量:" + capacity + '\t' + "载客量:" + passenger;
    }
}

<Initial类>
package com.imooc;

import java.util.Scanner;

public class Initial {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建汽车对象
        Car[] cars = new Car[5];
        cars[0] = new TruckCar(1,"重型卡车 ",460,4.5);
        cars[1] = new TruckCar(2,"轻型卡车 ",300,3.5);
        cars[2] = new PassengerCar(3,"大客车",500,45);
        cars[3] = new PassengerCar(4,"小客车",400,30);
        cars[4] = new PickCar(5,"皮卡",450,6,3.5);
        
        System.out.println("欢迎使用答答租车系统!");
        System.out.println("请问您是否需要租车?" + '\t' + "1.是" + '\t' + "2.否");
        Scanner s = new Scanner(System.in);
        int in = s.nextInt();
        //判断是否租车
        if(in == 1) {
            //输出汽车列表
            System.out.println("-------------------------------");
            System.out.println("您可租车的类型及其价目表:");
            for(Car c:cars) {
                System.out.println(c.toString());
            }
            
            //客户选择车辆数目
            System.out.println("-------------------------------");
            System.out.println("请选择您租赁汽车的数量:");
            int num = s.nextInt();
            //客户选择车辆型号
            Car[] rentCars = new Car[num];
            for(int i = 1; i <= num ;i++) {
                System.out.println("请选择您第" + i + "辆车型号(请输入车辆序号):");
                int j = s.nextInt();
                rentCars[i-1] = cars[j-1];
            }
            //客户选择租赁时间
            System.out.println("请输出您租赁时间:");
            int time = s.nextInt();
            
            //计算金额
            double totalprice = 0;
            for(int i = 0; i < num; i++) {
                totalprice += rentCars[i].getPrice()*time;
            }
            //计算总载货量、总载客量
            double totalcapacity = 0;
            int totalpassenger = 0;
            for(int i = 0; i < num; i++) {
                if(rentCars[i] instanceof PassengerCar) {
                    totalpassenger += ((PassengerCar)rentCars[i]).getPassenger();
                }
                if(rentCars[i] instanceof TruckCar) {
                    totalcapacity += ((TruckCar)rentCars[i]).getCapacity();
                }
                if(rentCars[i] instanceof PickCar) {
                    totalpassenger += ((PickCar)rentCars[i]).getPassenger();
                    totalcapacity += ((PickCar)rentCars[i]).getCapacity();
                }
            }
            
            //输出租赁信息
            System.out.println("-------------------------------");
            System.out.println("租赁车辆列表如下:");
            for(Car c1:rentCars) {
                System.out.println(c1.toString());
            }
            System.out.println("总载客量为:" + totalpassenger);
            System.out.println("总载货量为:" + totalcapacity);
            System.out.println("租赁时间为:" + time + "天");
            System.out.println("您共需支付:" + totalprice);
        }
    }
}

正在回答

2 回答

看看我的

//leixing类

package zuche;
import java.util.Scanner;
public class leixing {
 public void enmu() {
  System.out.println("您可租车的类型及价目表:");
  System.out.println("序号            汽车名称              租金                               容量");
  System.out.println("1.    奥迪A4    500元/天             载人:4人    ");
  System.out.println("2.    马自达6    400元/天             载人:4人    ");
  System.out.println("3.    皮卡雪6    450元/天             载人:4人 ,载货:2吨   ");
  System.out.println("4.    金龙                      800元/天             载人:20人    ");
  System.out.println("5.    松花江                 400元/天             载货:4吨   ");
  System.out.println("6.    依维河                 1000元/天             载人:4人    ");
 }
 
 public void cust() {
  System.out.println("请输入你要租车的数量:");
  Scanner s = new Scanner(System.in);
  int in = s.nextInt();
  int[] a = new int[in];
  int sum = 0;
  int person = 0;
  int zl = 0;
  for(int i=1; i<=in; i++)
  {
   System.out.println("请输入第"+i+"辆车的序号");
   int j = s.nextInt();
   switch(j) {
   case 1:
    sum += 500;
    person +=4;
    zl += 0;
    break;
   case 2:
    sum += 400;
    person +=4;
    zl += 0;
    break;
   case 3:
    sum += 450;
    person +=4;
    zl += 2;
    break;
   case 4:
    sum += 800;
    person +=20;
    zl += 0;
    break;
   case 5:
    sum += 400;
    person += 0;
    zl += 20;
    break;
   case 6:
    sum += 1000;
    person += 4;
    zl += 0;
    break;
   default:
    System.out.println("你的输入有误,请重新输入");
    int z = s.nextInt();
   }
   //sum = sum*3;
   System.out.println();
  }
  System.out.println("请输入租车的天数:");
  int k = s.nextInt();
  System.out.println();
  System.out.println();
  System.out.println("您的账单:");
  System.out.println("可载货"+zl+"吨");
  System.out.println("可载"+person+"人");
  System.out.println("总金额为:\n"+sum*k);
 }
}

测试类testcar

package zuche;
import java.util.Scanner;
public class testche {

 public static void main(String[] args) {
  leixing te = new leixing();
  System.out.println("是否租车:\n"+"1.是                              2.否");
  Scanner hh = new Scanner(System.in);
  int iii = hh.nextInt();
  if(iii==1) {
   te.enmu();
   te.cust();
  }
  else
  {
   System.out.println("退出系统");
   return ;
  }
 }

}


0 回复 有任何疑惑可以回复我~

练个手,老哥们看看有什么可以优化的地方

1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530106    人
  • 解答问题       6086    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

答答租车程序

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信