下午好!
我做了一些快速搜索,我很难弄清楚应该如何去做我需要做的事情。
对于这个程序,我们正在创建一个基本的工作票类。每个属性都有自己的mutator和accessor,但另外还有一个mutator,它将所有属性作为参数并一次性设置。
我不能仅仅使用构造函数放置在值中的主要原因是因为对于这个mutator,我们必须对值执行一些验证。对于单个属性mutators,其中两个已经在mutator中执行了验证。只有在调用setWorkTicket mutator时才需要验证其他两个。
有没有办法从mutator中调用mutator?或者,确保它验证这两个属性的唯一方法是将它们的验证从mutators复制并粘贴到setWorkTicket中吗?
如果您需要澄清,或者我提出的问题不清楚,请告诉我。我附上了这个课程。
/** Lab2.cpp
* @desc SOME DESCRIPTION
* @author Aaron, Aaron
* @since 24 September 2014
**/
#include <iostream>
#include <iomanip>
#include <string>
#include "MyInputValidation.h"
#include <sstream>
#include <stdexcept>
using namespace std;
class WorkTicket
{
public:
/*** PARAMETERIZED CONSTRUCTOR ***/
WorkTicket(): myWorkTicketNumber(0), myClientID(""), myDay(1), myMonth(1), myYear(2000), myIssue("") {}
WorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue);
/*** ACCESSORS ***/
int getWorkTicketNumber() const { return myWorkTicketNumber; }
string getClientID() const { return myClientID; }
string getDate(int myDay, int myMonth, int myYear) const;
string getIssue() const { return myIssue; }
void showWorkTicket() const;
/*** MUTATORS ***/
void setWorkTicketNumber(int workTicketNumber);
void setClientID(string clientID) { myClientID = clientID; }
void setDate(int day, int month, int year);
void setIssue(string issue) { myIssue = issue; }
bool setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue);
private:
int myWorkTicketNumber;
string myClientID;
int myDay;
int myMonth;
int myYear;
string myIssue;
};
/*** MAIN ***/
int main ()
{
// Output an information header
cout << "=============================" << endl
<< "=== WorkTicket Class Demo ===" << endl
<< "=============================" << endl;
try
{
// DECLARATIONS
WorkTicket ticketOne(1, "Aaron", 24, 9, 2014, "Printer not accessible from networked computers.");
ticketOne.showWorkTicket();
}
catch(const exception& ex)
{
cerr << ex.what(); // display the exception message.
}
// done.
cout << endl << endl;
return 0;
}
/*** PARAMETERIZED CONSTRUCTOR ***/
WorkTicket::WorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{
setWorkTicketNumber(workTicketNumber);
setClientID(clientID);
setDate(day, month, year);
setIssue(issue);
}
/*** ACCESSORS ***/
string WorkTicket::getDate(int myDay, int myMonth, int myYear) const
{
stringstream dateReturn;
dateReturn << myDay << "-" << myMonth << "-" << myYear;
return dateReturn.str();
}
void WorkTicket::showWorkTicket() const
{
cout << setw(20) << "\nWork Ticket Number: " << myWorkTicketNumber << endl
<< setw(20) << left << "Client ID: " << myClientID << endl
<< setw(20) << left << "Ticket Date: " << myDay << "-" << myMonth << "-" << myYear << endl
<< setw(20) << left << "Issue: " << myIssue << endl;
}
/*** MUTATORS ***/
void WorkTicket::setWorkTicketNumber(int workTicketNumber)
{
// Local declarations
const int MIN_TICKET_NUMBER = 1; // Ace
if(workTicketNumber < MIN_TICKET_NUMBER)
{
// declare a stringstream object
stringstream strOut;
// build a descriptive error string.
strOut << "\nTicket Number argument: " << workTicketNumber << " is out of range. " << endl
<< "Ticket Number must be greater than or equal to " << MIN_TICKET_NUMBER << ". ";
// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else // otherwise, the rank parameter is fine
{
// assign the parameter to the member variable
myWorkTicketNumber = workTicketNumber;
}
}
void WorkTicket::setDate(int day, int month, int year)
{
const int MIN_DAY_MONTH = 1;
const int MAX_DAY = 31;
const int MAX_MONTH = 12;
const int MIN_YEAR = 2000;
const int MAX_YEAR = 2099;
stringstream strOut;
if(day < MIN_DAY_MONTH || day > MAX_DAY)
{
// build a descriptive error string.
strOut << "\nDate argument: " << day << " is out of range. " << endl
<< "Day must be greater than or equal to " << MIN_DAY_MONTH
<< " and less than or equal to " << MAX_DAY << ". ";
// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else if(month < MIN_DAY_MONTH || month > MAX_MONTH)
{
// build a descriptive error string.
strOut << "\nDate argument: " << month << " is out of range. " << endl
<< "Month must be greater than or equal to " << MIN_DAY_MONTH
<< " and less than or equal to " << MAX_MONTH << ". ";
// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else if(year < MIN_YEAR || year > MAX_YEAR)
{
// declare a stringstream object
stringstream strOut;
// build a descriptive error string.
strOut << "\nDate argument: " << year << " is out of range. " << endl
<< "Year must be greater than or equal to " << MIN_YEAR
<< " and less than or equal to " << MAX_YEAR << ". ";
// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else // otherwise, the rank parameter is fine
{
// assign the parameter to the member variable
myDay = day;
myMonth = month;
myYear = year;
}
}
bool WorkTicket::setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{
return true;
}
答案 0 :(得分:2)
刚刚做错了什么:
bool WorkTicket::setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{
setWorkTicketNumber(workTicketNumber);
setDate(day, month, year);
setClientID(clientID);
setIssue(issue);
return true;
}
您可以让异常传播或在setWorkTicket()
中捕获它们,并根据您的需要返回false
。