综合练习:使用多态完善汽车租赁系统计价功能

练习3

客车类


public final class Bus extends MotoVehicle {
	private int seatCount;

	public Bus(String no, String brand, int seatCount) {
		super(no, brand);
		this.seatCount = seatCount;
	}
	public int getSeatCount() {
		return seatCount;
	}

	public int calRent(int days) {
		if (seatCount <= 16) {
			return days * 800;
		} else {
			return days * 1500;
		}
	}
}


轿车类

public final class Car extends MotoVehicle {
	private String type;
	public Car(String no, String brand, String type) {
		super(no, brand);
		this.type = type;
	}
	public String getType() {
		return type;
	}
	public int calRent(int days) {
		if ("550i".equals(type)) {
			return days * 500;
		} else if ("商务舱GL8".equals(type)) {
			return 600 * days;
		} else {
			return 300 * days;
		}
	}
}

汽车类(父类)

public abstract class MotoVehicle {
	private String no;
	private String brand;

	public MotoVehicle(String no, String brand) {
		this.no = no;
		this.brand = brand;
	}
	public String getNo() {
		return no;
	}
	public String getBrand() {
		return brand;
	}

	public abstract int calRent(int days);
}

顾客类:

public class Customer {
	String id;
	String name;

	public Customer(String id, String name) {
		this.id=id;
		this.name=name;
	}
	public String getId() {
		return id;
	}
	public String getName() {
		return name;
	}

	public int calcTotalRent(MotoVehicle motos[],int days){	
		int sum=0;
		for(int i=0;i<motos.length;i++)
			sum+=motos[i].calRent(days);
		return sum;
	}
}

测试类:

public class TestRent {
	public static void main(String[] args) {
		int days;
		int totalRent;
		MotoVehicle motos[] = new MotoVehicle[4];
		motos[0] = new Car("冀D28588", "宝马", "550i");
		motos[1] = new Car("冀DN3284", "宝马", "550i");
		motos[2] = new Car("冀D43765", "别克", "商务舱GL8");
		motos[3] = new Bus("冀D43765", "金龙", 34);
		days = 5;
		Customer customer= new Customer("s070537", "张三丰");
		totalRent = customer.calcTotalRent(motos, days);
		System.out.println("车牌号\t\t品牌");
		for(int i=0;i<motos.length;i++){
			System.out.println(motos[i].getNo()+"\t\t"+motos[i].getBrand());
		}
		System.out.println("\n客户名:" + customer.getName()+",租赁天数:"+days
				+ ",租赁总费用:" + totalRent+"。");

	}
}

练习4:
客车类:

public final class Bus extends MotoVehicle {
	private int seatCount;

	public Bus(String no, String brand, int seatCount) {
		super(no, brand);
		this.seatCount = seatCount;
	}
	public int getSeatCount() {
		return seatCount;
	}

	public int calRent(int days) {
		if (seatCount <= 16) {
			return days * 800;
		} else {
			return days * 1500;
		}
	}
}

轿车类:

public final class Car extends MotoVehicle {
	private String type;
	public Car(String no, String brand, String type) {
		super(no, brand);
		this.type = type;
	}
	public String getType() {
		return type;
	}
	public int calRent(int days) {
		if ("550i".equals(type)) {
			return days * 500;
		} else if ("商务舱GL8".equals(type)) {
			return 600 * days;
		} else {
			return 300 * days;
		}
	}
}

汽车类(父类):

public abstract class MotoVehicle {
	private String no;
	private String brand;

	public MotoVehicle(String no, String brand) {
		this.no = no;
		this.brand = brand;
	}
	public String getNo() {
		return no;
	}
	public String getBrand() {
		return brand;
	}

	public abstract int calRent(int days);
}

卡车类:

public class Truck extends MotoVehicle {
    int tonnage;// 吨位

    public Truck(String no, String brand, int tonnage) {
        super(no, brand);
        this.tonnage = tonnage;
    }

    public int getTonnage() {
        return tonnage;
    }
    /**
     * 计算卡车租赁价
     */
    public int calRent(int days) {
        return tonnage * 50 * days;
    }
}

顾客类:

public class Customer {
	String id;
	String name;

	public Customer(String id, String name) {
		this.id=id;
		this.name=name;
	}
	public String getId() {
		return id;
	}
	public String getName() {
		return name;
	}

	public int calcTotalRent(MotoVehicle motos[], int days){
		int sum=0;
		for(int i=0;i<motos.length;i++)
			sum+=motos[i].calRent(days);
		return sum;
	}
}

测试类:

public class TestRent {
	public static void main(String[] args) {
		int days;
		int totalRent;
		MotoVehicle motos[] = new MotoVehicle[5];
		motos[0] = new Car("冀D28588", "宝马", "550i");
		motos[1] = new Car("冀DN3284", "宝马", "550i");
		motos[2] = new Car("冀D43765", "别克", "商务舱GL8");
		motos[3] = new Bus("冀D43765", "金龙", 34);
		motos[4] = new Truck("冀D56577", "解放", 30);
		days = 5;
		Customer customer= new Customer("s070537", "张三丰");
		totalRent = customer.calcTotalRent(motos, days);
		System.out.println("车牌号\t\t品牌");
		for(int i=0;i<motos.length;i++){
			System.out.println(motos[i].getNo()+"\t\t"+motos[i].getBrand());
		}
		System.out.println("\n客户名:" + customer.getName()+",租赁天数:"+days
				+ ",租赁总费用:" + totalRent+"。");

	}
}
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值