java练习,汽车租赁系统(类的封装,重写,抽象)

main方法,程序入口

public class TestCarAndBus {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("欢迎来到汽车租赁系统,请选择你要租赁的汽车类型:1.轿车 2.巴士");
		int choose=input.nextInt();
		switch (choose) {
		case 1:
			System.out.println("请选择轿车品牌:1.宝马 2.别克");
			 choose=input.nextInt();
			if (choose==1) {
				Car c=new Car("京.123456","宝马","550i");
				System.out.println("请选择租赁时间");
				int days=input.nextInt();
				c.calcRent(days);
				System.out.println("一共花费"+c.calcRent(days));
				c.show();
			}else if (choose==2) {
				System.out.println("请选择别克车的型号:1.GL8 2.林荫大道");
				choose=input.nextInt();
				if (choose==1) {
					Car c=new Car("京.12484","别克","商务车GL8");
					System.out.println("请选择租赁时间");
					int days=input.nextInt();
					c.calcRent(days);
					System.out.println("一共花费"+c.calcRent(days));
					c.show();
				}else if (choose==2) {
					Car c=new Car("京.1248445","别克","林荫大道");
					System.out.println("请选择租赁时间");
					int days=input.nextInt();
					c.calcRent(days);
					System.out.println("一共花费"+c.calcRent(days));
					c.show();
				}
			}
			break;
		case 2:System.out.println("请选择巴士的座位数:1.小于等于16座 2.大于16座");
			choose =input.nextInt();
			if (choose==1) {
				Bus b=new Bus("鄂.123456","金杯",12);
				System.out.println("请选择租赁时间");
				int days=input.nextInt();
				b.calcRent(days);
				System.out.println("一共花费"+b.calcRent(days));
				b.show();
			}else if (choose==2) {
				Bus b=new Bus("鄂.123456","金龙",24);
				System.out.println("请选择租赁时间");
				int days=input.nextInt();
				b.calcRent(days);
				System.out.println("一共花费"+b.calcRent(days));
				b.show();
			}
			break;

		default:
			break;
		}
	}

} 

父类

public abstract class Motovehicle {
	private String no;
	private String brand;
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Motovehicle(){
		
	}
	public Motovehicle(String no,String brand){
		this.no=no;
		this.brand=brand;
	}
	public abstract int calcRent(int days);
	public void show(){
		
	}
}

car子类

public  final class Car extends Motovehicle {
	private String type;
	public Car (String no,String brand,String type){
		super(no,brand);
		this.type=type;
	}
	public int calcRent(int days) {
		int rent = 0;
	if ("宝马".equals(getBrand())) {
		rent=days*500;
	}else if ("别克".equals(getBrand())) {
		if ("商务车GL8".equals(type)) {
			rent=days*600;
		}else if ("林荫大道".equals(type)) {
			rent=days*300;
		}
	
	}
		return rent;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void show(){
		System.out.println("车的型号是"+super.getBrand()+"车牌是"+super.getNo()+"车的型号是"+type);
	}
}

bus 子类

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 void setSeatCount(int seatCount) {
		this.seatCount = seatCount;
	}

	public int calcRent(int days) {
		int rend=0;
		if (seatCount<=16) {
			rend=800*days;
		}else if (seatCount>16) {
			rend=1500*days;
		}
		return rend;
	}
	public void show(){
		System.out.println("车的型号是:"+super.getBrand()+"车牌是"+super.getNo()+"车的座位是"+seatCount);
	}
}
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值