728x90
이 프로그램에서 마지막으로 구현할 부분은 사용자 인터페이스 코드로서,
사용자가 직원 데이터베이스를 쉽게 사용할 수 있도록 메뉴 기반으로 구현한다.
main() 함수는 화면에 메뉴를 출력하고, 여기서 선택한 동작을 수행하는 과정을 무한히 반복한다.
각 동작은 대부분 별도의 함수로 정의한다. 직원 정보를 화면에 출력하는 것과 같은 간단한 동작은 main() 함수 코드 안에서 직접 구현한다.
#include <iostream>
#include <stdexcept>
#include <exception>
#include "Database.h"
using namespace std;
using namespace Records;
int displayMenu();
void doHire(Database& db);
void doFire(Database& db);
void doPromote(Database& db);
void doDemote(Database& db);
int main()
{
Database employeeDB;
bool done = false;
while (!done) {
int selection = displayMenu();
switch (selection) {
case 0:
done = true;
break;
case 1:
doHire(employeeDB);
break;
case 2:
doFire(employeeDB);
break;
case 3:
doPromote(employeeDB);
break;
case 4:
employeeDB.displayAll();
break;
case 5:
employeeDB.displayCurrent();
break;
case 6:
employeeDB.displayFormer();
break;
default:
cerr << "Unknown command." << endl;
break;
}
}
return 0;
}
displayMenu() 함수는 화면에 메뉴를 출려갛고 사용자로부터 입력값을 받는다.
이 코드는 사용자가 이상한 행동을 하지 않으며 숫자를 요청하면 숫자를 입력한다고 가정했다.
int displayMenu()
{
int selection;
cout << endl;
cout << "Emplotee Database" << endl;
cout << "-----------------" << endl;
cout << "1) Hire a new emplotee" << endl;
cout << "2) Fire an employee" << endl;
cout << "3) Promote an employee" << endl;
cout << "4) List all employees" << endl;
cout << "5) List all current employees" << endl;
cout << "6) List all former employees" << endl;
cout << "0) Quit" << endl;
cout << endl;
cout << "---> ";
cin >> selection;
return selection;
}
doHire() 함수는 사용자로부터 신입직원의 이름을 입력받아서
데이터베이스에 그 직원에 대한 정보를 추가하도록 요청한다.
void doHire(Database& db)
{
string firstName;
string lastName;
cout << "First name? ";
cin >> firstName;
cout << "Last name? ";
cin >> lastName;
db.addEmployee(firstName, lastName);
}
doFire()와 doPromote()는 둘 다 직원번호를 이용하여 데이터베이스에서 그 직원에 대한 정보를 조회하고
Employee 객체의 public 메서드를 사용하여 항목을 적절히 변경한다.
void doFire(Database& db)
{
int employeeNumber;
cout << "Employee number? ";
cin >> employeeNumber;
try {
Employee& emp = db.getEmployee(employeeNumber);
emp.fire();
cout << "Employee " << employeeNumber << " terminated." << endl;
}
catch (const std::logic_error& exception) {
cerr << "Unable to terminate employee: " << exception.what() << endl;
}
}
void doPromote(Database& db)
{
int employeeNumber;
int raiseAmount;
cout << "Employee number? ";
cin >> employeeNumber;
cout << "How much of a raise? ";
cin >> raiseAmount;
try {
Employee& emp = db.getEmployee(employeeNumber);
emp.promote(raiseAmount);
}
catch (const std::logic_error& exception) {
cerr << "Unable to promote employee: " << exception.what() << endl;
}
}
'💻프로그래밍 내용 정리 > C++17' 카테고리의 다른 글
[C++ 2.1.2] 스트링 리터럴(리터럴 폴링, 로 스트링 리터럴, 확장 로스트링 리터럴) (0) | 2022.08.19 |
---|---|
[C++ 2.1.1] 동적 스트링(C 스타일 스트링), strlen, strcpy, strcat (0) | 2022.08.19 |
[C++ 1.6.3] Database 클래스 (0) | 2022.08.18 |
[C++ 1.6.2] Employee 클래스 (0) | 2022.08.18 |
[C++ 1.6.1] C++ 프로그램 만들기(직원 관리 시스템) (0) | 2022.08.18 |