string Customer::statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); string result = "Rental Record for "+ getName() + "\n"; while(rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement(); thisAmount = amountFor(each); //add frequent renter points frequentRenterPoints ++; //add bonus for a two day new release rental if((each.getMovice().getPriceCode() == Movice.NEW_RELEASE)&& each.getDaysRented() > 1) frequentRenterPoints ++; //show figures for this rental result += "\t" + each.getMovice().getTitle() + "\t" + string.valueOf(thisAmount) +"\n"; totalAmount += thisAmount; } //add footer lines result += "Amount owed is "+ string.valueof(totalAmount)+"\n"; result += "You earned "+ string.valueof(frequentRenterPoints)+" frequent renter points"; return result; }
//calcualtes the amount for a rental double Customer::amountFor(const Rental& r) const{ double thisAmount = 0; if(r.getVideo().getCode() == Video::REGULAR) { thisAmount += 2; if (r.getDaysRented() > 2) thisAmount += (r.getDaysRented() - 2) * 1.5; } else if(r.getVideo().getCode() ==Video::NEW_RELEASE) { thisAmount += r.getDaysRented() * 3; } else if(r.getVideo().getCode() == Video::CHILDRENS) { thisAmount += 1.5; if (r.getDaysRented() > 3) thisAmount += (r.getDaysRented() - 3) * 1.5; } return thisAmount; }
// customer rental statement std::string Customer::statement() const { double totalAmount = 0; int frequentRenterPoints = 0; std::string result = "Rental Record for " + getName() + "\n"; for(std::vector<Rental>::const_iterator it = rentals.begin(); it != rentals.end(); ++it) { double thisAmount = 0; Rental each = (Rental) *it; //determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie::REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie::NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie::CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; } // add frequent renter points ++frequentRenterPoints; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie::NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints ++; //show figures for this rental result += "\t" + each.getMovie().getTitle()+ "\t"; std::ostringstream amount_stream; amount_stream << thisAmount; result += amount_stream.str(); result += "\n"; totalAmount += thisAmount; } //add footer lines // total amount owed result += "Amount owed is: "; std::ostringstream owed_stream; owed_stream << totalAmount; result += owed_stream.str(); result += "\n"; // frequent renter points earned result += "You earned: "; std::ostringstream frequent_stream; frequent_stream << frequentRenterPoints; result += frequent_stream.str(); result += " frequent renter points\n"; return result; }
//拆分函数(提炼到独立函数) //独立函数名称修改 double amountFor(Rental aRental) { double result = 0; //determin amounts for each line switch(aRental.getMovie().getPriceCode()) { case Movice.REGULAR: result += 2; if(aRental.getDaysRented() > 2) { result += (aRental.getDaysRented() - 2) * 1.5; } break; case Movice.NEW_RELEASE: result += aRental.getDaysRented() * 3; break; case Movice.CHILDRENS: result += 1.5; if (aRental.getDaysRented() > 3) { result += (aRental.getDaysRented() - 3) * 1.5; } break; } return result; }
// calculates amount for current statement double Customer::amountFor(const Rental& rental) const { double thisAmount = 0; switch(rental.getVideo().getCode()) { case Video::REGULAR: thisAmount += 2; if (rental.getDaysRented() > 2) thisAmount += (rental.getDaysRented() - 2) * 1.5; break; case Video::NEW_RELEASE: thisAmount += rental.getDaysRented() * 3; break; case Video::CHILDRENS: thisAmount += 1.5; if (rental.getDaysRented() > 3) thisAmount += (rental.getDaysRented() - 3) * 1.5; break; } return thisAmount; }
string Customer::statement() { double totalAmount = 0; int frequentRenterPoints = 0; ostringstream result; result << "Rental Record for " << getName () << "\n"; for(Iter iter = rentals.begin(); iter != rentals.end(); ++iter) { double thisAmount = 0; Rental* each = *iter; // determines the amount for each line switch (each->getMovie()->getPriceCode()) { case Movie::Regular: thisAmount += 2; if (each->getDaysRented () > 2) thisAmount += (each->getDaysRented () - 2) * 1.5; break; case Movie::NewRelease: thisAmount += each->getDaysRented () * 3; break; case Movie::Childrens: thisAmount += 1.5; if (each->getDaysRented () > 3) thisAmount += (each->getDaysRented () - 3) * 1.5; break; } frequentRenterPoints++; if (each->getMovie()->getPriceCode() == Movie::NewRelease && each->getDaysRented () > 1) frequentRenterPoints++; result << "\t" << each->getMovie()->getTitle() << "\t" << thisAmount << "\n"; totalAmount += thisAmount; } result << "You owed " << totalAmount << "\n" << "You earned " << frequentRenterPoints << " frequent renter points\n"; return result.str(); }