java实现租车系统

要求:有客车,货车,皮卡三大类的车可以出租,实现出租车的数量,价格以及货物人数的一个报备。

思路:先写个 车的抽象类然后在分别写出客车,货车,皮卡的类,再用super继承抽象类。

 然后写个测试类实现租车系统的运行生成账单。

car 抽象类

package cars01;

public abstract class car {
	 String carName;
	 int persons;
	 double weights;
	 int rent;
	 public car(String carName,int persons,int rent)
	 {
		 this.carName=carName;
		 this.persons=persons;
		 this.rent=rent;
	 }
	 public car(String carName,double weights,int rent)
	 {
		 this.carName=carName;
		 this.weights=weights;
		 this.rent=rent;
	 }
	 public car(String carName,double weights,int persons,int rent)
	 {
		 this.carName=carName;
		 this.persons=persons;
		 this.weights=weights;
		 this.rent=rent;
	 }
	 abstract void show();
}

passenges类

package cars01;

public class passengesCar extends car {//乘客类
	public passengesCar(String carName,int persons,int rent){
		super(carName,persons,rent);
	}
	public void show(){
		System.out.println(carName+"  "+rent+"元/天  载人:"+persons+"人");
	}
}

Truckcar类

package cars01;

public class passengesCar extends car {//乘客类
	public passengesCar(String carName,int persons,int rent){
		super(carName,persons,rent);
	}
	public void show(){
		System.out.println(carName+"  "+rent+"元/天  载人:"+persons+"人");
	}
}

pickupcar类

package cars01;

public class pickupCar extends car {//皮卡类
	public pickupCar(String carName,double weights,int persons,int rent) {
		super(carName,weights,persons,rent);
	}
	void show() {
		System.out.println(carName+"  "+rent+"元/天  载货量:"+weights+"kg"+"载客数:"+persons+"人");
	}

}

test类

package cars01;
import java.util.Scanner;
import java.lang.System;
public class test {

	public static void main(String[] args) {
		 car [] cars= {new TruckCar("松花江",4.0,400),new TruckCar("依维柯",20.0,1000),new pickupCar("皮卡雪",4.0,2,450),
				 new passengesCar("马自达",4,400),new passengesCar("奥迪A4",4,500)};
		   System.out.println("欢迎使用租车系统");
		   System.out.println("您是否要租车:1是 0否");
		   Scanner s1=new Scanner(System.in);
		   int x=s1.nextInt();
		   if(x==1)
		   {
			   System.out.println("您可租车的类型及其价目表: ");
			   System.out.println("序号 汽车名字 租金 容量");
			   for(int i=0;i<cars.length;i++)
			   {
				   System.out.print(i+" ");
				   cars[i].show();
			   }
		   
			   System.out.println("请输入您要租汽车的数量");
			   int y=s1.nextInt();
			   int [] num=new int[y];
			   for(int i=0;i<y;i++)
			   {
				   System.out.printf("请输入第%d辆车的序号:  ",i+1);
				   num[i]=s1.nextInt();
			   }
			   System.out.println("请输入租车天数");
			   int days=s1.nextInt();
			   System.out.println("您的账单: ");
			   test ans=new test();
			   ans.bill(num,cars,days);
		   }
		   else
		   {
				System.out.println("再见");
				System.exit(0);
			}
		   
		   return ;
	}
	public void bill(int [] num,car [] cars,int days)
	{
		int persons=0;
		double weights=0;
		int rents=0;
		for(int i=0;i<num.length;i++)
		{
			if(num[i]>2)
			{
				System.out.println(cars[num[i]].carName+" ");
				persons+=cars[num[i]].persons;
				rents+=cars[num[i]].rent;
			}
			else
			{
				continue;
			}
		}
		for(int i=0;i<num.length;i++)
		{
			if(num[i]<3)
			{
				System.out.println(cars[num[i]].carName+" ");
				weights+=cars[num[i]].weights;
				rents+=cars[num[i]].rent;
			}
		}
		System.out.println("共载人: "+persons+"人");
		System.out.println("共载货: "+weights+"吨");
		System.out.println("租车总价格: "+rents*days+"元");
		return ;
	}
	
}

猜你喜欢

转载自blog.csdn.net/wxl7777/article/details/87911908