Java汽车租凭项目

Java汽车租凭项目

模拟完成如图以下所示的汽车租凭项目

在这里插入图片描述

代码如下

机动车父类:

package exam;

public abstract class MotoV {
	private String brand;
	private String carId;
	private int prive;
	private double totalPrice;
	
	public MotoV() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public MotoV(String brand, String carId, int prive) {
		super();
		this.brand = brand;
		this.carId = carId;
		this.prive = prive;
	}
	
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getCarId() {
		return carId;
	}
	public void setCarId(String carId) {
		this.carId = carId;
	}
	public int getPrive() {
		return prive;
	}
	public void setPrive(int prive) {
		this.prive = prive;
	}
	


	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public abstract double price(int days);
}

轿车子类:

package exam;

public class Car extends MotoV {
	private String type;

	public Car() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Car(String brand, String carId, int prive,String type) {
		super(brand, carId, prive);
		this.type = type;
		// TODO Auto-generated constructor stub
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	@Override
	public double price(int days) {
		double discount=1.0;
		if(days>150) {
			discount=0.7;
		}else if(days>30) {
			discount=0.8;
		}else if(days>7) {
			discount=0.9;
		}
		double total=getPrive()*discount*days;
		return total;
	}
}

客车子类:

package exam;

public class Bus extends MotoV {
	private int seat;

	public Bus() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Bus(String brand, String carId, int prive,int seat) {
		super(brand, carId, prive);
		this.seat = seat;
		// TODO Auto-generated constructor stub
	}
	
	public int getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}
	
	
	@Override
	public double price(int days) {
		double discount=1.0;
		if(days>=150) {
			discount=0.6;
		}else if(days>=30) {
			discount=0.7;
		}else if(days>=7) {
			discount=0.8;
		}else if(days>=3) {
			discount=0.9;
		}
		double total=getPrive()*discount*days;
		return total;
	}

}

机动车操作类

package exam;

public class MotoVsys {
	private MotoV[] mo= new MotoV[8];
	
	public MotoVsys() {
		mo[0]= new Car("宝马","京NY28588",800,"x6");
		mo[1]= new Car("宝马","京CNY3284",600,"550i");
		mo[2]= new Car("别克","京NT37465",300,"林荫大道");
		mo[3]= new Car("别克","京NT96968",600,"GL8");
		mo[4]= new Bus("金杯","京6566754",800,16);
		mo[5]= new Bus("金龙","京8696997",800,16);
		mo[6]= new Bus("金杯","京9696996",1500,32);
		mo[7]= new Bus("金龙","京8696998",1500,32);
	}
	public MotoV leaveoutCar(String brand, String type,int seat) {
			for(int i=0;i<mo.length;i++) {
				if(mo[i] instanceof Car) {
					Car c = (Car)mo[i];
				if(c.getBrand().equals(brand) && c.getType().equals(type)) {
					return c;
				}
				}else if(mo[i] instanceof Bus){
					Bus b = (Bus)mo[i];
					if(b.getBrand().equals(brand) && b.getSeat()==seat) {
						return b;
					}
				}
			}
			return null;
	}
	public double totalPrice(MotoV mo,int days)  {
		if(mo instanceof Car) {
			return mo.price(days);
		}else if(mo instanceof Bus) {
			return mo.price(days);
		}
		return 0;
	}
	public String carId(MotoV mo) {
		if(mo instanceof Car) {
			return mo.getCarId();
		}else if(mo instanceof Bus) {
			return mo.getCarId();
		}
		
		return null;
	}
	public double allPrice(MotoV[] mo) {
		double total=0;
		for(int i=0;i<mo.length;i++) {
				if(mo[i] instanceof Car) {
					
					total+=mo[i].getTotalPrice();
				}else if(mo[i] instanceof Bus) {
					total+=mo[i].getTotalPrice();
				}
			}
		return total;
	}
}

测试类:

package exam;

import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MotoVsys mot =new MotoVsys();
		System.out.println("***********租车系统****************");
		System.out.println("请输入租车几次");
		int num = sc.nextInt();
		MotoV[] mt = new MotoV[num];
		double total = 0;
		for(int i=0;i<num;i++) {
			System.out.println("请输入要租的车的类型:1、轿车  2、客车");
			int num1 = sc.nextInt();
			String brand = null;
			String type = null;
			int seat =0;
			switch (num1) {
			case 1:
				System.out.println("请输入要租的车的品牌: 1、宝马  2、别克");
				int num2=sc.nextInt();
				brand = num2==1?"宝马":"别克";
				switch(num2) {
					case 1:
						System.out.println("请输入要租的车的型号: 1、X6  2、550i");
						int num3=sc.nextInt();
						type = num3==1?"X6":"550i";
						break;
					case 2:
						System.out.println("请输入要租的车的型号: 1、林荫大道  2、GL8");
						int num4=sc.nextInt();
						type = num4==1?"林荫大道":"GL8";
						break;
				}
				break;

			case 2:
				System.out.println("请输入要租的车的品牌: 1、金杯  2、金龙");
				int num5=sc.nextInt();
				brand = num5==1?"金杯":"金龙";
				System.out.println("请选择座位: 1、16座  2、32");
				int num6=sc.nextInt();
				seat = num6==1?16:32;
				break;
			}
			System.out.println("请输入要租的天数: ");
			int days = sc.nextInt();
			
			MotoV mo=mot.leaveoutCar(brand, type, seat);
			mt[i]=mo;
			System.out.println("车牌号为: "+mot.carId(mt[i]));
			mo.setTotalPrice(mot.totalPrice(mo, days));
			System.out.println("租金为: "+mot.totalPrice(mo, days));
			System.out.println();
		}
		
		System.out.println("总租金为: "+mot.allPrice(mt));
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值