//车类
package Vehicle;
public abstract class Car {
private String carId; //车牌号
private String brand; //品牌
private int rent; //租金
public Car() {
super();
}
public Car(String carId, String brand, int rent) {
super();
this.carId = carId;
this.brand = brand;
this.rent = rent;
}
public String getCarId() {
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public abstract double carRent(double day);
}
package Vehicle;
//轿车类
public class Automobile extends Car{
private String type; //型号
public Automobile() {
super();
}
public Automobile(String carId, String brand, int rent, String type) {
super(carId, brand, rent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double carRent(double day) {
double number = this.getRent()*day;
if(day>150){
number=number*0.7;
}else if(day>30){
number=number*0.8;
}else if(day>7){
number = number*0.9;
}
return number;
}
}
package Vehicle;
//客车类
public class Bus extends Car{
private int seating; //座位数
public Bus() {
super();
}
public Bus(String carId, String brand, int rent, int seating) {
super(carId, brand, rent);
this.seating = seating;
}
public int getSeating() {
return seating;
}
public void setSeating(int seating) {
this.seating = seating;
}
@Override
public double carRent(double day) {
double number = this.getRent();
if(day>=150){
number=number*0.6;
}else if(day>=30){
number=number*0.7;
}else if(day>=7){
number = number*0.8;
}else if(day>=3){
number = number*0.9;
}
return number;
}
}
-------------------------------上面在一个包里,下面在另一个包里--------------------------------------------------
package Business;
import Vehicle.Automobile;
import Vehicle.Bus;
import Vehicle.Car;
public class CarBusiness {
// 定义Car型数组
public Car[] car=new Car[8];
// 初始化
public void init(){
car[0]=new Automobile("京NY28588","宝马",800,"X6");
car[1]=new Automobile("京CNY3284","宝马",600,"550i");
car[2]=new Automobile("京NT37465","别克",300,"林荫大道");
car[3]=new Automobile("京NT96968","别克",600,"GL8");
car[4]=new Bus("京6566754","金杯",800,16);
car[5]=new Bus("京8696997","金龙",800,16);
car[6]=new Bus("京9696996","金杯",1500,34);
car[7]=new Bus("京8696998","金龙",1500,34);
}
// 找车,根据条件,输出相对应的数(品牌,型号,座位数)
public Car rentCar(String brand,String type,int seating){
Car newCar=null;
for(int i=0 ;i<car.length;i++){
if(car[i] instanceof Automobile){
Automobile car1 =(Automobile)car[i];
if(car1.getBrand().equals(brand) && car1.getType().equals(type)){
newCar=car1;
break;
}
}else if(car[i] instanceof Bus){
Bus bus =(Bus)car[i];
if(bus.getBrand().equals(brand) && bus.getSeating()==seating){
newCar=bus;
break;
}
}
}
return newCar;
}
}
package Business;
//管理和测试
import java.util.Scanner;
import Vehicle.Car;
public class CarManagement {
Scanner sc = new Scanner(System.in);
public void show(){
CarBusiness cb = new CarBusiness();
cb.init();
System.out.println("----------------欢迎光临---------------------");
System.out.println("请选择租车的类型:");
System.out.print("1/轿车,2/客车(请选择):");
int num=sc.nextInt();
String brand ="";
String type="";
int seating=0;
if(num==1){
System.out.print("请选择要租车的品牌:1、别克,2、宝马:");
int choose = sc.nextInt();
if(choose==1){
brand="别克";
System.out.print("请选择汽车的型号:1、林荫大道,2、GL8:");
type = (sc.nextInt()==1)?"林荫大道":"GL8";
System.out.print("请输入要租车的天数:");
double numb=sc.nextInt();
Car car = cb.rentCar(brand, type, seating);
System.out.println("租车成功:你的车是:");
System.out.println("车牌号:"+cb.rentCar(brand, type, seating).getCarId()+"\t品牌:"+cb.rentCar(brand, type, seating).getBrand()+
"\t型号:"+type);
System.out.println("你需要支付"+car.carRent(numb)+"元");
}else if(choose==2){
brand="宝马";
System.out.print("请选择汽车的型号:1、X6,2、550i:");
type = (sc.nextInt()==1)?"X6":"550i";
System.out.print("请输入要租车的天数:");
Car car = cb.rentCar(brand, type, seating);
double numb=sc.nextInt();
System.out.println("租车成功:你的车是:");
System.out.println("车牌号:"+cb.rentCar(brand, type, seating).getCarId()+"\t品牌:"+cb.rentCar(brand, type, seating).getBrand()+
"\t型号:"+type);
System.out.println("你需要支付"+car.carRent(numb)+"元");
}
}else if(num==2){
type="";
System.out.print("请选择要租车的品牌:1、金杯,2、金龙:");
brand=sc.nextInt()==1?"金杯":"金龙";
System.out.print("请选择汽车的座位数:1、16座,2、34座:");
seating = sc.nextInt()==1?16:34;
System.out.print("请输入要租车的天数:");
double numb=sc.nextInt();
Car car = cb.rentCar(brand, type, seating);
System.out.println("租车成功:你的车是:");
System.out.println("车牌号:"+cb.rentCar(brand, type, seating).getCarId()+"\t品牌:"+cb.rentCar(brand, type, seating).getBrand()+
"\t座位数:"+seating);
System.out.println("你需要支付"+car.carRent(numb)+"元");
}else{
System.out.println("你的输入有误,已退出");
}
}
public static void main(String[] args) {
CarManagement cm=new CarManagement();
cm.show();
}
}