停车场程序

UML类图:

代码:

#include <iostream>
#include <string>
using namespace std;
class Park; // 前向引用声明
// 汽车
class Automobile {
public:
Automobile(string number) :plateNumber(number) {}
Automobile() = default;
void enter(Park* park); // 汽车进入停车场
void leave(Park* park); // 汽车离开停车场
string getNumber() {
return this->plateNumber; // 得到车牌号
}
~Automobile() {}
protected:
virtual void pay(Park* park) = 0;
string plateNumber; // 车牌号
};
// 停车场
class Park {
public:
Park(int number) :spaceNum(number), parkNum(0), income(0) {
spaces = new Automobile * [number];
for (int i = 0; i < number; i++) { // 初始化对象指针,使其为nullptr
spaces[i] = nullptr;
}
}
bool allocate(Automobile* pa) { // 分配停车位
bool flag = false;
for (int i = 0; i < this->spaceNum; i++) {
if (spaces[i] == nullptr) {
spaces[i] = pa;
parkNum++;
cout << pa->getNumber() << "进入停车场,分配停车位" << endl;
flag = true;
break;
}
}
if (flag==false) {
cout << "无法为" << pa->getNumber() << "分配停车位" << endl;
}
return flag;
}
bool reclaim(Automobile* pa) { // 离开停车场
bool flag = false;
for (int i = 0; i < this->spaceNum; i++) {
if (spaces[i] == pa) {
spaces[i] = nullptr;
parkNum--;
flag = true;
break;
}
}
if (flag == false) {
cout << "停车场中没有车牌为" << pa->getNumber() << "的汽车" << endl;
}
return flag;
}
void charge(int fare) { // 停车场收停车费
this->income += fare;
}
void showInfo() { // 显示停车场目前信息
cout << "停车场目前停放了" << this->parkNum << "辆汽车";
if (this->parkNum != 0) {
cout << ":";
for (int i = 0; i < spaceNum; i++) {
if (spaces[i] != nullptr) {
cout << spaces[i]->getNumber() << ",";
}
}
} else {
cout << ",";
}
cout << "共收入" << this->income << "元停车费" << endl;
}
~Park() {
delete[] spaces;
}
private:
int spaceNum; // 停车位数量
int parkNum; // 已停车位数
int income; // 停车场总收入
Automobile** spaces; // 停车位用Automobile的指针数组表示
};
void Automobile::enter(Park* park) {
park->allocate(this);
}
void Automobile::leave(Park* park) {
park->reclaim(this);
this->pay(park);
}
// 卡车
class Truck :public Automobile {
public:
Truck(string number, float load) :Automobile(number), load(load), fare(3) {}
float getLoad() { // 得到载重量
return this->load;
}
int getFare() { // 得到停车费
return this->fare;
}
void pay(Park* park) { // 交停车费
cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl;
park->charge(this->fare);
}
~Truck() {}
private:
float load; // 载重量
int fare; // 停车费
};
// 轿车
class Car :public Automobile {
public:
Car(string number, string brand) :Automobile(number), brand(brand), fare(1) {}
string getBrand() { // 得到品牌
return this->brand;
}
int getFare() { // 得到停车费
return this->fare;
}
void pay(Park* park) { // 交停车费
cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl;
park->charge(this->fare);
}
~Car() {}
private:
string brand; // 品牌
int fare; // 停车费
};
// 公交车
class Bus :public Automobile {
public:
Bus(string number, int carry) :Automobile(number), carry(carry), fare(2) {}
int getCarry() { // 得到载员数量
return this->carry;
}
int getFare() { // 得到停车费
return this->fare;
}
void pay(Park* park) { // 交停车费
cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl;
park->charge(this->fare);
}
~Bus() {}
private:
int carry; // 载员数量
int fare; // 停车费
};
int main() {
int N;
cout << "请输入停车位数量:";
cin >> N;
Park park(N);
Automobile* auto1 = new Car("鲁B-12345", "奥迪A6"); // 创建轿车对象
Automobile* auto2 = new Truck("鲁B-23456", 15); // 创建卡车对象
Automobile* auto3 = new Bus("鲁B-34567", 50); // 创建公交车对象
Automobile* auto4 = new Car("鲁B-45678", "宝马320"); // 创建轿车对象
auto1->enter(&park); // car进入停车场,分配停车位
auto2->enter(&park); // truck进入停车场,分配车位
auto1->leave(&park); // car离开停车场,缴纳停车费
auto3->enter(&park); // bus进入停车场,分配车位
/*显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
park.showInfo();
auto4->enter(&park); // car进入停车场,分配停车位
// car进入停车场,分配停车位。因为没有空余停车位,所以无法分配
auto3->leave(&park); // bus离开停车场,缴纳停车费
auto2->leave(&park); // truck离开停车场,缴纳停车费
/*显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
park.showInfo();
delete auto1;
delete auto2;
delete auto3;
delete auto4;
return 0;
}
posted @   catting123  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示