依据提供的类写一个无人售货机系统
时间: 2024-11-15 10:31:52 浏览: 0
无人售货机Echarts数据分析
基于提供的类和结构,我们可以构建一个完整的无人售货机系统。以下是一个示例实现:
### 1. 主程序入口 (`main.cpp`)
```cpp
#include <iostream>
#include <vector>
#include "AbstractVendingMachine.h"
#include "Item.h"
#include "Cart.h"
#include "WechatPayment.h"
#include "AlipayPayment.h"
#include "ConstantTemperature.h"
#include "Server.h"
#include "CustomerClient.h"
#include "MotorClient.h"
#include "DryerTemperatureClient.h"
int main() {
// 创建商品列表
std::vector<Item> items = {
{"Coke", 2.5, 10},
{"Water", 1.5, 20},
{"Chips", 1.0, 15}
};
// 创建客户购物客户端
CustomerClient customerClient;
// 显示商品列表
customerClient.displayItems(items);
// 添加商品到购物车
customerClient.addItemAtClient(items[0]);
customerClient.addItemAtClient(items[1]);
// 显示购物车内容
customerClient.displayCartContent();
// 设置支付方式(微信支付)
WechatPayment wechatPayment;
customerClient.setPaymentMethod(&wechatPayment);
// 处理支付
customerClient.processPayment();
// 创建服务器对象
Server server;
// 结束订单
server.endOrder(customerClient.cart);
// 创建电机客户端
MotorClient motorClient;
motorClient.startMotorAtClient();
motorClient.displayMotorStatus();
motorClient.stopMotorAtClient();
motorClient.displayMotorStatus();
// 创建除霜恒温客户端
DryerTemperatureClient dryerTempClient;
dryerTempClient.monitorTemperatureAndAdjust();
return 0;
}
```
### 2. 类定义文件
#### `AbstractVendingMachine.h`
```cpp
#ifndef ABSTRACT_VENDING_MACHINE_H
#define ABSTRACT_VENDING_MACHINE_H
#include <iostream>
class AbstractVendingMachine {
protected:
int compartments;
int compartmentCapacity;
public:
virtual void setCompartments(int num) = 0;
virtual void setCompartmentCapacity(int capacity) = 0;
virtual void displayCompartments() const = 0;
};
#endif // ABSTRACT_VENDING_MACHINE_H
```
#### `Item.h`
```cpp
#ifndef ITEM_H
#define ITEM_H
#include <iostream>
#include <string>
class Item {
private:
std::string name;
double price;
int quantity;
public:
Item(const std::string& name, double price, int quantity)
: name(name), price(price), quantity(quantity) {}
void displayItemInfo() const {
std::cout << "Name: " << name << ", Price: " << price << ", Quantity: " << quantity << std::endl;
}
void increaseQuantity(int amount) { quantity += amount; }
bool decreaseQuantity(int amount) {
if (quantity >= amount) {
quantity -= amount;
return true;
}
return false;
}
void removeItem() { quantity = 0; }
};
#endif // ITEM_H
```
#### `Cart.h`
```cpp
#ifndef CART_H
#define CART_H
#include <vector>
#include "Item.h"
class Cart {
private:
std::vector<Item> items;
public:
void displayCartContent() const {
for (const auto& item : items) {
item.displayItemInfo();
}
}
void addItem(const Item& item) { items.push_back(item); }
bool deleteItem(const std::string& itemName) {
for (auto it = items.begin(); it != items.end(); ++it) {
if (it->name == itemName) {
items.erase(it);
return true;
}
}
return false;
}
double calculateTotalPrice() const {
double total = 0.0;
for (const auto& item : items) {
total += item.price * item.quantity;
}
return total;
}
void clearCart() { items.clear(); }
};
#endif // CART_H
```
#### `Payment.h`
```cpp
#ifndef PAYMENT_H
#define PAYMENT_H
#include <iostream>
#include <string>
class Payment {
protected:
std::string paymentMethod;
double paymentAmount;
public:
virtual void pay() = 0;
virtual void showQRCode() = 0;
};
#endif // PAYMENT_H
```
#### `WechatPayment.h`
```cpp
#ifndef WECHAT_PAYMENT_H
#define WECHAT_PAYMENT_H
#include "Payment.h"
class WechatPayment : public Payment {
public:
void pay() override {
std::cout << "Paying with WeChat" << std::endl;
}
void showQRCode() override {
std::cout << "Displaying WeChat QR Code" << std::endl;
}
};
#endif // WECHAT_PAYMENT_H
```
#### `AlipayPayment.h`
```cpp
#ifndef ALIPAY_PAYMENT_H
#define ALIPAY_PAYMENT_H
#include "Payment.h"
class AlipayPayment : public Payment {
public:
void pay() override {
std::cout << "Paying with Alipay" << std::endl;
}
void showQRCode() override {
std::cout << "Displaying Alipay QR Code" << std::endl;
}
};
#endif // ALIPAY_PAYMENT_H
```
#### `Dryer.h`
```cpp
#ifndef DRYER_H
#define DRYER_H
#include <iostream>
class Dryer {
private:
int monitoringTime;
bool monitoringResult;
public:
void monitor() {
std::cout << "Monitoring frost..." << std::endl;
}
void displayMonitoringResult() const {
std::cout << "Monitoring Result: " << (monitoringResult ? "Frost Detected" : "No Frost") << std::endl;
}
};
#endif // DRYER_H
```
#### `ConstantTemperature.h`
```cpp
#ifndef CONSTANT_TEMPERATURE_H
#define CONSTANT_TEMPERATURE_H
#include "Dryer.h"
class ConstantTemperature : public Dryer {
public:
void getMonitoringResult() {
monitoringResult = true; // 示例代码
}
void adjustTemperature() {
if (monitoringResult) {
std::cout << "Heating up..." << std::endl;
} else {
std::cout << "Cooling down..." << std::endl;
}
}
};
#endif // CONSTANT_TEMPERATURE_H
```
#### `Motor.h`
```cpp
#ifndef MOTOR_H
#define MOTOR_H
#include <iostream>
#include <string>
class Motor {
private:
bool status;
int speed;
int rpm;
std::string direction;
std::string brand;
std::string model;
int power;
public:
void startMotor() {
status = true;
std::cout << "Motor started" << std::endl;
}
void stopMotor() {
status = false;
std::cout << "Motor stopped" << std::endl;
}
void setParameters(int speed, int rpm, const std::string& direction, const std::string& brand, const std::string& model, int power) {
this->speed = speed;
this->rpm = rpm;
this->direction = direction;
this->brand = brand;
this->model = model;
this->power = power;
}
};
#endif // MOTOR_H
```
#### `Server.h`
```cpp
#ifndef SERVER_H
#define SERVER_H
#include "Cart.h"
class Server {
public:
void endOrder(const Cart& cart) {
std::cout << "Order completed. Dispensing items..." << std::endl;
}
void analyzeMonitoringData(bool result) {
if (result) {
std::cout << "Adjusting temperature to maintain constant environment" << std::endl;
}
}
void monitorVendingMachineStatus() {
std::cout << "Monitoring vending machine status" << std::endl;
}
void storeCustomerInformation(const std::string& customerInfo) {
std::cout << "Storing customer information: " << customerInfo << std::endl;
}
};
#endif // SERVER_H
```
#### `CustomerClient.h`
```cpp
#ifndef CUSTOMER_CLIENT_H
#define CUSTOMER_CLIENT_H
#include "Cart.h"
#include "Payment.h"
#include "Item.h"
class CustomerClient {
private:
Cart cart;
Payment* payment;
public:
void displayItems(const std::vector<Item>& items) {
for (const auto& item : items) {
item.displayItemInfo();
}
}
void displayCartContent() {
cart.displayCartContent();
}
void displayPaymentInfo(double amount) {
std::cout << "Total Amount: " << amount << std::endl;
}
void addItemAtClient(const Item& item) {
cart.addItem(item);
}
void deleteItemAtClient(const std::string& itemName) {
cart.deleteItem(itemName);
}
void setPaymentMethod(Payment* method) {
payment = method;
}
void processPayment() {
payment->pay();
payment->showQRCode();
}
};
#endif // CUSTOMER_CLIENT_H
```
#### `MotorClient.h`
```cpp
#ifndef MOTOR_CLIENT_H
#define MOTOR_CLIENT_H
#include "Motor.h"
class MotorClient {
private:
Motor motor;
public:
void displayMotorStatus() {
std::cout << "Motor Status: " << (motor.status ? "Running" : "Stopped") << std::endl;
}
void startMotorAtClient() {
motor.startMotor();
}
void stopMotorAtClient() {
motor.stopMotor();
}
void setMotorParametersAtClient(int speed, int rpm, const std::string& direction, const std::string& brand, const std::string& model, int power) {
motor.setParameters(speed, rpm, direction, brand, model, power);
}
};
#endif // MOTOR_CLIENT_H
```
#### `DryerTemperatureClient.h`
```cpp
#ifndef DRYER_TEMPERATURE_CLIENT_H
#define DRYER_TEMPERATURE_CLIENT_H
#include "ConstantTemperature.h"
class DryerTemperatureClient {
private:
ConstantTemperature ct;
public:
void monitorTemperatureAndAdjust() {
ct.getMonitoringResult();
ct.adjustTemperature();
}
};
#endif // DRYER_TEMPERATURE_CLIENT_H
```
### 3. 编译和运行
确保所有头文件和源文件都在同一个目录下,然后使用以下命令编译和运行程序:
```sh
g++ -o vending_machine main.cpp
./vending_machine
```
这个示例实现了基本的无人售货机系统,包括商品展示、购物车管理、支付处理、电机控制和环境监控等功能。你可以根据实际需求进一步扩展和完善这个系统。
阅读全文