给代码加入用户密码功能,使用户注册时需要设置密码,登陆时需要输入密码,写出完整代码并且可以运行
时间: 2024-09-26 16:10:26 浏览: 29
为了实现用户密码的功能,我们需要在 `User` 类中增加一个私有成员变量来存储密码,并且提供相应的 setter 和 getter 方法来处理密码的设置和获取。此外,在 `BikeRentalSystem` 类中的用户注册和登录方法也需要进行调整以支持密码验证。
以下是修改后的代码:
```cpp
#include <iostream>
#include <fstream>
#include< string >
#include <vector>
// 定义用户类
class User {
private:
std::string username;
std::string gender;
int age;
std::string idNumber;
std::string phoneNumber;
std::string occupation;
std::string address;
double accountBalance;
std::string password;
public:
// 构造函数
User(std::string username, std::string gender, int age, std::string idNumber,
std::string phoneNumber, std::string occupation, std::string address,
double accountBalance, std::string password)
: username(username), gender(gender), age(age), idNumber(idNumber),
phoneNumber(phoneNumber), occupation(occupation), address(address),
accountBalance(accountBalance), password(password) {}
// 获取用户名
std::string getUsername() const { return username; }
// 获取性别
std::string getGender() const { return gender; }
// 获取年龄
int getAge() const { return age; }
// 获取身份证号码
std::string getIdNumber() const { return idNumber; }
// 获取手机号码
std::string getPhoneNumber() const { return phoneNumber; }
// 获取职业
std::string getOccupation() const { return occupation; }
// 获取家庭住址
std::string getAddress() const { return address; }
// 获取账户余额
double getAccountBalance() const { return accountBalance; }
// 获取密码
std::string getPassword() const { return password; }
// 充值
void recharge(double amount) { accountBalance += amount; }
// 扣除金额
void deduct(double amount) { accountBalance -= amount; }
};
// 定义自行车类
class Bike {
private:
std::string bikeName;
std::string parkingLocation;
public:
// 构造函数
Bike(std::string bikeName, std::string parkingLocation)
: bikeName(bikeName), parkingLocation(parkingLocation) {}
// 获取自行车名称
std::string getBikeName() const { return bikeName; }
// 获取停车位置
std::string getParkingLocation() const { return parkingLocation; }
// 设置停车位置
void setParkingLocation(std::string location) { parkingLocation = location; }
};
// 定义自行车租赁管理系统类
class BikeRentalSystem {
private:
std::vector<User> users;
std::vector<Bike> bikes;
std::string userFile;
std::string bikeFile;
public:
// 构造函数
BikeRentalSystem(std::string userFile, std::string bikeFile)
: userFile(userFile), bikeFile(bikeFile) {}
// 添加用户
void addUser(User user) { users.push_back(user); }
// 删除用户
void deleteUser(std::string username) {
for (auto it = users.begin(); it != users.end(); ++it) {
if (it->getUsername() == username) {
users.erase(it);
break;
}
}
}
// 修改用户信息
void modifyUser(User user) {
for (auto& u : users) {
if (u.getUsername() == user.getUsername()) {
u = user;
break;
}
}
}
// 查找用户
User* findUser(std::string username) {
for (auto& u : users) {
if (u.getUsername() == username) {
return &u;
}
}
return nullptr;
}
// 添加自行车
void addBike(Bike bike) { bikes.push_back(bike); }
// 删除自行车
void deleteBike(std::string bikeName) {
for (auto it = bikes.begin(); it != bikes.end(); ++it) {
if (it->getBikeName() == bikeName) {
bikes.erase(it);
break;
}
}
}
// 修改自行车信息
void modifyBike(Bike bike) {
for (auto& b : bikes) {
if (b.getBikeName() == bike.getBikeName()) {
b = bike;
break;
}
}
}
// 查找自行车
Bike* findBike(std::string bikeName) {
for (auto& b : bikes) {
if (b.getBikeName() == bikeName) {
return &b;
}
}
return nullptr;
}
// 自行车借出
void rentBike(std::string username, std::string bikeName) {
User* user = findUser(username);
Bike* bike = findBike(bikeName);
if (user && bike && bike->getParkingLocation() != "") {
user->deduct(10.0); // 扣除租金
bike->setParkingLocation(""); // 更新自行车停车位置为空
std::cout << "租车成功!" << std::endl;
} else {
std::cout << "租车失败!" << std::endl;
}
}
// 自行车归还
void returnBike(std::string username, std::string bikeName) {
User* user = findUser(username);
Bike* bike = findBike(bikeName);
if (user && bike && bike->getParkingLocation() == "") {
user->recharge(5.0); // 退还押金
bike->setParkingLocation("停车点"); // 更新自行车停车位置
std::cout << "还车成功!" << std::endl;
} else {
std::cout << "还车失败!" << std::endl;
}
}
// 显示用户信息
void displayUserInfo(std::string username) {
User* user = findUser(username);
if (user) {
std::cout << "用户名:" << user->getUsername() << std::endl;
std::cout << "性别:" << user->getGender() << std::endl;
std::cout << "年龄:" << user->getAge() << std::endl;
std::cout << "身份证号码:" << user->getIdNumber() << std::endl;
std::cout << "手机号码:" << user->getPhoneNumber() << std::endl;
std::cout << "职业:" << user->getOccupation() << std::endl;
std::cout << "家庭住址:" << user->getAddress() << std::endl;
std::cout << "账户余额:" << user->getAccountBalance() << std::endl;
} else {
std::cout << "用户不存在!" << std::endl;
}
}
// 显示自行车信息
void displayBikeInfo(std::string bikeName) {
Bike* bike = findBike(bikeName);
if (bike) {
std::cout << "自行车名称:" << bike->getBikeName() << std::endl;
std::cout << "停车位置:" << bike->getParkingLocation() << std::endl;
} else {
std::cout << "自行车不存在!" << std::endl;
}
}
// 添加 getBikes 函数
std::vector<Bike>& getBikes() { return bikes; }
};
int main() {
BikeRentalSystem system("users.txt", "bikes.txt");
int choice;
std::string username, password, bikeName;
do {
std::cout << "自行车租赁管理系统" << std::endl;
std::cout << "1. 用户注册" << std::endl;
std::cout << "2. 用户登录" << std::endl;
std::cout << "3. 管理员登录" << std::endl;
std::cout << "4. 退出" << std::endl;
std::cout << "请选择操作:";
std::cin >> choice;
switch (choice) {
case 1: {
std::string username, gender, idNumber, phoneNumber, occupation, address, password;
int age;
double accountBalance;
std::cout << "请输入用户名:";
std::cin >> username;
std::cout << "请输入密码:";
std::cin >> password;
std::cout << "请输入性别:";
std::cin >> gender;
std::cout << "请输入年龄:";
std::cin >> age;
std::cout << "请输入身份证号码:";
std::cin >> idNumber;
std::cout << "请输入手机号码:";
std::cin >> phoneNumber;
std::cout << "请输入职业:";
std::cin >> occupation;
std::cout << "请输入家庭住址:";
std::cin >> address;
std::cout << "请输入初始账户余额:";
std::cin >> accountBalance;
User user(username, gender, age, idNumber, phoneNumber, occupation, address, accountBalance, password);
system.addUser(user);
std::cout << "用户注册成功!" << std::endl;
break;
}
case 2: {
std::cout << "请输入用户名:";
std::cin >> username;
std::cout << "请输入密码:";
std::cin >> password;
User* user = system.findUser(username);
if (user && user->getPassword() == password) {
std::cout << "用户登录成功!" << std::endl;
int subChoice;
do {
std::cout << "1. 个人信息维护" << std::endl;
std::cout << "2. 车辆借出" << std::endl;
std::cout << "3. 车辆归还" << std::endl;
std::cout << "4. 充值" << std::endl;
std::cout << "5. 查看个人信息" << std::endl;
std::cout << "6. 查看车辆信息" << std::endl;
std::cout << "7. 退出" << std::endl;
std::cout << "请选择操作:";
std::cin >> subChoice;
switch (subChoice) {
case 1: {
std::string gender, idNumber, phoneNumber, occupation, address, password;
int age;
std::cout << "请输入性别:";
std::cin >> gender;
std::cout << "请输入年龄:";
std::cin >> age;
std::cout << "请输入身份证号码:";
std::cin >> idNumber;
std::cout << "请输入手机号码:";
std::cin >> phoneNumber;
std::cout << "请输入职业:";
std::cin >> occupation;
std::cout << "请输入家庭住址:";
std::cin >> address;
std::cout << "请输入新密码:";
std::cin >> password;
User updatedUser(username, gender, age, idNumber, phoneNumber, occupation, address, user->getAccountBalance(), password);
system.modifyUser(updatedUser);
std::cout << "个人信息维护成功!" << std::endl;
break;
}
case 2: {
std::cout << "请输入要借出的自行车名称:";
std::cin >> bikeName;
system.rentBike(username, bikeName);
break;
}
case 3: {
std::cout << "请输入要归还的自行车名称:";
std::cin >> bikeName;
system.returnBike(username, bikeName);
break;
}
case 4: {
double amount;
std::cout << "请输入充值金额:";
std::cin >> amount;
user->recharge(amount);
std::cout << "充值成功!" << std::endl;
break;
}
case 5: {
system.displayUserInfo(username);
break;
}
case 6: {
system.displayBikeInfo(bikeName);
break;
}
case 7: {
std::cout << "退出" << std::endl;
break;
}
default: {
std::cout << "无效的选择,请重新输入!" << std::endl;
break;
}
}
} while (subChoice != 7);
} else {
std::cout << "用户不存在或密码错误!" << std::endl;
}
break;
}
case 3: {
std::string adminUsername, adminPassword;
std::cout << "请输入管理员用户名:";
std::cin >> adminUsername;
std::cout << "请输入管理员密码:";
std::cin >> adminPassword;
if (adminUsername == "admin" && adminPassword == "admin") {
std::cout << "管理员登录成功!" << std::endl;
int subChoice;
do {
std::cout << "1. 用户管理" << std::endl;
std::cout << "2. 自行车管理" << std::endl;
std::cout << "3. 营收管理" << std::endl;
std::cout << "4. 退出" << std::endl;
std::cout << "请选择操作:";
std::cin >> subChoice;
switch (subChoice) {
case 1: {
int userChoice;
std::cout << "1. 添加用户" << std::endl;
std::cout << "2. 删除用户" << std::endl;
std::cout << "3. 修改用户信息" << std::endl;
std::cout << "4. 查找用户" << std::endl;
std::cout << "5. 退出" << std::endl;
std::cout << "请选择操作:";
std::cin >> userChoice;
switch (userChoice) {
case 1: {
std::string username, gender, idNumber, phoneNumber, occupation, address, password;
int age;
double accountBalance;
std::cout << "请输入用户名:";
std::cin >> username
阅读全文