需求说明:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示。
编写程序实现汽车租赁业务,效果如下图所示。
新增“卡车”车型。新购置了卡车,根据吨位,租金每吨每天50元,在作业1的基础上对系统进行扩展,计算汽车租赁的总租金,实现效果如下图所示。
package DayOptional;
/**
* @author yzh
* 2021-01-12 13:59
* @汽车类
*/
public class Che {
private String brand;//品牌
private String carNum;//车牌
private double money;//日租金
private int day;//租赁天数
private float discount;//折扣
public Che(String brand, String carNum, double money) {
this.brand = brand;
this.carNum = carNum;
this.money = money;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCarNum() {
return carNum;
}
public void setCarNum(String carNum) {
this.carNum = carNum;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
//把返回值设置为1不打折,子类中如不满足折扣条件,执行super.getDiscount()
public float getDiscount() {
return 1;
}
//让折扣变成只读属性
/*public void setDiscount(float discount) {
this.discount = discount;
}*/
@Override
public String toString() {
return "Che{" +
"brand='" + brand + '\'' +
", carNum='" + carNum + '\'' +
", money=" + money +
", day=" + day +
", discount=" + discount +
'}';
}
//计算租金的方法:
public double calRent(){
//租金:租赁天数*日租金*折扣
return getDay()*getMoney()*getDiscount();
}
public double noDiscount(){
return getDay()*getMoney();
}
}
package DayOptional;
/**
* @author yzh
* 2021-01-12 14:03
* 轿车类
*/
public class JiaoChe extends Che{
private String type;//车型
public JiaoChe(String brand, String carNum, double money, String type) {
super(brand, carNum, money);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//重写父类getDiscount方法,提供轿车特有的折扣算法,天数判定顺序从大到小
@Override
public float getDiscount() {
if(getDay()>150){
return 0.7f;
}else if(getDay()>30){
return 0.8f;
}else if(getDay()>7){
return 0.9f;
}
return super.getDiscount();
}
@Override
public String toString() {
return super.toString();
}
@Override
public double calRent() {
return super.calRent();
}
}
package DayOptional;
/**
* @author yzh
* 2021-01-12 14:04
* 客车类
*/
public class KeChe extends Che{
private int seatNum;//车座位数
public KeChe(String brand, String carNum, double money,int seatNum) {
super(brand, carNum, money);
this.seatNum = seatNum;
}
public int getSeatNum() {
return seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
//提供客车特有折扣算法
@Override
public float getDiscount() {
if(getDay()>=150){
return 0.6f;
}else if(getDay()>=30){
return 0.7f;
}else if(getDay()>=7){
return 0.8f;
}else if(getDay()>=3){
return 0.9f;
}
return super.getDiscount();
}
@Override
public String toString() {
return super.toString();
}
@Override
public double calRent() {
return super.calRent();
}
}
package DayOptional;
/**
* @author yzh
* 2021-01-13 17:45
* 卡车类
*/
public class Truck extends Che{
private int tonnage;
public Truck(String brand, String carNum, double money, int tonnage) {
super(brand, carNum, money);
this.tonnage = tonnage;
}
public int getTonnage() {
return tonnage;
}
public void setTonnage(int tonnage) {
this.tonnage = tonnage;
}
@Override
public float getDiscount() {
return super.getDiscount();
}
@Override
public double calRent() {
return getDay()*getMoney()*getDiscount()*getTonnage();
}
@Override
public double noDiscount() {
return super.noDiscount()*getTonnage();
}
}
package DayOptional;
/**
* @author yzh
* 2021-01-13 20:13
* 汽车管理类
*/
public class CheService {
public Che findChe(Che[] ches,String findBrand,String findType,int findSeatNum,int findTonnage){
for(Che che:ches){
if(che instanceof JiaoChe){
if(findBrand.equals(che.getBrand()) && findType.equals(((JiaoChe)che).getType())){
return che;
}
}else if(che instanceof KeChe){
if(findBrand.equals(che.getBrand()) && findSeatNum==((KeChe)che).getSeatNum()){
return che;
}
}else if(che instanceof Truck){
if(findBrand.equals(che.getBrand()) && findTonnage==((Truck)che).getTonnage()){
return che;
}
}
}
return null;
}
}
package DayOptional;
import java.util.Scanner;
/**
* @author yzh
* 2021-01-12 15:05
* 测试类
*/
public class TestOne {
public static void main(String[] args) {
CheService service=new CheService();
Scanner input=new Scanner(System.in);
Che[] ches={
new JiaoChe("宝马","京NY28588",800,"X6"),
new JiaoChe("宝马","京CNY3284",600,"550i"),
new JiaoChe("别克","京NT37465",300,"林荫大道"),
new JiaoChe("别克","京NT96968",600,"GL8"),
new KeChe("金杯","京6566754",800,16),
new KeChe("金龙","京8696997",800,16),
new KeChe("金杯","京9696996",1500,34),
new KeChe("金龙","京8696998",1500,34),
new Truck("一汽解放","京P225797",50,1),
new Truck("一汽解放","京P116808",50,2),
new Truck("重庆红岩","京P007919",50,1),
new Truck("重庆红岩","京P118020",50,2),
};
//储存租赁的车辆数组
Che[] rentChes=new Che[10];
//所有车辆总租赁费用
double sumCost=0;
//折扣总金额
double sumDiscount=0;
do {
System.out.println("**********欢迎光临腾飞汽车租赁公司**********");
System.out.println("1、轿车 2、客车 3、卡车");
System.out.println("请选择您要租赁的汽车类型:");
int select=input.nextInt();
String findType=null;//假设客户选择的汽车型号不存在
String findBrand=null;//假设客户选择的汽车品牌不存在
int seatNum=0;//假设客户选择的客车座位数
int tonnage=0;//假设客户选择的卡车吨位
int brandId;
switch(select){
case 1:
System.out.println("请您选择要租赁的轿车品牌:1、宝马 2、别克");
brandId= input.nextInt();
if(brandId==1){
findBrand="宝马";
System.out.println("型号:1、X6 2、550i");
}else if(brandId==2){
findBrand="别克";
System.out.println("型号:1、林荫大道 2、GL8");
}else{
System.out.println("您选择的轿车品牌暂时没有");
}
System.out.println("请您选择型号:");
int typeId= input.nextInt();
if(typeId==1){
findType=brandId==1?"X6":"林荫大道";
}else if(typeId==2){
findType=brandId==1?"550i":"GL8";
}else{
System.out.println("您选择的轿车型号暂时没有");
}
break;
case 2:
System.out.println("请您选择要租赁的客车品牌:1、金杯 2、金龙");
brandId= input.nextInt();
if(brandId==1){
findBrand="金杯";
}else if(brandId==2){
findBrand="金龙";
}else{
System.out.println("您选择的客车品牌暂时没有");
}
System.out.println("座位数量:1、16座 2、34座");
System.out.println("请您选择座位数量:");
int seatNumId= input.nextInt();
if(seatNumId==1){
seatNum=16;
}else if(seatNumId==2){
seatNum=34;
}else{
System.out.println("您选择的客车座位数暂时没有");
}
break;
case 3:
System.out.println("请您选择要租赁的卡车品牌:1、一汽解放 2、重庆红岩");
brandId= input.nextInt();
if(brandId==1){
findBrand="一汽解放";
}else if(brandId==2){
findBrand="重庆红岩";
}else{
System.out.println("您选择的卡车品牌暂时没有");
}
System.out.println("吨位:1、1吨 2、2吨");
System.out.println("请您选择吨位:");
int tonnageId= input.nextInt();
if(tonnageId==1){
tonnage=1;
}else if(tonnageId==2){
tonnage=2;
}else{
System.out.println("您选择的卡车吨位暂时没有");
}
break;
default:
System.out.println("本公司暂无该类型汽车");
break;
}
Che resultChe=service.findChe(ches,findBrand,findType,seatNum,tonnage);
if(resultChe!=null){
System.out.print("请输入您要租赁的天数:");
int days=input.nextInt();
System.out.println("分配给您得汽车号牌是:"+resultChe.getCarNum());
resultChe.setDay(days);
double totalCost=resultChe.calRent();
sumCost+=totalCost;//总费用
System.out.println("您需要支付的租赁费用为:"+totalCost+"元");
System.out.println("本次租赁折扣:"+resultChe.getDiscount()+
",原价:"+resultChe.noDiscount()+"元"+
",帮您节省"+(resultChe.noDiscount()-resultChe.calRent()));
sumDiscount+=(resultChe.noDiscount()-resultChe.calRent());//总节省金额
}
//录入租赁车辆信息:
for(int i=0;i<rentChes.length;i++){
if (rentChes[i]==null) {
rentChes[i]=resultChe;
break;
}
}
for (Che rentCh : rentChes) {
if (rentCh != null) {
System.out.print("您租赁的车辆信息如下:" + "品牌:" + rentCh.getBrand() +
",车牌:" + rentCh.getCarNum() + ",租期:" + rentCh.getDay() + "天" +
",租金:" + rentCh.calRent() + "元");
if (rentCh instanceof JiaoChe) {
System.out.println(",型号:" + ((JiaoChe) rentCh).getType());
} else if (rentCh instanceof KeChe) {
System.out.println(",座位:" + ((KeChe) rentCh).getSeatNum());
} else {
System.out.println(",吨位:" + ((Truck) rentCh).getTonnage());
}
}
}
System.out.println("所有车辆实付费用:"+sumCost+"元"+",总计优惠:"+sumDiscount+"元!");
System.out.println("是否继续租车,按0继续,按任意其它数字退出");
int button=input.nextInt();
if(button!=0){
System.out.println("系统退出,欢迎下次使用");
return;
}
} while (true);
}
}