某汽车租赁公司出租多种轿车和客车,出租费用 以日为单位计算。出租车型及信息如下表所示
最终实现效果
1.首先创建一个父类 (抽象类)
package test6;
//父类
public abstract class Car {private String brand;private int dayRent;private String id;public Car() {}public Car(String brand, int dayRent, String id) {this.brand = brand;this.dayRent = dayRent;this.id = id;}public abstract double getMoney(int day,int dayRent);public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public int getDayRent() {return dayRent;}public void setDayRent(int dayRent) {this.dayRent = dayRent;}public String getId() {return id;}public void setId(String id) {this.id = id;}
}
2.创建一个子类,轿车类,因为计算租金不一样,所以我们要重写父类的抽象方法
package test6;
//子类 轿车
public class SaloonCar extends Car{private String model;public SaloonCar() {super();}@Overridepublic double getMoney(int day,int dayRent) {double rent=0;if (day>0){if (day>0&&day<=7){rent=day*dayRent;}if (day>7&&day<=30){rent = day*dayRent*0.9;} else if (day>=30&&day<=150) {rent = day*dayRent*0.8;} else if (day>150) {rent = day*dayRent*0.7;}}else {System.out.println("输入天数有误,请重新输入天数");}return rent;}public SaloonCar(String brand,int dayRent,String id,String model) {super(brand,dayRent,id);this.model = model;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}
}
3.然后再创建一个客车类
package test6;
//子类 客车
public class PassengerCar extends Car{private int seatNumber;public PassengerCar() {}@Override//重写父类的抽象方法public double getMoney(int day ,int dayRent) {double rent = 0;if (day>0){if (day>0&&day<3){rent=day*dayRent;}if (day>=3&&day<7){rent = day*dayRent*0.9;} else if (day>=7&&day<30) {rent = day*dayRent*0.8;} else if (day>=30&&day<150) {rent = day*dayRent*0.7;} else if (day>=150) {rent = day*dayRent*0.6;}}else {System.out.println("输入天数有误,请重新输入天数");}return rent;}public PassengerCar(String brand,int dayRent,String id,int seatNumber) {super(brand,dayRent,id);this.seatNumber = seatNumber;}public int getSeatNumber() {return seatNumber;}public void setSeatNumber(int seatNumber) {this.seatNumber = seatNumber;}
}
4.最后再创建一个测试类
package test6;import java.util.ArrayList;
import java.util.Scanner;public class TestCar {public static void main(String[] args) {ArrayList<SaloonCar> list = new ArrayList<>();SaloonCar s1=new SaloonCar("宝马",800,"(京NY28588)","宝马55i");SaloonCar s2=new SaloonCar("宝马",600,"(京CNY3284)","宝马X6");SaloonCar s3=new SaloonCar("别克",300,"(京NT37465)","别克林荫大道");SaloonCar s4=new SaloonCar("别克",300,"(京NT96968)","别克GL8");list.add(s1);list.add(s2);list.add(s3);list.add(s4);ArrayList<PassengerCar> list1 = new ArrayList<>();PassengerCar p1=new PassengerCar("金杯",800,"(京6566754)",16);PassengerCar p2=new PassengerCar("金龙",800,"(京86696997)",16);PassengerCar p3=new PassengerCar("金杯",1500,"(京9696996)",32);PassengerCar p4=new PassengerCar("金龙",1500,"(京8696998)",32);list1.add(p1);list1.add(p2);list1.add(p3);list1.add(p4);Scanner sc=new Scanner(System.in);System.out.println("****************欢迎来到汽车租赁公司*****************");System.out.println("请选择你要租赁的汽车类型:1、轿车 2、客车");String choose = sc.next();if (choose.equals("1")){//轿车int index = chooseSaloonCar();//调用方法得到轿车对应的索引SaloonCar s=list.get(index);System.out.println("请输入租赁的天数:");int day=sc.nextInt();double rent = s.getMoney(day,s.getDayRent());System.out.println("分配给您的汽车牌号是:"+s.getId());System.out.println("您需要支付的租赁费用是:"+rent+"元");} else if (choose.equals("2")) {//客车int index = choosePassengerCarIndex();//调用方法得到轿车对应的索引PassengerCar p=list1.get(index);System.out.println("请输入租赁的天数:");int day=sc.nextInt();double rent = p.getMoney(day,p.getDayRent());System.out.println("分配给您的汽车牌号是:"+p.getId());System.out.println("您需要支付的租赁费用是:"+rent+"元");}else {System.out.println("输入错误,没有这个选项");}}//定义一个方法用于返回轿车对应的索引public static int chooseSaloonCar(){Scanner sc=new Scanner(System.in);String choose="";System.out.println("请选择你要租赁的汽车品牌:1、别克 2、宝马");choose=sc.next();if (choose.equals("1")){//别克System.out.println("请选择汽车品牌:1、别克林荫大道 2、别克GL8");choose=sc.next();if (choose.equals("1")){return 2;}else if (choose.equals("2")){return 3;}else {System.out.println("输入错误,没有这个选项");}} else if (choose.equals("2")) {//宝马System.out.println("请选择汽车品牌:1、宝马X6 2、宝马550i");choose=sc.next();if (choose.equals("1")){return 1;} else if (choose.equals("2")) {return 0;}}else {System.out.println("输入错误,没有这个选项");}return -1;}//定义一个方法用于返回客车对应的索引public static int choosePassengerCarIndex(){Scanner sc=new Scanner(System.in);String choose=""; //选择汽车System.out.println("请选择你要租赁的汽车品牌:1、金杯 2、金龙");choose=sc.next();if (choose.equals("1")){//金杯System.out.println("请选择你要租赁的汽车座位数:1、16座 2、32座");choose=sc.next();if (choose.equals("1")){return 0;} else if (choose.equals("2")) {return 2;}else {System.out.println("输入错误,没有这个选项");}} else if (choose.equals("2")) {//金龙System.out.println("请选择你要租赁的汽车座位数:1、16座 2、32座");choose=sc.next();if (choose.equals("1")){return 1;}else if (choose.equals("2")){return 3;}else {System.out.println("输入错误,没有这个选项");}}return -1;}}
运行效果如下图