
lafore_robert_objectoriented_programming_in_c
.pdf

836 |
Chapter 16 |
|
{
cout << “Enter tenant’s name: “; getaLine(renterName);
aptNo = ptrTenantList->getAptNo(renterName);
if(aptNo > 0) |
// if name found, |
{ |
// get rent amount |
cout << “Enter amount paid (345.67): “; |
|
cin >> rentPaid; |
|
cin.ignore(80, ‘\n’); |
|
cout << “Enter month rent is for (1-12): “; |
|
cin >> month; |
|
cin.ignore(80, ‘\n’); |
|
month--; |
// (internal is 0-11) |
ptrRentRecord->insertRent(aptNo, month, rentPaid); |
|
} |
|
else |
// return |
cout << “No tenant with that name.\n”; } // end getRent()
//--------------------------------------------------------------
///////////////////methods for class expense//////////////////// bool operator < (const expense& e1, const expense& e2)
{ |
// compares dates |
if(e1.month == e2.month) |
// if same month, |
return e1.day < e2.day; |
// compare days |
else |
// otherwise, |
return e1.month < e2.month; |
// compare months |
} |
|
//-------------------------------------------------------------- |
|
bool operator == (const expense& e1, const expense& e2) |
|
{ return e1.month == e2.month && e1.day == e2.day; } |
|
//-------------------------------------------------------------- |
|
ostream& operator << (ostream& s, const expense& exp) |
|
{ |
|
s << exp.month << ‘/’ << exp.day << ‘\t’ << exp.payee << ‘\t’ ; |
|
s << exp.amount << ‘\t’ << exp.category << endl; |
|
return s; |
|
} |
|
//-------------------------------------------------------------- |
|
////////////////////////////////////////////////////////////////
bool compareDates::operator () (expense* ptrE1, expense* ptrE2) const
{ return *ptrE1 < *ptrE2; } //--------------------------------------------------------------


Chapter 16
838
sort( vectPtrsExpenses.begin(), vectPtrsExpenses.end(), compareCategories() );
// for |
each |
category, sum the entries |
|
iter = |
vectPtrsExpenses.begin(); |
||
string |
tempCat |
= (*iter)->category; |
|
float sumCat = |
0.0; |
||
while( |
iter |
!= |
vectPtrsExpenses.end() ) |
{
if(tempCat == (*iter)->category)
sumCat += (*iter)->amount; |
// same category |
else |
|
{ |
// different category |
cout << ‘\t’ << tempCat << ‘\t’ << sumCat << endl; |
|
totalExpenses += sumCat; |
// add previous category |
tempCat = (*iter)->category; |
|
sumCat = (*iter)->amount; |
// add final amount |
} |
|
iter++; |
|
} // end while |
|
totalExpenses += sumCat; |
// add final category |
cout << ‘\t’ << tempCat << ‘\t’ << sumCat << endl; |
|
return totalExpenses; |
|
} // end displaySummary() |
|
//----------------------------------------------------------- |
|
//////////////methods for class expenseInputScreen////////////// expenseInputScreen::expenseInputScreen(expenseRecord* per) :
ptrExpenseRecord(per)
{ /*empty*/ } //-----------------------------------------------------------
void expenseInputScreen::getExpense()
{
int month, day;
string category, payee; float amount;
cout << “Enter month (1-12): “; cin >> month;
cin.ignore(80, ‘\n’);
cout << “Enter day (1-31): “; cin >> day;
cin.ignore(80, ‘\n’);
cout << “Enter expense category (Repairing, Utilities): “; getaLine(category);


Chapter 16
840
//-----------------------------------------------------------
void userInterface::interact()
{
while(true)
{
cout << “Enter ‘i’ to input data, \n”
<<“ ‘d’ to display a report, \n”
<<“ ‘q’ to quit program: “;
ch = getaChar(); |
|
if(ch==’i’) |
// enter data |
{ |
|
cout << “Enter ‘t’ to add tenant, \n” |
|
<<“ ‘r’ to record rent payment, \n”
<<“ ‘e’ to record expense: “;
ch = getaChar(); switch(ch)
{
//input screens exist only while being used case ‘t’: ptrTenantInputScreen =
new tenantInputScreen(ptrTenantList); ptrTenantInputScreen->getTenant(); delete ptrTenantInputScreen;
break;
case ‘r’: ptrRentInputScreen =
new rentInputScreen(ptrTenantList, ptrRentRecord); ptrRentInputScreen->getRent();
delete ptrRentInputScreen; break;
case ‘e’: ptrExpenseInputScreen =
new expenseInputScreen(ptrExpenseRecord); ptrExpenseInputScreen->getExpense(); delete ptrExpenseInputScreen;
break;
default: cout << “Unknown input option\n”; break;
} // end switch
}// end if
else if(ch==’d’) // display data
{
cout << “Enter ‘t’ to display tenants, \n”
<<“ ‘r’ to display rents\n”
<<“ ‘e’ to display expenses, \n”
<<“ ‘a’ to display annual report: “; ch = getaChar();
switch(ch)
{




Chapter 16
844
Summary
Trial and error may be sufficient for simple software development. For complex projects, a more organized approach is usually necessary. In this chapter, we’ve shown one possible methodology. The Unified Process consists of inception, elaboration, construction, and transition phases. Elaboration corresponds to program analysis, and construction corresponds to design and writing code.
The Unified Process uses the use case approach to capture the program’s users (actors) and the tasks (use cases) they want the program to carry out. A UML use case diagram shows the actors and use cases. From the use case descriptions, any noun is a candidate to become a class or a class attribute. Verbs become class member functions (also called operations or methods).
In addition to the use case diagram, other UML diagrams help to facilitate understanding between a program’s users and its developers. Relationships among classes can be shown in a class diagram, flow of control from one activity to another can be shown in activity diagrams, and sequence diagrams depict the communication between objects during the course of a use case.
Questions
Answers to these questions can be found in Appendix G.
1.True or false: the use case approach is concerned primarily with which methods a class uses.
2.Use cases are used to (among other things)
a.summarize problems encountered in program code.
b.discover what constructors a class may have.
c.help select appropriate class attributes.
d.deduce what classes may be necessary in a program.
3.A use case is basically a ________.
4.True or false: After a use case diagram is created, new use cases can be added after coding has begun.
5.A use case description is sometimes written in two ____________ .
6.An actor might be
a.a different system that interacts with the system being developed.
b.a software entity that helps the developer solve a particular coding problem.
c.a person who interacts with the system being developed.
d.the designer of the system.