1 package com.lease; 2 /** 3 * 汽车类 4 * @author Administrator 5 * 2019-01-15 6 */ 7 public abstract class Car { 8 private String type; //汽车类型 9 //private double rent; //日租金 10 11 public Car() { //空参构造 12 super(); 13 } 14 15 public Car(String type) { //带参构造 16 super(); 17 this.type = type; 18 } 19 20 public String getType() { //get方法 21 return type; 22 } 23 public void setType(String type) { //set方法 24 this.type = type; 25 } 26 27 //租车方法 28 public abstract void lease(String type, String model,int days,double sumRent,int seat); 29 30 public abstract double discount(int days,double rent); //打折方法 31 }
1 package com.lease; 2 /** 3 * 轿车类 4 * @author Administrator 5 * 2019-01-15 6 */ 7 public class Sedan extends Car { 8 private String model; //轿车型号 9 private String carno; //车牌 10 private double rent; //日租金 11 12 public Sedan() { 13 super(); 14 15 } 16 //带参构造 17 public Sedan(String type, String model) { 18 super(); 19 this.model = model; 20 if("别克".equals(type)) { //判断类型 21 if("GL8".equals(model)) { //判断型号 22 rent = 600; //日租金 23 carno = "京NT96968"; //车牌 24 }else { 25 rent = 300; 26 carno = "京NT96968"; 27 } 28 }else { 29 if("X6".equals(model)) { 30 rent = 800; 31 carno = "京NT98568"; 32 }else { 33 rent = 600; 34 carno = "京NT99548"; 35 } 36 } 37 38 } 39 40 /** 41 * gei,set方法 42 */ 43 public double getRent() { 44 return rent; 45 } 46 public void setRent(double rent) { 47 this.rent = rent; 48 } 49 public String getModel() { 50 return model; 51 } 52 public void setModel(String model) { 53 this.model = model; 54 } 55 //打折方法并计算租金总额 56 public double discount(int days,double rent) { 57 double sumRent; 58 if(days>150) { 59 sumRent = rent*days*0.7; 60 }else if(days>30){ 61 sumRent = rent*days*0.8; 62 }else if(days>7){ 63 sumRent = rent*days*0.9; 64 }else { 65 sumRent = rent*days; 66 } 67 return sumRent; 68 } 69 //租车方法,并输出您租车的信息 70 public void lease(String type, String model,int days,double sumRent,int seat) { 71 System.out.println("您租赁的是:" + type +model); 72 System.out.println("您租赁的天数是:" + days + "天"); 73 System.out.println("您的车牌是:" + this.carno); 74 System.out.println("您租赁的总价格是:" + sumRent + "元"); 75 76 } 77 78 79 80 }
1 package com.lease; 2 /** 3 * 客车类 4 * @author Administrator 5 * 2019-01-15 6 */ 7 public class Bus extends Car { 8 private int seat; //客车座位 9 private String carno; //车牌 10 private double rent; //日租金 11 12 public Bus() { 13 super(); 14 } 15 16 public Bus(String type, int seat) { 17 super(); 18 this.seat = seat; 19 if(seat==16) { //判断座位数 20 rent = 800; //日租金 21 if("金龙".equals(type)) { //判断类型 22 carno = "京NU5968"; //车牌 23 }else { 24 carno = "京NB95468"; 25 } 26 27 }else { 28 rent = 1600; 29 if("金龙".equals(type)) { 30 carno = "京NB11012"; 31 }else { 32 carno = "京NB91110"; 33 }; 34 } 35 } 36 /** 37 * gei,set方法 38 */ 39 public int getSeat() { 40 return seat; 41 } 42 public void setSeat(int seat) { 43 this.seat = seat; 44 } 45 public double getRent() { 46 return rent; 47 } 48 public void setRent(double rent) { 49 this.rent = rent; 50 } 51 public int getModel() { 52 return seat; 53 } 54 public void setModel(int sent) { 55 this.seat = sent; 56 } 57 //打折方法并计算租金总额 58 public double discount(int days,double rent) { 59 double sumRent; 60 if(days>=150) { 61 sumRent = rent*days*0.6; 62 }else if(days>30){ 63 sumRent = rent*days*0.8; 64 }else if(days>7){ 65 sumRent = rent*days*0.9; 66 }else { 67 sumRent = rent*days; 68 } 69 return sumRent; 70 } 71 @Override//租车方法,并输出您租车的信息 72 public void lease(String type, String model, int days, double sumRent,int seat) { 73 System.out.println("您租赁的是:" + type + seat + "座"); 74 System.out.println("您租赁的天数是:" + days + "天"); 75 System.out.println("您的车牌是:" + this.carno); 76 System.out.println("您租赁的总价格是:" + sumRent + "元"); 77 } 78 }
package com.demo; import java.util.Scanner; import com.lease.Bus; import com.lease.Car; import com.lease.Sedan; public class CarDemo { public static void main(String[] args) { Car car; String model; int seat; int days; Scanner sc =new Scanner(System.in); System.out.println("*****欢迎来到租车系统 *****"); System.out.println("1、轿车\t2、客车"); System.out.println("请输入选择的序号:"); int choose =sc.nextInt(); if(choose==1) { System.out.println("1、宝马\t2、别克"); System.out.println("请输入选择的序号:"); choose = sc.nextInt(); String type = choose == 1? "宝马":"别克"; //选择的类型 if(choose == 1){ System.out.println("1、X6\t2、550i"); System.out.println("请输入选择的序号:"); choose = sc.nextInt(); model = choose == 1? "X6":"550i"; //选择的型号 }else { System.out.println("1、林荫大道\t2、GL8"); System.out.println("请输入选择的序号:"); choose = sc.nextInt(); model = choose == 1? "林荫大道":"GL8"; //选择的型号 } System.out.println("请输入您要租赁的天数:"); days = sc.nextInt(); car = new Sedan(type,model); //Sedan类用带参构造创建对象 Sedan s = (Sedan)car; //调用租赁方法 s.lease(type, model, days, s.discount(days, s.getRent())/*调用discount方法得到总金额*/, 0); }else { System.out.println("1、金杯\t2、金龙"); System.out.println("请输入选择的序号:"); choose = sc.nextInt(); String type = choose == 1? "金杯":"金龙"; //选择的类型 System.out.println("1、16\t2、24"); System.out.println("请输入选择的序号:"); choose = sc.nextInt(); seat = choose == 1? 16:24; //选择座位数 System.out.println("请输入您要租赁的天数:"); days = sc.nextInt(); car = new Bus(type,seat); //Bus类 带参构造 创建对象 //调用租赁方法 Bus bus = (Bus)car; bus.lease(type, null, days, bus.discount(days, bus.getRent()), seat); } } }
运行结果:
来源:https://www.cnblogs.com/fanxinmu/p/10362818.html