先看效果:
下面代码只实现了查看车辆信息、客户租车(可选择车型、数量、租用天数)两个功能。
#include<string>
#include<iostream>
#include<iomanip>
#include<windows.h>
using namespace std;
//基类:车
class Vehicle
{
public:
int veh_count = 0;//车的编号
double veh_money = 0;//车的日费用
int veh_hiredays = 0;//车的租用时长
string veh_kind ;//车的种类
int car_limitnum = 0;//限载人数
int car_limitkilo = 0;//限载货重
};
Vehicle passenger_car;
Vehicle truck_car;
Vehicle pickup_car;
int i, j, k;
void init()
{
//客车
passenger_car.veh_count = 1;
passenger_car.veh_money = 200;
passenger_car.veh_kind = "客车";
passenger_car.car_limitnum = 20;
passenger_car.car_limitkilo = 0;
//货车
truck_car.veh_count = 2;
truck_car.veh_money = 300;
truck_car.veh_kind = "货车";
truck_car.car_limitnum = 0;
truck_car.car_limitkilo = 1;
//皮卡车
pickup_car.veh_count = 3;
pickup_car.veh_money = 400;
pickup_car.veh_kind = "皮卡车";
pickup_car.car_limitnum = 15;
pickup_car.car_limitkilo = 1;
}
void check(int a,int car_count)//a用于判断是否有租用该车,为输入的租用的车的数量;car_count为车的编号
{
if (a > 0)
{
switch (car_count)
{
case 1:cout << "客车 "; break;
case 2:cout << "货车 "; break;
case 3:cout << "皮卡车 "; break;
}
}
}
void clearscreen()//清除界面
{
system("cls");
}
void mainorder() //主界面
{
cout << "---------------------------------" << endl;
cout << " 欢迎使用***极简租车系统 " << endl;
cout << "---------------------------------" << endl;
cout << " 按1查询车辆信息" << endl;
cout << " 按2进行租车流程" << endl;
}
void press_one_order()
{
cout << "编号 车辆种类 日费用(元) 限载人数 限载货重(吨)" << endl;
//客车
cout << " " << passenger_car.veh_count << " " << passenger_car.veh_kind << " " << passenger_car.veh_money
<< " " << passenger_car.car_limitnum << " " << passenger_car.car_limitkilo<<endl;
//货车
cout << " " << truck_car.veh_count << " " << truck_car.veh_kind << " " << truck_car.veh_money
<< " " << truck_car.car_limitnum << " " << truck_car.car_limitkilo << endl;
//皮卡车
cout << " " << pickup_car.veh_count << " " << pickup_car.veh_kind << " " << pickup_car.veh_money
<< " " << pickup_car.car_limitnum << " " << pickup_car.car_limitkilo << endl;
cout << "按任意整数返回" << endl;
}
void display_bill()//用setw()来使数据整齐
{
cout << "编号 车辆种类 费用(元) 载人数 载重量(吨)" << endl;
//客车
cout << " " << passenger_car.veh_count << " " << passenger_car.veh_kind << " " << setw(12)<<i*passenger_car.veh_money* passenger_car.veh_hiredays
<< setw(10) << i*passenger_car.car_limitnum << setw(14) << i*passenger_car.car_limitkilo<<endl;
//货车
cout << " " << truck_car.veh_count << " " << truck_car.veh_kind << " " << setw(12) << j*truck_car.veh_money* truck_car.veh_hiredays
<< setw(10) << j*truck_car.car_limitnum << setw(14) << j*truck_car.car_limitkilo << endl;
//皮卡车
cout << " " << pickup_car.veh_count << " " << pickup_car.veh_kind << " " << setw(12) << k*pickup_car.veh_money* pickup_car.veh_hiredays
<< setw(10) << k*pickup_car.car_limitnum << setw(14) << k*pickup_car.car_limitkilo << endl;
//总计
cout << "总计 " << "--------" <<setw(12)<<i * passenger_car.veh_money * passenger_car.veh_hiredays
+ j * truck_car.veh_money * truck_car.veh_hiredays + k * pickup_car.veh_money*pickup_car.veh_hiredays
<< setw(10) << i * passenger_car.car_limitnum + j * truck_car.car_limitnum + k * pickup_car.car_limitnum
<< setw(14) << i * passenger_car.car_limitkilo + j * truck_car.car_limitkilo + k * pickup_car.car_limitkilo<<endl;
//所租车的种类
cout << "您租用的车型为:";
check(i, 1);//检测客车
check(j, 2);//检车货车
check(k, 3);//检测皮卡车
cout << endl;
cout << "按任意整数返回" << endl;
}
int main()
{
int num;
init();
while (1)
{
clearscreen();
mainorder();
cin >> num;
if (num == 1)
{
clearscreen();
press_one_order();
cin >> num;
if (num == 3)
{
clearscreen();
mainorder();
}
num = 0;
}
if (num == 2)
{
clearscreen();
cout << "请依次输入要租用客车、货车、皮卡车的数量:";
cin >> i;
cin >> j;
cin >> k;
cout << endl;
cout << "请依次输入要租用客车、货车、皮卡车的天数:";
cin >> passenger_car.veh_hiredays;
cin >> truck_car.veh_hiredays;
cin >> pickup_car.veh_hiredays;
cout << endl;
cout << "您的租车清单为:" << endl;
display_bill();
cin >> num;
if (num == 3)
{
clearscreen();
mainorder();
}
num = 0;
}
}
}