@ -0,0 +1,39 @@
|
||||
#ifndef PROJECT_APPLICATION_H_
|
||||
#define PROJECT_APPLICATION_H_
|
||||
|
||||
#include "IUserManager.h"
|
||||
#include "IQuestionGenerator.h"
|
||||
#include "IFileManager.h"
|
||||
#include "QuestionChecker.h"
|
||||
#include "SessionManager.h"
|
||||
#include <string>
|
||||
|
||||
class Application {
|
||||
public:
|
||||
Application();
|
||||
~Application();
|
||||
|
||||
int Run();
|
||||
|
||||
private:
|
||||
void InitializeComponents();
|
||||
void DisplayWelcome() const;
|
||||
void HandleLogin();
|
||||
void DisplayMainMenu() const;
|
||||
void DisplayCurrentStatus() const;
|
||||
void HandleGenerateQuestions();
|
||||
void HandleSwitchType();
|
||||
void HandleUserChoice(const std::string& choice);
|
||||
void Cleanup();
|
||||
|
||||
bool IsNumber(const std::string& str) const;
|
||||
int StringToInt(const std::string& str) const;
|
||||
|
||||
IUserManager* user_manager_;
|
||||
IQuestionGenerator* question_generator_;
|
||||
IFileManager* file_manager_;
|
||||
QuestionChecker* question_checker_;
|
||||
SessionManager* session_manager_;
|
||||
};
|
||||
|
||||
#endif // PROJECT_APPLICATION_H_
|
||||
Binary file not shown.
@ -1,20 +1,24 @@
|
||||
#ifndef FILEMANAGER_H
|
||||
#define FILEMANAGER_H
|
||||
#ifndef PROJECT_FILEMANAGER_H_
|
||||
#define PROJECT_FILEMANAGER_H_
|
||||
|
||||
#include "IFileManager.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class FileManager : public IFileManager {
|
||||
public:
|
||||
FileManager();
|
||||
virtual ~FileManager() {}
|
||||
|
||||
virtual bool saveQuestions(const std::vector<std::string>& questions, const std::string& username, const UserType& type);
|
||||
virtual std::vector<std::string> loadExistingQuestions(const std::string& username);
|
||||
public:
|
||||
FileManager();
|
||||
virtual ~FileManager() {}
|
||||
|
||||
virtual bool SaveQuestions(const std::vector<std::string>& questions,
|
||||
const std::string& username,
|
||||
const UserType& type);
|
||||
|
||||
virtual std::vector<std::string> LoadExistingQuestions(
|
||||
const std::string& username);
|
||||
|
||||
private:
|
||||
std::string getCurrentTimestamp() const;
|
||||
private:
|
||||
std::string GetCurrentTimestamp() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Binary file not shown.
@ -1,15 +1,20 @@
|
||||
#ifndef IFILEMANAGER_H
|
||||
#define IFILEMANAGER_H
|
||||
#ifndef PROJECT_IFILEMANAGER_H_
|
||||
#define PROJECT_IFILEMANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "UserType.h"
|
||||
|
||||
class IFileManager {
|
||||
public:
|
||||
virtual ~IFileManager() {}
|
||||
virtual bool saveQuestions(const std::vector<std::string>& questions, const std::string& username, const UserType& type) = 0;
|
||||
virtual std::vector<std::string> loadExistingQuestions(const std::string& username) = 0;
|
||||
public:
|
||||
virtual ~IFileManager() {}
|
||||
|
||||
virtual bool SaveQuestions(const std::vector<std::string>& questions,
|
||||
const std::string& username,
|
||||
const UserType& type) = 0;
|
||||
|
||||
virtual std::vector<std::string> LoadExistingQuestions(
|
||||
const std::string& username) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -1,15 +1,19 @@
|
||||
#ifndef IQUESTIONGENERATOR_H
|
||||
#define IQUESTIONGENERATOR_H
|
||||
#ifndef PROJECT_IQUESTIONGENERATOR_H_
|
||||
#define PROJECT_IQUESTIONGENERATOR_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "UserType.h"
|
||||
|
||||
class IQuestionGenerator {
|
||||
public:
|
||||
virtual ~IQuestionGenerator() {}
|
||||
virtual std::vector<std::string> generateQuestions(const UserType& type, int count, const std::string& username) = 0;
|
||||
virtual void addExistingQuestions(const std::vector<std::string>& questions) = 0;
|
||||
public:
|
||||
virtual ~IQuestionGenerator() {}
|
||||
|
||||
virtual std::vector<std::string> GenerateQuestions(const UserType& type,
|
||||
int count,
|
||||
const std::string& username) = 0;
|
||||
|
||||
virtual void AddExistingQuestions(const std::vector<std::string>& questions) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -1,18 +1,20 @@
|
||||
#ifndef IUSERMANAGER_H
|
||||
#define IUSERMANAGER_H
|
||||
#ifndef PROJECT_IUSERMANAGER_H_
|
||||
#define PROJECT_IUSERMANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include "UserType.h"
|
||||
|
||||
class IUserManager {
|
||||
public:
|
||||
virtual ~IUserManager() {}
|
||||
|
||||
virtual bool authenticate(const std::string& username, const std::string& password) = 0;
|
||||
virtual UserType getCurrentUserType() const = 0;
|
||||
virtual std::string getCurrentUsername() const = 0;
|
||||
virtual void logout() = 0;
|
||||
virtual bool isLoggedIn() const = 0;
|
||||
public:
|
||||
virtual ~IUserManager() {}
|
||||
|
||||
virtual bool Authenticate(const std::string& username,
|
||||
const std::string& password) = 0;
|
||||
|
||||
virtual UserType GetCurrentUserType() const = 0;
|
||||
virtual std::string GetCurrentUsername() const = 0;
|
||||
virtual void Logout() = 0;
|
||||
virtual bool IsLoggedIn() const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Binary file not shown.
@ -1,26 +1,29 @@
|
||||
#ifndef QUESTIONGENERATOR_H
|
||||
#define QUESTIONGENERATOR_H
|
||||
#ifndef PROJECT_QUESTIONGENERATOR_H_
|
||||
#define PROJECT_QUESTIONGENERATOR_H_
|
||||
|
||||
#include "IQuestionGenerator.h"
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
class QuestionGenerator : public IQuestionGenerator {
|
||||
public:
|
||||
QuestionGenerator();
|
||||
virtual ~QuestionGenerator() {}
|
||||
|
||||
virtual std::vector<std::string> generateQuestions(const UserType& type, int count, const std::string& username);
|
||||
virtual void addExistingQuestions(const std::vector<std::string>& questions);
|
||||
public:
|
||||
QuestionGenerator();
|
||||
virtual ~QuestionGenerator() {}
|
||||
|
||||
virtual std::vector<std::string> GenerateQuestions(const UserType& type,
|
||||
int count,
|
||||
const std::string& username);
|
||||
|
||||
virtual void AddExistingQuestions(const std::vector<std::string>& questions);
|
||||
|
||||
private:
|
||||
std::string generatePrimaryQuestion();
|
||||
std::string generateJuniorQuestion();
|
||||
std::string generateSeniorQuestion();
|
||||
int getRandomNumber(int min, int max);
|
||||
char getRandomOperator();
|
||||
|
||||
std::set<std::string> existingQuestions;
|
||||
private:
|
||||
std::string GeneratePrimaryQuestion();
|
||||
std::string GenerateJuniorQuestion();
|
||||
std::string GenerateSeniorQuestion();
|
||||
int GetRandomNumber(int min, int max);
|
||||
char GetRandomOperator();
|
||||
|
||||
std::set<std::string> existing_questions_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Binary file not shown.
@ -1,30 +1,33 @@
|
||||
#ifndef SESSIONMANAGER_H
|
||||
#define SESSIONMANAGER_H
|
||||
#ifndef PROJECT_SESSIONMANAGER_H_
|
||||
#define PROJECT_SESSIONMANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "UserType.h"
|
||||
#include "IUserManager.h"
|
||||
#include "IQuestionGenerator.h"
|
||||
#include "QuestionChecker.h"
|
||||
|
||||
class SessionManager {
|
||||
public:
|
||||
SessionManager(IUserManager* user_manager, IQuestionGenerator* question_generator, QuestionChecker* question_checker);
|
||||
|
||||
bool switchUserType(const std::string& target_type);
|
||||
UserType getCurrentUserType() const;
|
||||
std::string getCurrentUsername() const;
|
||||
bool isLoggedIn() const;
|
||||
bool login(const std::string& username, const std::string& password);
|
||||
void logout();
|
||||
std::vector<std::string> generateQuestions(int count);
|
||||
public:
|
||||
SessionManager(IUserManager* user_manager,
|
||||
IQuestionGenerator* question_generator,
|
||||
QuestionChecker* question_checker);
|
||||
|
||||
bool SwitchUserType(const std::string& target_type);
|
||||
UserType GetCurrentUserType() const;
|
||||
std::string GetCurrentUsername() const;
|
||||
bool IsLoggedIn() const;
|
||||
bool Login(const std::string& username, const std::string& password);
|
||||
void Logout();
|
||||
std::vector<std::string> GenerateQuestions(int count);
|
||||
|
||||
private:
|
||||
IUserManager* user_manager_;
|
||||
IQuestionGenerator* question_generator_;
|
||||
QuestionChecker* question_checker_;
|
||||
UserType current_type_;
|
||||
bool type_switched_;
|
||||
private:
|
||||
IUserManager* user_manager_;
|
||||
IQuestionGenerator* question_generator_;
|
||||
QuestionChecker* question_checker_;
|
||||
UserType current_type_;
|
||||
bool type_switched_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Binary file not shown.
@ -1,8 +1,8 @@
|
||||
#include "User.h"
|
||||
|
||||
User::User(const std::string& username, const std::string& password, const UserType& type)
|
||||
: username(username), password(password), type(type) {}
|
||||
: username_(username), password_(password), type_(type) {}
|
||||
|
||||
bool User::authenticate(const std::string& inputPassword) const {
|
||||
return password == inputPassword;
|
||||
bool User::Authenticate(const std::string& input_password) const {
|
||||
return password_ == input_password;
|
||||
}
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
#ifndef USER_H
|
||||
#define USER_H
|
||||
#ifndef PROJECT_USER_H_
|
||||
#define PROJECT_USER_H_
|
||||
|
||||
#include <string>
|
||||
#include "UserType.h"
|
||||
|
||||
class User {
|
||||
public:
|
||||
User(const std::string& username, const std::string& password, const UserType& type);
|
||||
|
||||
std::string getUsername() const { return username; }
|
||||
std::string getPassword() const { return password; }
|
||||
UserType getType() const { return type; }
|
||||
|
||||
bool authenticate(const std::string& inputPassword) const;
|
||||
public:
|
||||
User(const std::string& username, const std::string& password, const UserType& type);
|
||||
|
||||
std::string GetUsername() const { return username_; }
|
||||
std::string GetPassword() const { return password_; }
|
||||
UserType GetType() const { return type_; }
|
||||
|
||||
bool Authenticate(const std::string& input_password) const;
|
||||
|
||||
private:
|
||||
std::string username;
|
||||
std::string password;
|
||||
UserType type;
|
||||
private:
|
||||
std::string username_;
|
||||
std::string password_;
|
||||
UserType type_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // PROJECT_USER_H_
|
||||
|
||||
Binary file not shown.
@ -1,53 +1,54 @@
|
||||
#include "UserManager.h"
|
||||
#include <iostream>
|
||||
|
||||
UserManager::UserManager() : currentUser(NULL), loggedIn(false) {
|
||||
initializeUsers();
|
||||
UserManager::UserManager() : current_user_(NULL), logged_in_(false) {
|
||||
InitializeUsers();
|
||||
}
|
||||
|
||||
void UserManager::initializeUsers() {
|
||||
presetUsers.push_back(User("张三1", "123", UserType::PRIMARY));
|
||||
presetUsers.push_back(User("张三2", "123", UserType::PRIMARY));
|
||||
presetUsers.push_back(User("张三3", "123", UserType::PRIMARY));
|
||||
presetUsers.push_back(User("李四1", "123", UserType::JUNIOR));
|
||||
presetUsers.push_back(User("李四2", "123", UserType::JUNIOR));
|
||||
presetUsers.push_back(User("李四3", "123", UserType::JUNIOR));
|
||||
presetUsers.push_back(User("王五1", "123", UserType::SENIOR));
|
||||
presetUsers.push_back(User("王五2", "123", UserType::SENIOR));
|
||||
presetUsers.push_back(User("王五3", "123", UserType::SENIOR));
|
||||
void UserManager::InitializeUsers() {
|
||||
preset_users_.push_back(User("张三1", "123", UserType::kPrimary));
|
||||
preset_users_.push_back(User("张三2", "123", UserType::kPrimary));
|
||||
preset_users_.push_back(User("张三3", "123", UserType::kPrimary));
|
||||
preset_users_.push_back(User("李四1", "123", UserType::kJunior));
|
||||
preset_users_.push_back(User("李四2", "123", UserType::kJunior));
|
||||
preset_users_.push_back(User("李四3", "123", UserType::kJunior));
|
||||
preset_users_.push_back(User("王五1", "123", UserType::kSenior));
|
||||
preset_users_.push_back(User("王五2", "123", UserType::kSenior));
|
||||
preset_users_.push_back(User("王五3", "123", UserType::kSenior));
|
||||
}
|
||||
|
||||
bool UserManager::authenticate(const std::string& username, const std::string& password) {
|
||||
for (size_t i = 0; i < presetUsers.size(); i++) {
|
||||
if (presetUsers[i].getUsername() == username &&
|
||||
presetUsers[i].authenticate(password)) {
|
||||
currentUser = &presetUsers[i];
|
||||
loggedIn = true;
|
||||
return true;
|
||||
}
|
||||
bool UserManager::Authenticate(const std::string& username, const std::string& password) {
|
||||
// 使用传统迭代器替代 range-based for
|
||||
for (std::vector<User>::iterator it = preset_users_.begin();
|
||||
it != preset_users_.end(); ++it) {
|
||||
if (it->GetUsername() == username && it->Authenticate(password)) {
|
||||
current_user_ = &(*it); // 获取指针
|
||||
logged_in_ = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
UserType UserManager::getCurrentUserType() const {
|
||||
if (currentUser) {
|
||||
return currentUser->getType();
|
||||
}
|
||||
return UserType::PRIMARY;
|
||||
UserType UserManager::GetCurrentUserType() const {
|
||||
if (current_user_ != NULL) { // 使用 NULL 替代 nullptr
|
||||
return current_user_->GetType();
|
||||
}
|
||||
return UserType::kPrimary;
|
||||
}
|
||||
|
||||
std::string UserManager::getCurrentUsername() const {
|
||||
if (currentUser) {
|
||||
return currentUser->getUsername();
|
||||
}
|
||||
return "";
|
||||
std::string UserManager::GetCurrentUsername() const {
|
||||
if (current_user_ != NULL) { // 使用 NULL 替代 nullptr
|
||||
return current_user_->GetUsername();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void UserManager::logout() {
|
||||
currentUser = NULL;
|
||||
loggedIn = false;
|
||||
void UserManager::Logout() {
|
||||
current_user_ = NULL; // 使用 NULL 替代 nullptr
|
||||
logged_in_ = false;
|
||||
}
|
||||
|
||||
bool UserManager::isLoggedIn() const {
|
||||
return loggedIn;
|
||||
bool UserManager::IsLoggedIn() const {
|
||||
return logged_in_;
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
#ifndef USERMANAGER_H
|
||||
#define USERMANAGER_H
|
||||
#ifndef PROJECT_USERMANAGER_H_
|
||||
#define PROJECT_USERMANAGER_H_
|
||||
|
||||
#include "IUserManager.h"
|
||||
#include <vector>
|
||||
#include "User.h"
|
||||
|
||||
class UserManager : public IUserManager {
|
||||
public:
|
||||
UserManager();
|
||||
virtual ~UserManager() {}
|
||||
|
||||
virtual bool authenticate(const std::string& username, const std::string& password);
|
||||
virtual UserType getCurrentUserType() const;
|
||||
virtual std::string getCurrentUsername() const;
|
||||
virtual void logout();
|
||||
virtual bool isLoggedIn() const;
|
||||
public:
|
||||
UserManager();
|
||||
virtual ~UserManager() {}
|
||||
|
||||
virtual bool Authenticate(const std::string& username, const std::string& password);
|
||||
virtual UserType GetCurrentUserType() const;
|
||||
virtual std::string GetCurrentUsername() const;
|
||||
virtual void Logout();
|
||||
virtual bool IsLoggedIn() const;
|
||||
|
||||
private:
|
||||
std::vector<User> presetUsers;
|
||||
const User* currentUser;
|
||||
bool loggedIn;
|
||||
|
||||
void initializeUsers();
|
||||
private:
|
||||
void InitializeUsers();
|
||||
|
||||
std::vector<User> preset_users_;
|
||||
const User* current_user_;
|
||||
bool logged_in_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // PROJECT_USERMANAGER_H_
|
||||
|
||||
Binary file not shown.
@ -1,5 +1,5 @@
|
||||
#include "UserType.h"
|
||||
|
||||
const UserType UserType::PRIMARY(0, "小学");
|
||||
const UserType UserType::JUNIOR(1, "初中");
|
||||
const UserType UserType::SENIOR(2, "高中");
|
||||
const UserType UserType::kPrimary(0, "小学");
|
||||
const UserType UserType::kJunior(1, "初中");
|
||||
const UserType UserType::kSenior(2, "高中");
|
||||
|
||||
@ -1,28 +1,28 @@
|
||||
#ifndef USERTYPE_H
|
||||
#define USERTYPE_H
|
||||
#ifndef PROJECT_USERTYPE_H_
|
||||
#define PROJECT_USERTYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class UserType {
|
||||
public:
|
||||
static const UserType PRIMARY;
|
||||
static const UserType JUNIOR;
|
||||
static const UserType SENIOR;
|
||||
|
||||
UserType() : value(-1), name("δ֪") {} // Ìí¼ÓĬÈϹ¹Ô캯Êý
|
||||
UserType(const UserType& other) : value(other.value), name(other.name) {}
|
||||
|
||||
std::string getName() const { return name; }
|
||||
int getValue() const { return value; }
|
||||
|
||||
bool operator==(const UserType& other) const { return value == other.value; }
|
||||
bool operator!=(const UserType& other) const { return value != other.value; }
|
||||
|
||||
private:
|
||||
UserType(int val, const std::string& n) : value(val), name(n) {}
|
||||
|
||||
int value;
|
||||
std::string name;
|
||||
public:
|
||||
static const UserType kPrimary;
|
||||
static const UserType kJunior;
|
||||
static const UserType kSenior;
|
||||
|
||||
UserType() : value_(-1), name_("Unknown") {}
|
||||
UserType(const UserType& other) : value_(other.value_), name_(other.name_) {}
|
||||
|
||||
std::string GetName() const { return name_; }
|
||||
int GetValue() const { return value_; }
|
||||
|
||||
bool operator==(const UserType& other) const { return value_ == other.value_; }
|
||||
bool operator!=(const UserType& other) const { return value_ != other.value_; }
|
||||
|
||||
private:
|
||||
UserType(int val, const std::string& n) : value_(val), name_(n) {}
|
||||
|
||||
int value_;
|
||||
std::string name_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue