C++类——派生类、链表、多态设计实现的汽车租赁系统

功能介绍

在这里插入图片描述
用图表的信息实现不同价格的汽车租赁
1、每辆车有车牌号、品牌、颜色、行驶公里数、租金、租车人,轿车有类型区别,客车有座位数量区别
2、可选择借的车型、借用的天数
3、计算租用的价钱,更改汽车的租用信息
4、还车时更改汽车的租用信息,并增加行驶公里数

程序演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码部分

Vehicle.h

//文件Vehicle.h
#pragma once
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;

//基类:车
class Vehicle
{
private:
	string  vnumber; //车牌
	string  vbrand;  //品牌
	string  vcolour; //颜色
	string  vmile;   //公里数
	string  vcost;    //租金
	string  vcondition;  //租车人
public:
	Vehicle* next;
	Vehicle(string number, string brand, string colour, string mile,string cost,string condition);
	~Vehicle();
	virtual string filename() = 0; //纯虚数 文件名
	virtual int cost() = 0;	       //纯虚数  租金
	virtual void bill() = 0;       //纯虚数  打印账单
	virtual void show()=0;         //纯虚数  显示车辆信息
	virtual string getType()=0;    //纯虚数  得到类型
	void vborrow();                //租车函数
	void vreturn();                //还车函数
	string getNumber();            //分别返回车牌号、品牌、颜色、里程数、日租金、租车人
	string getBrand();
	string getColour();
	string getMile();
	string getCost();
	string getCondition();
};

//轿车
class Car :public Vehicle
{
private:
	string cmodel; //类型
public:
	Car(string number, string brand, string colour, string mile,string cost, string condition,string type);
	~Car();
	virtual string filename() ;
	virtual int cost();	
	virtual void bill() ;
	virtual void show();
	string getType();
};


//客车
class Bus :public Vehicle
{
private:
	string bseat;  //座位
public:
	Bus(string number, string brand, string colour, string mile,string cost, string condition,string seat);
	~Bus();
	virtual string filename() ;
	virtual int cost();	
	virtual void bill() ;
	virtual void show();
	string getType();
}; 

Vehicle.cpp

//文件Vehicle.cpp
#include "vehicle.h"


using namespace std;

//基类:车
Vehicle::Vehicle(string number, string brand, string colour, string mile,string cost, string condition)
{
	vnumber = number;
	vbrand = brand;
	vcolour = colour;
	vmile = mile;
	vcost = cost;
	vcondition = condition;
}

Vehicle::~Vehicle()
{
}


void Vehicle::vborrow() //租车函数
{
	cout << "请输入租车人的姓名" << endl;
	cin >> vcondition;
}

void Vehicle::vreturn()//还车函数
{
	int n,m;
	cout << "请输入增加的公里数" << endl;
	cin >> m;
	n = std::stoi(vmile)+m;
	vmile = std::to_string(n);
	vcondition = "未租出";
}

string Vehicle::getNumber()
{
	return vnumber;
}

string Vehicle::getBrand()
{
	return vbrand;
}

string Vehicle::getColour()
{
	return vcolour;
}

string Vehicle::getMile()
{
	return vmile;
}

string Vehicle::getCost()
{
	return vcost;
}

string Vehicle::getCondition()
{
	return vcondition;
}

//轿车
Car::Car(string number, string brand, string colour, string mile,string cost, string condition,string type)
	:Vehicle(number, brand, colour, mile,cost, condition)
{
	cmodel = type;
}
Car::~Car()
{
}
string Car::filename()   //返回文件名
{
	return "Car.txt";
}
int Car::cost()         //返回租金
{
	int n;
	n = std::stoi(Car::getCost()) ;
	return n;
}
void Car::show()        //显示车辆信息
{
	cout << setw(15) <<cmodel<<setw(15)
		<< Car::getNumber ()<< setw(15)
		<< Car::getBrand ()<< setw(15)
		<< Car::getColour()<< setw(15)
		<< Car::getMile()<< setw(15)
		<< Car::getCost() << setw(15)
		<< Car::getCondition() << endl;
}
void Car::bill()       //账单
{
	cout <<endl<< "您租用了轿车" << cmodel << "," << "每日租金为" << Car::cost() << "元。" << endl;
}
string Car::getType()  //返回类型
{
	return cmodel;
}


