Skip to content

Commit

Permalink
去除totalAmount临时变量
Browse files Browse the repository at this point in the history
  • Loading branch information
chentuo14 committed Mar 5, 2019
1 parent 5cb6dc9 commit b260249
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ch1_firstCase/ch1_switch/customer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ std::__cxx11::string Customer::getName()
}

std::__cxx11::string Customer::statement()
{
double totalAmount = 0; //总金额
{
int frequentRenterPoints = 0; //积分点

std::string result = "Rental Record for " + getName() + "\n";
Expand All @@ -29,10 +28,9 @@ std::__cxx11::string Customer::statement()
//添加详单
result += "\t" + each.getMovie().getTitle() + "\t"
+ std::to_string(each.getCharge()) + "\n";
totalAmount += each.getCharge();
}
//添加脚注
result += "Amount owed is " + std::to_string(totalAmount) + "\n";
result += "Amount owed is " + std::to_string(getTotalCharge()) + "\n";
result += "You earned " + std::to_string(frequentRenterPoints) +
" frequent renter points" + "\n";
return result;
Expand All @@ -48,3 +46,14 @@ double Customer::amountFor(Rental aRental)
return aRental.getCharge();
}

double Customer::getTotalCharge()
{
double result = 0; //总金额
std::vector<Rental>::iterator iter = _rentals.begin();
for(;iter != _rentals.end();++iter) {
Rental each = *iter;
result += each.getCharge();
}
return result;
}

1 change: 1 addition & 0 deletions ch1_firstCase/ch1_switch/customer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Customer
std::string statement();
std::vector<Rental>& getRentals();
double amountFor(Rental aRental);
double getTotalCharge();

private:
std::string _name; //顾客名
Expand Down

0 comments on commit b260249

Please sign in to comment.