答答租车程序
<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);
}
}
}