//大巴
Bus::Bus(string number, string brand, string colour, string mile, string cost, string condition,string type) 
	:Vehicle(number, brand, colour, mile, cost, condition)
{
	bseat = type;
}
Bus::~Bus()
{
}
string Bus::filename()   //返回文件名
{
	return "Bus.txt";
}
int Bus::cost()        //返回日租金
{
	int n;
	n = std::stoi(Bus::getCost());
	return n;
}
void Bus::show()     //显示车辆信息
{
	cout <<setw(15)<< bseat << setw(15)
		<< Bus::getNumber() << setw(15)
		<< Bus::getBrand() << setw(15)
		<< Bus::getColour() << setw(15)
		<< Bus::getMile() << setw(15)
		<< Bus::getCost() << setw(15)
		<< Bus::getCondition() << endl;
}
void Bus::bill()    //账单
{
	cout <<endl<< "您租用了客车" << bseat << "," << "每日租金为" << Bus::cost() << "元。" << endl;
}
string Bus::getType()  //座位
{
	return bseat;
}

VehicleList.h

//VehicleList.h
#pragma once
#include "Vehicle.h"
#include"function.h"

using namespace std;
//链表
class VehicleList
{
private:
	Vehicle* head;  //链表头
public:
	void CarList(); //建立轿车链表并从文件读取
	void BusList(); //建立客车链表并从文件读取
	void showlist();//显示链表内容
	void save();    //保存文件
	int borrow(int* n);//租车、计算价格等等
	int return_();    //换车、增加里程数等等
	void freep();    //释放链表
};

VehicleList.cpp

#include "vehicle.h"

using namespace std;

//基类:车
Vehicle::Vehicle(string number, string brand, string colour, string mile,string cost, string condition)
{
	vnumber = number;
	vbrand = brand;
	vcolour = colour;
	vmile = mile;
	vcost = cost;
	vcondition = condition;
}

Vehicle::~Vehicle()
{
}


void Vehicle::vborrow() //租车函数
{
	cout << "请输入租车人的姓名" << endl;
	cin >> vcondition;
}

void Vehicle::vreturn()//还车函数
{
	int n,m;
	cout << "请输入增加的公里数" << endl;
	cin >> m;
	n = std::stoi(vmile)+m;
	vmile = std::to_string(n);
	vcondition = "未租出";
}

string Vehicle::getNumber()
{
	return vnumber;
}

string Vehicle::getBrand()
{
	return vbrand;
}

string Vehicle::getColour()
{
	return vcolour;
}

string Vehicle::getMile()
{
	return vmile;
}

string Vehicle::getCost()
{
	return vcost;
}

string Vehicle::getCondition()
{
	return vcondition;
}

//轿车
Car::Car(string number, string brand, string colour, string mile,string cost, string condition,string type)
	:Vehicle(number, brand, colour, mile,cost, condition)
{
	cmodel = type;
}
Car::~Car()
{
}
string Car::filename()   //返回文件名
{
	return "Car.txt";
}
int Car::cost()         //返回租金
{
	int n;
	n = std::stoi(Car::getCost()) ;
	return n;
}
void Car::show()        //显示车辆信息
{
	cout << setw(15) <<cmodel<<setw(15)
		<< Car::getNumber ()<< setw(15)
		<< Car::getBrand ()<< setw(15)
		<< Car::getColour()<< setw(15)
		<< Car::getMile()<< setw(15)
		<< Car::getCost() << setw(15)
		<< Car::getCondition() << endl;
}
void Car::bill()       //账单
{
	cout <<endl<< "您租用了轿车" << cmodel << "," << "每日租金为" << Car::cost() << "元。" << endl;
}
string Car::getType()  //返回类型
{
	return cmodel;
}


//大巴
Bus::Bus(string number, string brand, string colour, string mile, string cost, string condition,string type) 
	:Vehicle(number, brand, colour, mile, cost, condition)
{
	bseat = type;
}
Bus::~Bus()
{
}
string Bus::filename()   //返回文件名
{
	return "Bus.txt";
}
int Bus::cost()        //返回日租金
{
	int n;
	n = std::stoi(Bus::getCost());
	return n;
}
void Bus::show()     //显示车辆信息
{
	cout <<setw(15)<< bseat << setw(15)
		<< Bus::getNumber() << setw(15)
		<< Bus::getBrand() << setw(15)
		<< Bus::getColour() << setw(15)
		<< Bus::getMile() << setw(15)
		<< Bus::getCost() << setw(15)
		<< Bus::getCondition() << endl;
}
void Bus::bill()    //账单
{
	cout <<endl<< "您租用了客车" << bseat << "," << "每日租金为" << Bus::cost() << "元。" << endl;
}
string Bus::getType()  //座位
{
	return bseat;
}

function.h

//function.h
#pragma once
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>

void clearscreen();//清屏

void mainorder();  //主目录

void order1();    //目录1

void order21();    //主目录2的第1个

void order22(int n);  //主目录2的第2个

void order23(int a, int b, int c, int d); //主目录的第3个

