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);
	}
}
发布了71 篇原创文章 · 获赞 15 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览