C++ 小练手

本文档描述了一个诊所信息管理系统的实现,包括管理员和患者两种角色的功能,如添加用户、重置密码、添加医生信息、门诊记录及费用统计等。系统采用C++编程,包含Clinic、Doctor和Patient三个类,实现了用户登录、账户管理、门诊记录的增删查改等功能。
摘要由CSDN通过智能技术生成

诊所信息管理系统

项目简介

为一个诊所设计一个信息管理系统。
设计医生和患者两种角色,在此基础上,通过增加患者和账单,使他们共用于表示一家诊所的信息管理。

功能要求

管理员用户名: admin
管理员密码: 123456

管理员:

  1. 添加患者的用户名和密码
  2. 重置患者的用户名和密码
  3. 添加医生的账号记录,包括医生的专业说明,年龄,姓名和挂号费用
  4. 添加患者的门诊记录,包括医生信息,患者信息,门诊费用,医药费,诊断
  5. 显示每个患者的门诊记录
  6. 能够统计患者的门诊费用

患者:
7. 修改自己的密码和基础信息
8. 查看自己的门诊记录和花费等

类设计

  1. Clinic 诊所
  2. Doctor 医生
  3. Patient 患者

实现代码

  1. main.cpp
//
// Created by ling on 2021/6/16.
//

#include "clinic.h"

int main() {
    Clinic clinic;
    clinic.loginAdmin("admin", "123456");
    clinic.addUser("admin", "123456", "Patient.Ling", 25);

    clinic.loginUser("admin", "123456");

    clinic.loginAdmin("admin", "123456");
    clinic.resetUser(1, "123456789");
    clinic.loginUser("admin", "123456");
    clinic.loginUser("admin", "123456789");

    clinic.loginAdmin("admin", "123456");
    clinic.addBill(1, 1, "肠胃炎", 10);
    clinic.addDoctor("Doc.Ling", 35, "肠胃专家", 24.6);
    clinic.addBill(1, 1, "肠胃炎", 10);
    clinic.listBill();
    clinic.listPatientBill(1);
    return 0;
}
  1. clinic.h
#ifndef __CLINIC_H__
#define __CLINIC_H__

#include <string>
#include <vector>
#include "unordered_map"

using namespace std;

struct Patient {
    /* data */
    int id;
    string name;
    int age;
    string username;
    string password;
};

struct Doctor {
    /* data */
    int id;
    string name;
    int age;
    string info;
    double office_visit_fee;
};

struct Bill {
    /* data */
    int id;
    Doctor doctor;
    Patient patient;
    string diagnosis;
    double medicine_cost;
    double office_visit_fee;

    void printf() const;
};

class Clinic {
private:
    string m_admin_username;
    string m_admin_password;
    int m_state; // 0-admin 1-user

private:
    typedef pair<int, vector<Bill>> PatientBillPair;
    unordered_map<int, vector<Bill>> m_bills;
    std::vector<Patient> m_patients;
    std::vector<Doctor> m_doctors;

private:
    typedef pair<string, Patient> KeyPatientPair;
    unordered_map<string, Patient> m_user_table;


public:
    Clinic();

    Clinic(string admin_username, string admin_password);

public:
    typedef int STATE;
    STATE STATE_ADMIN = 0;
    STATE STATE_USER = 1;

private:
    void setState(STATE state);

    bool checkState(STATE state);

public:
    bool loginAdmin(string username, string password);

public:
    bool addDoctor(string name, int age, string info, double office_visit_fee);

    bool addBill(int doctor_id, int patient_id, string diagnosis, double medicine_cost);

public:
    void listBill();

    void listPatientBill(int patient_id);

public:
    bool loginUser(string username, string password);

    bool addUser(string username, string password, string name, int age);

    bool resetUser(int user_id, string password);
};

#endif
  1. clinic.cpp
#include "clinic.h"
#include <iostream>
#include <stdexcept>

Clinic::Clinic() {
    m_admin_username = "admin";
    m_admin_password = "123456";
}

Clinic::Clinic(string admin_username, string admin_password) {
    m_admin_username = admin_username;
    m_admin_password = admin_password;
}

void Clinic::setState(Clinic::STATE state) {
    if (state == 0) {
        cout << "切换诊所信息管理系统状态为: 管理员身份" << endl;
    } else {
        cout << "切换诊所信息管理系统状态为: 普通用户身份" << endl;
    }
    m_state = state;
};

