某汽车租赁公司出租多种轿车和客车,出租费用以日为单位。
车型
| 轿车 | 客车(金杯、金龙) | |||
别克商务舱GL8 | 宝马55i | 别克林萌大道 | <=16座 | >16座 | |
日租费(元) | 600 | 500 | 300 | 800 | 1500 |
/**
*汽车抽象类
*/
public abstract class Qiche {
public static String no;
public static String brand;
/**
* 无参构造方法
*/
public Qiche() {
no = "京AU8769";
brand = "宝马";
}
/**
* 有参构造方法
*
* @param no
* @param brand
*/
public Qiche(String no, String brand) {
this.no = no;
this.brand = brand;
}
/**
* 计算租金
* 父类的抽象方法指引子类去重写方法
* @return
*/
public abstract double zujin(int days,int type);
}
/**
*汽车类,抽象类,一般把父类都写成抽象类
*轿车类继承父类
*/
public final class Jiaoche extends Qiche {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* 无参构造方法
*/
public Jiaoche() {
type = "商务舱";
}
/**
* 有参构造方法
*/
public Jiaoche(String type) {
super(no, brand);
this.type = type;
}
public double zujin(int days, int type) {
double money = 0;
switch (type) {
case 1:// 1代表宝马550i
money = days * 500;
break;
case 2:// 2代表别克商务舱
money = days * 600;
break;
case 3://别克林萌大道
money = days * 300;
break;
default:
System.out.println("输入错误!");
break;
}
return money;
}
}
/**
* 客车类
* 客车类继承父类
*
*/
public final class Keche extends Qiche {
private int seatCount;
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
/**
* 有参构造方法
* @param seatCount
*/
public Keche(int seatCount){
super(no,brand);
this.seatCount=seatCount;
}
/**
* 无参构造方法
*/
public Keche() {
super();
// TODO Auto-generated constructor stub
}
public Keche(String no, String brand) {
super(no, brand);
// TODO Auto-generated constructor stub
}
public double zujin(int days,int type){
double money=0;
if(type>16){
money=days*1500;
}
if(type<=16){
money=days*800;
}
return money;
}
}
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner mys = new Scanner(System.in);
Random myr = new Random();
int ra = myr.nextInt(9999);
Jiaoche j = new Jiaoche();
Keche k = new Keche();
System.out.println("欢迎来到汽车租赁公司!");
System.out.println("请输入要租赁的天数:");
int a = mys.nextInt();
System.err.println("请输入要租赁的汽车类型:(1.轿车 2.客车)");
int b = mys.nextInt();
if (b == 1) {
System.out.println("请输入要租赁的汽车品牌:(1.宝马 2.别克)");
int c = mys.nextInt();
if (c == 1) {
System.out.println("分配给您的汽车牌号是:京BK" + ra);
double money = j.zujin(a, c);
System.out.println("顾客您好!您需要支付的租赁费用是:" + money);
} else if (c == 2) {
System.out.println("请输入轿车的型号:(2.商务舱GL8 3.林萌大道)");
int d = mys.nextInt();
// if(!("2").equals(d)||!("3").equals(d)){
// System.out.println("输入不正确!");
// }
// else{
System.out.println("分配给您的汽车牌号是:鲁LD" + ra);
double money = j.zujin(a, d);
System.out.println("顾客您好!您需要支付的租赁费用是:" + money);
// }
}
}
if (b == 2) {
System.out.println("请输入要租赁的汽车品牌:(1.金杯 2.金龙)");
int dd = mys.nextInt();
if (dd == 1 || dd == 2) {
System.out.println("请输入客车的座位数");
int ke = mys.nextInt();
System.err.println("分配给您的汽车牌号是:京KU" + ra);
double money = k.zujin(a, ke);
System.err.println("顾客您好!您需要支付的租赁费用是:" + money);
}
}
// else if(b!=1||b!=2){
// System.out.println("输入错误!");
// }
}
}
解决思路:
1.因为只有一家汽车租赁公司,所以在计算租赁价时不需要用该属性来标记某汽车,别克,宝马,金杯,金龙是汽车的品牌,没有必要设计为轿车的子类,可以作为轿车的一个属性型号type的值存在,基于分析,从需求抽象出如下类:汽车、轿车和客车。把汽车设计为父类,轿车和客车作为汽车的子类的存在
2.基于分析,汽车的属性又车牌号(no)、品牌(brand)等属性,品牌的属性值可以是别克、宝马、金杯和金龙。
轿车除了具有汽车类的属性外,还有型号(type)属性,如商务舱GL8,550i,林萌大道等,型号和租金有直接关系,不可忽略。
客车除了具有汽车类的属性外,还有座位数(seatCount)属性,同样不能忽略。
3.类的方法只有一个,就是计算租金,取名为zujin(int days,int type)(type为类型),设计为父类方法,让子类重写
4.把汽车设计为抽象类,不允许实例化。把轿车和客车设计为final类,不允许有子类,把父类中的zujin(int days,int type)设计为抽象方法,强迫子类重写。
5.先编写汽车、轿车和客车的代码,然后根据用户输入数据创建对象并调用zujin(int dats,int type)方法计算租金
(纯手打)
保持学习态度