汽车租赁(集合框架,封装继承多态等)

来源:互联网 发布:淘宝网毛衣 编辑:程序博客网 时间:2024/06/12 00:29

//父类Vehicle 

public abstract class Vehicle {
private String no;
private String brand;
private int money;


public Vehicle() {
}


public Vehicle(String no, String brand, int money) {
this.no = no;
this.brand = brand;
this.money = money;
}


public String getNo() {
return no;
}


public void setNo(String no) {
this.no = no;
}


public String getBrand() {
return brand;
}


public void setBrand(String brand) {
this.brand = brand;
}


public abstract double rent(int days);


public int getMoney() {
return money;
}


public void setMoney(int money) {
this.money = money;
}
}


//子类Car类

public class Car extends Vehicle {
private int type;


public Car() {
super();
}


public Car(String no, String brand, int type, int money) {
super(no, brand, money);
this.setType(type);
}


@Override
public double rent(int days) {
double price = 0;
double total = 0;
if ("宝马".equals(getBrand())) {
if (1 == getType()) {
price = days * getMoney();
} else if (2 == getType()) {
price = days * getMoney();
}
} else if ("别克".equals(getBrand())) {
if (1 == getType()) {
price = days * getMoney();
} else if (2 == getType()) {
price = days * getMoney();
}
}


if (days >= 150) {
total = price * 0.7;
} else if (days >= 30) {
total = price * 0.8;
} else if (days >= 7) {
total = price * 0.9;
} else {
total = price;
}


return total;
}


public int getType() {
return type;
}


public void setType(int type) {
this.type = type;
}


}

//子类Bus类

public class Bus extends Vehicle {
private int seats;


public Bus() {
super();
}


public Bus(String no, String brand, int seats, int money) {
super(no, brand, money);
this.setSeats(seats);
}


public int getSeats() {
return seats;
}


public void setSeats(int seats) {
this.seats = seats;
}


@Override
public double rent(int days) {
double price = 0;
double total = 0;
if ("金龙".equals(getBrand())) {
price = days * getMoney();
} else if ("金杯".equals(getBrand())) {
price = days * getMoney();
}


if (days >= 150) {
total = price * 0.6;
} else if (days >= 30) {
total = price * 0.7;
} else if (days >= 7) {
total = price * 0.8;
} else if (days >= 3) {
total = price * 0.9;
} else {
total = price;
}
return total;
}
}


//子类Truck类

public class Truck extends Vehicle {
private int tons;


public Truck() {
super();
}


public Truck(String no, String brand, int tons, int money) {
super(no, brand, money);
this.setTons(tons);
}


public int getTons() {
return tons;
}


public void setTons(int tons) {
this.tons = tons;
}


@Override
public double rent(int days) {
double price = 0;
price = days * tons * getMoney();
return price;
}
}


//业务类

import java.util.ArrayList;
import java.util.Scanner;

public class VehicleBiz {
static String no = "苏A12345", brand;
static int type, seats, tons, days, num, money;
static double allMoney;
static Vehicle ve[] = new Vehicle[100];
static ArrayList<Vehicle> list = new ArrayList<Vehicle>();


public void showChoice() {
Scanner input = new Scanner(System.in);
System.out.println("请输入你想租的车子数量:");
num = input.nextInt();
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "辆车子的天数:");
days = input.nextInt();
System.out.println("请输入要租赁的车子类型(1.轿车    2.客车   3.卡车):");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("请输入轿车的品牌(1.宝马    2.别克):");
int CarChoice = input.nextInt();
if (CarChoice == 1) {
System.out.println("请输入宝马的型号(1.550   2.X6):");
int CarType = input.nextInt();
brand = "宝马";
type = (CarType == 1) ? 1 : 2;
money = (CarType == 1) ? 100 : 200;
} else if (CarChoice == 2) {
System.out.println("请输入别克的型号(1.君威  2.君越):");
int CarType = input.nextInt();
brand = "别克";
type = (CarType == 1) ? 1 : 2;
money = (CarType == 1) ? 200 : 300;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Car(no, brand, type, money);
list.add(i, ve[i]);
break;
case 2:
System.out.println("请选择客车的品牌(1.金龙   2.金杯):");
int BusChoice = input.nextInt();
System.out.println("请输入你得座位数:");
int BusSeats = input.nextInt();
if (BusChoice == 1) {
brand = "金龙";
seats = BusSeats;
money = (BusSeats < 16) ? 1000 : 1500;
} else if (BusChoice == 2) {
brand = "金杯";
seats = BusSeats;
money = (BusSeats < 16) ? 1500 : 2000;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Bus(no, brand, seats, money);
list.add(i, ve[i]);
break;
case 3:
System.out.println("请选择卡车的品牌(1.一汽重卡    2.重庆红岩):");
int trcukChoice = input.nextInt();
System.out.println("请选择你要的吨位:");
int truckTons = input.nextInt();
if (trcukChoice == 1) {
brand = "一汽重卡";
tons = truckTons;
money = 50;
} else if (trcukChoice == 2) {
brand = "重庆红岩";
tons = truckTons;
money = 50;
}
System.out.println("您的车牌是:" + no);
ve[i] = new Truck(no, brand, tons, money);
list.add(i, ve[i]);
break;
}
allMoney = allMoney + ve[i].rent(days);
}
System.out.println("您一共租了" + num + "车子");
System.out.println("租金是" + allMoney + "元");
for (int j = 0; j < num; j++) {
if (ve[j] != null) {
System.out.println("您车子的型号是" + ve[j].getBrand() + ",您租赁了"
+ days + "天" + ",日租金是" + ve[j].getMoney() + "元");
}
}
}
}


//程序入口类(Test)类

public class Test {
public static void main(String[] args) {
VehicleBiz vb=new VehicleBiz();
vb.showChoice();

}


}

0 0
原创粉丝点击