药房管理系统涉及到药品管理、库存管理、销售管理等多个模块。其中类设计如下(使用C++语言):
1. 药品管理模块——药品类(Drug)的定义:
```cpp
class Drug {
private:
string name;
int quantity;
double price;
public:
Drug(string n, int q, double p) {
name = n;
quantity = q;
price = p;
}
// Getter and setter methods
string getName() {
return name;
}
int getQuantity() {
return quantity;
}
double getPrice() {
return price;
}
void setQuantity(int q) {
quantity = q;
}
};
```
2. 药品库存管理模块的示例代码:
```cpp
class Inventory {
private:
vector
public:
void addDrug(Drug drug) {
drugs.push_back(drug);
}
void updateDrugQuantity(string name, int quantity) {
for (int i = 0; i < drugs.size(); i++) {
if (drugs[i].getName() == name) {
drugs[i].setQuantity(quantity);
break;
}
}
}
void displayInventory() {
for (int i = 0; i < drugs.size(); i++) {
cout << "Name: " << drugs[i].getName() << ", Quantity: " << drugs[i].getQuantity() << ", Price: " << drugs[i].getPrice() << endl;
}
}
};
```
3. 销售管理模块的示例代码:
```cpp
class Sales {
private:
vector
public:
void sellDrug(Drug drug, int quantity) {
if (drug.getQuantity() >= quantity) {
drug.setQuantity(drug.getQuantity() - quantity);
soldDrugs.push_back(drug);
cout << "Drug sold successfully." << endl;
} else {
cout << "Insufficient quantity in stock." << endl;
}
}
void displaySales() {
for (int i = 0; i < soldDrugs.size(); i++) {
cout << "Name: " << soldDrugs[i].getName() << ", Quantity: " << soldDrugs[i].getQuantity() << ", Price: " << soldDrugs[i].getPrice() << endl;
}
}
};
```
4.数据库创建和连接代码:
```cpp
#include
#include
int main() {
// 创建数据库连接
MYSQL* conn = mysql_init(NULL);
// 连接数据库
if (!mysql_real_connect(conn, "localhost", "root", "password", "pharmacy", 0, NULL, 0)) {
std::cout << "数据库连接失败: " << mysql_error(conn) << std::endl;
return 1;
}
// 创建药品表
std::string createTableQuery = "CREATE TABLE drugs ("
"id INT PRIMARY KEY AUTO_INCREMENT,"
"name VARCHAR(100) NOT NULL,"
"quantity INT NOT NULL,"
"price FLOAT NOT NULL"
")";
if (mysql_query(conn, createTableQuery.c_str())) {
std::cout << "创建药品表失败: " << mysql_error(conn) << std::endl;
return 1;
}
// 关闭数据库连接
mysql_close(conn);
std::cout << "数据库连接已关闭" << std::endl;
return 0;
}
```
上述代码创建一个名为"pharmacy"的数据库,其中包含一个名为"drugs"的药品表。你需要将"localhost"、"root"和"password"替换为你自己的MySQL服务器地址、用户名和密码。
5.用户管理模块代码:
```cpp
#include
#include
using namespace std;
// 用户类
class User {
private:
string username;
string password;
public:
User(string username, string password) {
this->username = username;
this->password = password;
}
string getUsername() {
return username;
}
bool checkPassword(string password) {
return this->password == password;
}
};
// 用户管理类
class UserManager {
private:
vector
public:
void addUser(User user) {
users.push_back(user);
}
bool loginUser(string username, string password) {
for (int i = 0; i < users.size(); i++) {
if (users[i].getUsername() == username && users[i].checkPassword(password)) {
return true;
}
}
return false;
}
};
int main() {
UserManager userManager;
// 添加用户
User user1("admin", "admin123");
User user2("guest", "guest123");
userManager.addUser(user1);
userManager.addUser(user2);
// 用户登录
string username, password;
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
if (userManager.loginUser(username, password)) {
cout << "登录成功!" << endl;
} else {
cout << "登录失败!" << endl;
}
return 0;
}
```
代码中包括了一个User类和一个UserManager类。User类表示用户,包含用户名和密码属性,以及检查密码的方法。UserManager类表示用户管理,包含添加用户和登录验证的方法。
在main函数中,首先创建一个UserManager对象,并添加两个用户。然后通过用户输入的用户名和密码,调用UserManager的loginUser方法进行登录验证,如果登录成功,则输出"登录成功",否则输出"登录失败"。