void order3();  //目录3

int getorder(); //选择


function.cpp

//function.cpp

#include"function.h"

using namespace std;



void clearscreen()
{
	system("cls");
}

void mainorder()          //主目录
{
	clearscreen();
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~    汽车租赁系统   ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "1、查看车辆信息" << endl
		<< "2、租车" << endl
		<< "3、还车" << endl
		<< "4、退出系统" << endl;
}

void order1()             
{
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~    查看车辆信息   ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "1、轿车" << endl
		<< "2、客车" << endl
		<< "3、返回上一级" << endl;
}

void order21()
{
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~        租车       ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "请输入要租用的车辆数:" << endl;
}

void order22(int n)
{
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~        租车       ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "请选择租用的第"<<n<<"辆车的类型:" << endl
		<< "1、轿车" << endl
		<< "2、客车" << endl;
}

void order23(int a,int b,int c,int d)
{
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~      租车订单     ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "本次租车情况:" << endl
		<< "轿车" << a << "辆" << endl
		<< "客车" << b << "辆" << endl
		<< "租用" << c << "天" << endl
		<< "共计" << (d * c) << "元" << endl;
}

void order3()
{
	cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "~        还车       ~" << endl
		<< "~                   ~" << endl
		<< "~~~~~~~~~~~~~~~~~~~~~" << endl
		<< "1、轿车" << endl
		<< "2、客车" << endl
		<< "3、返回上一级" << endl;
}

int getorder()       //选择
{
	int order = 0;
	cin >> order;
	return order;
}


main.cpp

#include "VehicleList.h"
using namespace std;


int main()
{
	VehicleList list;
	while (1)
	{
		mainorder();
		switch (getorder())
		{
		case 1:
		{
			int m = 1;
			while (m)
			{
				clearscreen();
				order1();
				switch (getorder())
				{
				case 1:
					list.CarList();
					list.showlist();
					list.freep();
					system("pause");
					break;
				case 2:
					list.BusList();
					list.showlist();
					list.freep();
					system("pause");
					break;
				case 3:
					m = 0;
					break;
				}
			}
		}
			break;
		case 2:
		{
			clearscreen();
			int money=0,days=0, number, number1=0 , number2=0;
			order21();
			cin >> number;
			cout << "要租用的天数:" << endl;
			cin >> days;
			for (int i = 1; i <= number; i++)
			{
				clearscreen();
				order22(i);
				switch (getorder())
				{
				case 1:
					number1++;
					list.CarList();
					list.borrow(&money);
					list.save();
					list.freep();
					system("pause");
					break;
				case 2:
					number2++;
					list.BusList();
					list.borrow(&money);
					list.save();
					list.freep();
					system("pause");
					break;
				}
			}
			clearscreen();
			order23(number1,number2,days,money);
			system("pause");
		}
			break;
		case 3: 
		{
			int m = 1;
			while (m)
			{
				clearscreen();
				order3();
				switch (getorder())
				{
				case 1:
					list.CarList();
					list.return_();
					list.save();
					list.freep();
					system("pause");
					break;
				case 2:
					list.BusList();
					list.return_();
					list.save();
					list.freep();
					system("pause");
					break;
				case 3:
					m = 0;
					break;
				}
			}
		}
			break;
		case 4:
			cout <<endl<< "您已退出系统,欢迎下次使用" << endl;
			return 0;
		}
	}
}

文件格式

Bus.txt
在这里插入图片描述
Car.txt
在这里插入图片描述

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
一个简单的汽车租赁管理系统(C++控制台程序): 利用C++实现对汽车和客户信息的增、删、改等操作,并保存。 部分代码: // CarRent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "CarData.h" #include "Menu.h" #include"CustomerData.h" int main() { Menu::welcome(); Menu::login(); //登录界面 char choice; int carid, customerid; //汽车编号,客户编号 CustomerData CustomerDB; //客户库 Customer *r; //客户 CarData CarDB; //汽车库 Car *b; //汽车 Menu::showMenu(); //显示菜单 cout <> choice; while (choice != '0') { switch (choice) { case '1': //输入客户编号和要出租的汽车 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); if (NULL == r) { cout << "不存在该客户!"; cout << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!"; cout <borrowCar() == 0) { cout << "该汽车已租出!"; cout <borrowCar(b->getNo()); cout << "你在" <getBorTime()) << "成功出租一辆" <getName() << endl << "每日租金为(不足一天按一天计算):" <getPay(); break; case '2': //归还操作 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); //按编号查找 if (r == NULL) { cout << "不存在该客户!" << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!" <getOnshelf() == 1) { cout << "该汽车已经归还!" << endl; break; } cout << "您成功归还一辆"

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值