Answer to Question #83679 in C++ for Osman

Question #83679
write a c++ program of a school management system with the following features
1. Add/edit/delete courses
2. Add/edit/delete student
3. Add/edit/delete staff
4. Assign students to courses
5. Record student grade
6. Print academic record of students
1
Expert's answer
2018-12-13T08:29:10-0500

Course.h:

#ifndef QUESTION_83679_COURSE_H
#define QUESTION_83679_COURSE_H

#include <string>
#include <vector>

class Course
{
public:
    Course(std::string n) : name{n} {}
    Course(Course *c) : name{c->name} {}

    void setName(const std::string &n) { name = n; }

    std::string getName() const { return name; }

private:
    std::string name;
};


#endif //QUESTION_83679_COURSE_H

Student.h:

#ifndef QUESTION_83679_STUDENT_H
#define QUESTION_83679_STUDENT_H

#include <string>
#include <vector>
#include "Course.h"

using namespace std;

class Student
{
public:
    Student(std::string n) : name{n} {}

    void addCourse(Course c, std::string grade = "NONE");
    void delCourse(const int i) { courses.erase(courses.begin() + i); }
    void recordGrade(std::string courseName, std::string grade);
    void setName (const std::string &n) { name = n; }

    std::vector<std::string> getNameOfCourses();
    std::string getName() const { return name;}
    std::string getGrade(std::string nameC) const;

private:
    std::string name;

    struct StudentCourse
    {
        StudentCourse(Course c, std::string g = "NONE") : nameOfCourses{c.getName()}, grade{g} {}

        std::string nameOfCourses;
        std::string grade;
    };

    std::vector<StudentCourse> courses;
};


#endif //QUESTION_83679_STUDENT_H

Student.cpp:

#include "Student.h"

void Student::addCourse(Course c, std::string grade)
{
    courses.emplace_back(StudentCourse(c, grade));
}


void Student::recordGrade(std::string courseName, std::string grade)
{
    for (StudentCourse &i : courses)
        if (i.nameOfCourses == courseName)
        {
            i.grade = grade;
            break;
        }
}

std::vector<std::string> Student::getNameOfCourses()
{
    vector<std::string> ret;
    for (const auto i : courses)
        ret.push_back(i.nameOfCourses);

    return ret;
}

std::string Student::getGrade(std::string nameC) const
{
    for (int i = 0; i < courses.size(); i++)
        if (courses[i].nameOfCourses == nameC)
            return courses[i].grade;
}

Staff.h:

#ifndef QUESTION_83679_STAFF_H
#define QUESTION_83679_STAFF_H

#include <string>
#include <vector>
#include "Course.h"

class Staff
{

public:
    Staff(std::string n) : name{n} {}

    void setName(const std::string &n) { name = n; }

    std::string getName() { return name; }

private:
    std::string name;
};


#endif //QUESTION_83679_STAFF_H

main.cpp:

#include <iostream>
#include <vector>
#include "Course.h"
#include "Student.h"
#include "Staff.h"

using namespace std;

unsigned short menu(vector<string> strs);

void courseManagment(vector<Course> &c);
void studentManagment(vector<Course> &c, vector<Student> &stu);
void staffManagment(vector<Staff> &sta);



int main()
{
    vector<Course> courses;
    vector<Student> students;
    vector<Staff> staffs;

    vector<string> mStr{"Course management", "Student management", "Staff management"};
    unsigned short choice{0};

    do
    {
        choice = menu(mStr);
        switch (choice)
        {
            case 0:
                courseManagment(courses);
                break;
            case 1:
                studentManagment(courses, students);
                break;
            case 2:
                staffManagment(staffs);
                break;
            case 3:
                break;
            default: cout << "Invalid input" << endl;
        }
    } while (choice != 3);

    return 0;
}

unsigned short menu(vector<string> strs)
{
    for (int i = 0; i < strs.size(); i++)
    {
        cout << i + 1 << ") " << strs[i] << endl;
    }
    cout << strs.size() + 1 << ") Quit\n: ";
    unsigned short choice;
    cin >> choice;
    return --choice;
}

void addStaff(vector<Staff> &sta);
void editStaff(vector<Staff> &sta);
void deleteStaff(vector<Staff> &sta);

