-1
我有這個簡單的程序計算四種不同的工人類型的工資。它的語義寫法,但我想重構它,所以我可以讓每個工人類型是它自己的類。C++將基本語義重構爲目標類
該程序的主要控制在switch語句中。我想要做的是爲每個工人類型創建一個類,然後使用適當的setter和getters來執行正確的計算。
payroll.cpp
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void userPrompt (void);
int main()
{
// declare paycode and salary
int paycode;
double salary;
// run user prompt function, input paycode
userPrompt();
cin >> paycode;
while(paycode != -1) {
//switch statement to handle user input
switch(paycode) {
case 1: // manager
cout << "Manager Selected." << endl;
cout << "Enter Weekly Salary: ";
// calculate manager's salary
cin >> salary;
cout << "Manager's pay is $" << std::fixed << setprecision(2) << salary << "\n" << endl;
break;
case 2: // hourly worker
double wage;
int hours;
cout << "Hourly worker Selected." << endl;
cout << "Enter the hourly salary: ";
cin >> wage;
cout << "Enter the total hours worked: ";
cin >> hours;
// calculate hourly worker's pay
// with respect to possible overtime
if (hours <= 40)
salary = hours * wage;
else
salary = 40.0 * wage + (hours - 40) * wage * 1.5;
cout << "Hourly worker's pay is $" << std::fixed << setprecision(2) << salary << "\n" << endl;
break;
case 3: // commission worker
int sales;
cout << "Commission Worker Selected." << endl;
cout << "Enter gross weekly sales: ";
cin >> sales;
// calculate commission worker's pay
salary = sales * 0.092 + 250;
cout << "Commission worker's pay is $" << std::fixed << setprecision(2) << salary << "\n" << endl;
break;
case 4: // widget worker
int widgets, wagePerWidget;
cout << "Widget Worker Selected." << endl;
cout << "Enter number of pieces: ";
cin >> widgets;
cout << "Enter wage per piece: ";
cin >> wagePerWidget;
// calculate widget worker's pay
salary = widgets * wagePerWidget;
cout << "Widget Worker's pay is $" << std::fixed << setprecision(2) << salary << "\n" << endl;
break;
}
// prompt user to input paycode again or exit
cout<< "Enter paycode (-1 to end): ";
cin >> paycode;
}
exit (0);
}
// userPrompt function declaration
void userPrompt (void)
{
// prompt user to input paycode
cout << "Enter paycode (-1 to end): ";
}
那麼,什麼是這裏的問題? – Antimony
switch語句中的操作,我想將它們移動到每個工作類型的類中。 – frankV
所以...做到了嗎?你的問題到底是什麼? –