欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Java编写一个租车小程序

程序员文章站 2022-07-14 23:17:30
...

要求实现的功能有:

1.展示所有可租车辆

2.选择车型,租车量

3.显示租车清单,包括:总载货量,总载客量,总金额等;

4、车辆类型

客车:只能载客

货车:只能载货

皮卡:既能载人,有能载货

Java代码:

package car.rental.applet;
import java.util.Scanner;

class Vehicle{
	String name;
	int num;
	int passenger_num;
	int goods_num;
	int rental_cost;
	void vehicle_type(int a,int b) {
		passenger_num = a;
		goods_num = b;
		if(passenger_num == 0) {
			System.out.print("只能载货"+"                  ");
		}
		else if(goods_num == 0) {
			System.out.print("只能载客"+"                  ");
		}
		else{
			System.out.print("既能载人有能载货"+"   ");
		}
	}
	void vehicle_message(int a,String ch,int b) {
		num = a;
		name = ch;
		rental_cost = b;
		System.out.print(num+"     "+name+"     "+rental_cost+"元"+"       ");
	}
	void capacity(int a,int b) {
		passenger_num = a;
		goods_num = b;
		if(passenger_num == 0) {
			System.out.println("载货量:"+goods_num+"吨");
		}
		else if(goods_num == 0) {
			System.out.println("载客量:"+passenger_num+"人");
		}
		else{
			System.out.println("载货量:"+goods_num+"吨"+" "+"载客量:"+passenger_num+"人");
		}
	}
}

class Bus extends Vehicle{
	String name = "客车";
	int num = 1;
	int passenger_num = 33;
	int goods_num = 0;
	int rental_cost = 123;
}

class Truck extends Vehicle{
	String name = "货车";
	int num = 2;
	int passenger_num = 0;
	int goods_num = 210;
	int rental_cost = 189;
}

class Pickup_truck extends Vehicle{
	String name = "皮卡";
	int num = 3;
	int passenger_num = 22;
	int goods_num = 120;
	int rental_cost = 345;
}

public class Demo {

	public Demo() {
		// TODO 自动生成的构造函数存根
	}

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		System.out.println("欢迎来到在线租车系统:");
		System.out.println("可供选择的车辆有如下几种:");
		Bus bus = new Bus();
		Truck truck = new Truck();
		Pickup_truck pickup = new Pickup_truck();
		System.out.println("型号"+"         "+"车名"+"     "+"日租金额"+"     "+"类型"+"                        "+"容量");
		bus.vehicle_message (bus.num,bus.name,bus.rental_cost);
		bus.vehicle_type (bus.passenger_num, bus.goods_num);
		bus.capacity (bus.passenger_num,bus.goods_num);
		truck.vehicle_message (truck.num,truck.name,truck.rental_cost);
		truck.vehicle_type (truck.passenger_num, truck.goods_num);
		truck.capacity (truck.passenger_num,truck.goods_num);
		pickup.vehicle_message (pickup.num,pickup.name,pickup.rental_cost);
		pickup.vehicle_type (pickup.passenger_num, pickup.goods_num);
		pickup.capacity (pickup.passenger_num,pickup.goods_num);
		
		System.out.println ("请手动输入您要租用的车辆型号以及租车量:");
		
		Scanner in = new Scanner(System.in);
		int choice = in.nextInt();
		int num = in.nextInt();
		int cost = 0,p_num = 0,g_num = 0;
		if(choice == 1) {
			System.out.println("您已选择:");
			p_num = num*bus.passenger_num;
			cost = cost+bus.rental_cost*num;
			System.out.println("客车"+"     "+"载客量为:"+p_num+"     "+"金额为:"+cost+"元");
		}
		else if(choice == 2) {
			System.out.println("您已选择:");
			g_num = num*truck.goods_num;
			cost = cost+truck.rental_cost*num;
			System.out.println("货车"+"     "+"载货量为:"+g_num+"     "+"金额为:"+cost+"元");
		}
		else {
			System.out.println("您已选择:");
			p_num = num*pickup.passenger_num;
			g_num = num*pickup.goods_num;
			cost = cost+pickup.rental_cost*num;
			System.out.println("皮卡"+"     "+"载客量为:"+p_num+",载货量为:"+g_num+"     "+"金额为:"+cost+"元");
		}
		
		System.out.println("是否继续选择租用车辆:输入0继续,输入1退出租车系统");
		
		int flag = in.nextInt();
		while(flag != 1) {
			System.out.println ("请手动输入您要租用的车辆型号以及租车量:");
			choice = in.nextInt();
			num = in.nextInt();
			if(choice == 1) {
				System.out.println("您已选择:");
				p_num = p_num+num*bus.passenger_num;
				cost = cost+bus.rental_cost*num;
				System.out.println("客车"+"     "+"载客量为:"+p_num+"     "+"金额为:"+cost+"元");
			}
			else if(choice == 2) {
				System.out.println("您已选择:");
				g_num = g_num+num*truck.goods_num;
				cost = cost+truck.rental_cost*num;
				System.out.println("货车"+"     "+"载货量为:"+g_num+"     "+"金额为:"+cost+"元");
			}
			else {
				System.out.println("您已选择:");
				p_num = p_num+num*pickup.passenger_num;
				g_num = g_num+num*pickup.goods_num;
				cost = cost+pickup.rental_cost*num;
				System.out.println("皮卡"+"     "+"载客量为:"+p_num+",载货量为:"+g_num+"     "+"金额为:"+cost+"元");
			}
			System.out.println("是否继续选择租用车辆:输入0继续,输入1退出租车系统");
			flag = in.nextInt();
		}
		System.out.println("以下是您的租车清单:");
		System.out.println("总载客量为:"+p_num+"     "+"总载货量为:"+g_num+"     "+"总金额为:"+cost+"元");
		
		System.out.println("感谢您的使用,在线租车系统很乐意为您服务,欢迎下次光临!");
	}
	
}