bool Clinic::checkState(STATE state) {
    if (m_state == state) {
        cout << "权限正常" << endl;
        return true;
    } else {
        cout << "权限异常" << endl;
        return false;
    }
}

bool Clinic::loginAdmin(string username, string password) {
    if (username == m_admin_username && password == m_admin_password) {
        cout << "管理员账号登陆成功" << endl;
        setState(STATE_ADMIN);
        return true;
    } else {
        cout << "管理员账号登陆失败" << endl;
        return false;
    }
}

bool Clinic::loginUser(string username, string password) {
    string key;
    key.append(username);
    key.append(password);

    if (m_user_table.find(key) != m_user_table.end()) {
        cout << "普通账户登陆成功" << endl;
        setState(STATE_USER);
        return true;
    } else {
        cout << "普通账户登陆失败" << endl;
        return false;
    }
}

bool Clinic::addUser(string username, string password, string name, int age) {
    if (!checkState(STATE_ADMIN)) {
        cout << "只有管理员身份才能执行此操作" << endl;
        return false;
    }

    string key;
    key.append(username);
    key.append(password);

    if (m_user_table.find(key) == m_user_table.end()) {
        Patient patient = Patient{
                .id = (int) m_patients.size() + 1,
                .name = name,
                .age = age,
                .username = username,
                .password = password};
        m_user_table.insert(Clinic::KeyPatientPair(key, patient));
        m_patients.push_back(patient);
        cout << "添加普通用户[" << name << "]成功" << endl;
        return true;
    } else {
        cout << "普通用户[" << name << "]已存在" << endl;
        return false;
    }
}

bool Clinic::resetUser(int user_id, string password) {
    if (!checkState(STATE_ADMIN)) {
        cout << "只有管理员身份才能执行此操作" << endl;
        return false;
    }
    try {
        Patient patient = m_patients.at(user_id - 1);
        string key;
        key.append(patient.username);
        key.append(patient.password);
        m_user_table.erase(key);

        key.clear();
        key.append(patient.username);
        key.append(password);
        patient.password = password;
        m_user_table.insert(Clinic::KeyPatientPair(key, patient));
        cout << "重置普通用户密码成功" << endl;
        return true;
    }
    catch (out_of_range) {
        cout << "不存在该普通用户" << endl;
        return false;
    }
};

bool Clinic::addDoctor(string name, int age, string info, double office_visit_fee) {
    if (!checkState(STATE_ADMIN)) {
        cout << "只有管理员身份才能执行此操作" << endl;
        return false;
    }
    Doctor doctor = Doctor{
            .id = int(m_doctors.size() + 1),
            .name = name,
            .age = age,
            .info = info,
            .office_visit_fee = office_visit_fee};
    m_doctors.push_back(doctor);
    cout << "添加医生[" << name << "]信息成功" << endl;
    return true;
};

bool Clinic::addBill(int doctor_id, int patient_id, string diagnosis, double medicine_cost) {
    if (!checkState(STATE_ADMIN)) {
        cout << "只有管理员身份才能执行此操作" << endl;
        return false;
    }
    try {
        Doctor doctor = m_doctors.at(doctor_id - 1);
        Patient patient = m_patients.at(patient_id - 1);
        Bill bill = Bill{
                .doctor = doctor,
                .patient = patient,
                .diagnosis = diagnosis,
                .medicine_cost = medicine_cost,
                .office_visit_fee = doctor.office_visit_fee};
        m_bills[patient_id].push_back(bill);
        cout << "生成看病记录成功" << endl;
        return true;
    }
    catch (out_of_range) {
        cout << "生成看病记录失败" << endl;
        return false;
    }
};

void Bill::printf() const {
    cout << "======== 看病记录 BEGIN ========" << endl;
    cout << "病人: " << patient.name << endl;
    cout << "医生: " << doctor.name << endl;
    cout << "诊断: " << diagnosis << endl;
    cout << "医药费用: " << medicine_cost << endl;
    cout << "挂号费用: " << office_visit_fee << endl;
    cout << "======== 看病记录 END ========" << endl;
};

void Clinic::listBill() {
    for (auto &m_bill : m_bills) {
        for (auto &bill : m_bill.second) {
            bill.printf();
        }
    }
}

void Clinic::listPatientBill(int patient_id) {
    try {
        Patient patient = m_patients.at(patient_id - 1);
        vector<Bill> bills = m_bills.at(patient.id);
        for (auto bill = bills.begin(); bill != bills.end(); ++bill) {
            bill->printf();
        }
    } catch (out_of_range) {
        cout << "病人记录未找到" << endl;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值