Java实现简单的汽车租赁系统

需求如下:   

问题分析:

    首先应当构建一个MotoVehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌;还包含一个no属性,表示汽车牌号;

package cn.jbit.car;

public abstract class MotoVehicle {
	private String no;
	private String brand;
	/**
	 * 无参构造方法
	 */
	public MotoVehicle() {
		
	}
	/**
	 * 有参构造方法
	 * @param no 汽车牌号
	 * @param 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);
}

    其次,应有Car类继承自MotoVehicle类,并有一个type属性,表示轿车型号,应有一个计算租金的方法calRent()

package cn.jbit.car;

public class Car extends MotoVehicle{
	private String type;
	public Car() {
		
	}
	public Car (String no,String brand,String type) {
		super(no,brand);
		this.type=type;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	@Override
	public int calRent(int days) {
		// TODO Auto-generated method stub
		if("2".equals(type)) {
			return days*500;
		}
		else if ("1".equals(type)) {
			return days*600;
		}
		else {
			return 300*days;
		}
	} 
}

    再次,应有Bus类继承自MotoVehicle类,并有一个CountSet属性,表示客车的容量,同样的,应有一个计算租金的方法calRent();

package cn.jbit.car;

public class Bus extends MotoVehicle {
	int CountSet;
	public Bus() {
	}
	/**
	 * 带参构造函数
	 */
	public Bus(String brand,String no,int CountSet) {
		super(brand,no);
		this.CountSet=CountSet; 
	}
	public int getCountSet() {
		return CountSet;
	} 
	public void setCountSet(int countSet) {
		CountSet = countSet;
	}
	
	@Override
	public int calRent(int days) {
		// TODO Auto-generated method stub
		if(CountSet<16) {
			return 800*days;
		}
		else {
			return 1600*days;
		}
	}
	
}

    最后,以上三类应在test类中测试;

package cn.jbit.car;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		String no,brand,mtype;
		int countSet,days;
		Scanner input=new Scanner(System.in);
		System.out.println("*****欢迎来到汽车租赁公司!******");
		System.out.println("请输入天数:");
		days=input.nextInt();
		System.out.println("请输入车辆类型:");
		System.out.println("1、轿车    2、客车");
		mtype=input.next();
		if("1".equals(mtype)) {
			System.out.println("请输入轿车品牌:");
			System.out.println("1、宝马		2、别克");
			brand=input.next();
			if("1".equals(brand)) {
				System.out.println("2、宝马550i:500");
				System.out.println("请输入轿车型号:");
				mtype=input.next();
				System.out.println("请输入辆数:");
				int count=input.nextInt();
				Car car=new Car("辽B000",brand,mtype);
				System.out.println("您需支付:"+count*car.calRent(days));
			
			}
			else {
				System.out.println("1、别克商务GL8:600    3、别克林荫大道:300");
				mtype=input.next();
				System.out.println("请输入辆数:");
				int count=input.nextInt();
				Car car=new Car("辽B000",brand,mtype);
				System.out.println("您需支付:"+count*car.calRent(days));
			}
		}
		else {
			System.out.println("请输入品牌:");
			System.out.println("1、金杯    2、金龙");
			brand=input.next();
			System.out.println("请输入座位数:");
			countSet=input.nextInt();
			System.out.println("请输入辆数:");
			int count=input.nextInt();
			Bus b=new Bus(brand,"辽B000",countSet);
			System.out.println("您需支付:"+b.calRent(days)*count);
		}
	}

}

        






猜你喜欢

转载自blog.csdn.net/HurryRabbit/article/details/80917818