void staffManagment(vector<Staff> &sta)
{

    vector<string> mStr{"Add staff", "Edit staff", "Delete staff"};
    unsigned short choice{0};
    do
    {
        choice = menu(mStr);
        switch (choice)
        {
            case 0:
                addStaff(sta);
                break;
            case 1:
                editStaff(sta);
                break;
            case 2:
                deleteStaff(sta);
                break;
            case 3:
                break;
            default: cout << "Invalid input" << endl;
        }
    } while (choice != 3);
}

void addStaff(vector<Staff> &sta)
{
    cout << "Name: ";
    string name;
    cin >> name;
    sta.emplace_back(Staff(name));
}

void editStaff(vector<Staff> &sta)
{
    cout << "Choose staff: \n";
    vector<string> mStr;
    for (auto i : sta)
        mStr.push_back(i.getName());


    unsigned short i = menu(mStr);

    if (i < sta.size())
    {
        cout << "New name: ";
        string name;
        cin >> name;
        sta[i].setName(name);
    }
}

void deleteStaff(vector<Staff> &sta)
{
    cout << "Choose staff: \n";
    vector<string> mStr;
    for (auto i : sta)
        mStr.push_back(i.getName());

    unsigned short i = menu(mStr);
    if (i < sta.size())
        sta.erase(sta.begin() + i);
}

void addCourse(vector<Course> &c);
void editCourse(vector<Course> &c);
void deleteCourse(vector<Course> &c);

void courseManagment(vector<Course> &c)
{
    vector<string> mStr{"Add course", "Edit course", "Delete course"};
    unsigned short choice{0};
    do
    {
        choice = menu(mStr);
        switch (choice)
        {
            case 0:
                addCourse(c);
                break;
            case 1:
                editCourse(c);
                break;
            case 2:
                deleteCourse(c);
                break;
            case 3:
                break;
            default: cout << "Invalid input" << endl;
        }
    } while (choice != 3);
}

void addCourse(vector<Course> &c)
{
    cout << "Name: ";
    string name;
    cin >> name;
    c.emplace_back(Course(name));
}

void editCourse(vector<Course> &c)
{
    cout << "Choose course: \n";
    vector<string> mStr;
    for (auto i : c)
        mStr.push_back(i.getName());


    unsigned short i = menu(mStr);

    if (i < c.size())
    {
        cout << "New name: ";
        string name;
        cin >> name;
        c[i].setName(name);
    }
}

void deleteCourse(vector<Course> &c)
{
    cout << "Choose course: \n";
    vector<string> mStr;
    for (auto i : c)
        mStr.push_back(i.getName());

    unsigned short i = menu(mStr);
    if (i < c.size())
        c.erase(c.begin() + i);
}

void addStudent(vector<Student> &stu);
void editStudent(vector<Student> &stu);
void deleteStudent(vector<Student> &stu);
void addCourseToStudent(vector<Student> &stu, vector<Course> &c);
void recordStudentGrade(vector<Student> &stu);
void printStudentAcademicRecord(vector<Student> &stu);

void studentManagment(vector<Course> &c, vector<Student> &stu)
{
    vector<string> mStr{"Add student", "Edit student", "Delete student", "Assign students to courses",
                        "Record student grade", "Print academic record of students"};
    unsigned short choice{0};
    do
    {
        choice = menu(mStr);
        switch (choice)
        {
            case 0:
                addStudent(stu);
                break;
            case 1:
                editStudent(stu);
                break;
            case 2:
                deleteStudent(stu);
                break;
            case 3:
                addCourseToStudent(stu, c);
                break;
            case 4:
                recordStudentGrade(stu);
                break;
            case 5:
                printStudentAcademicRecord(stu);
                break;

            case 6:
                break;
            default: cout << "Invalid input" << endl;
        }
    } while (choice != 6);
}

void addStudent(vector<Student> &stu)
{
    cout << "Name: ";
    string name;
    cin >> name;
    stu.emplace_back(Student(name));
}

void editStudent(vector<Student> &stu)
{
    cout << "Choose student: \n";
    vector<string> mStr;
    for (auto i : stu)
        mStr.push_back(i.getName());


    unsigned short i = menu(mStr);

    if (i < stu.size())
    {
        cout << "New name: ";
        string name;
        cin >> name;
        stu[i].setName(name);
    }
}

void deleteStudent(vector<Student> &stu)
{
    cout << "Choose student: \n";
    vector<string> mStr;
    for (auto i : stu)
        mStr.push_back(i.getName());

    unsigned short i = menu(mStr);
    if (i < stu.size())

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS