Skip to content

Commit

Permalink
将getCharge()移动到Movie类中去
Browse files Browse the repository at this point in the history
  • Loading branch information
chentuo14 committed Mar 7, 2019
1 parent c27eee7 commit 159b11d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
22 changes: 22 additions & 0 deletions ch1_firstCase/ch1_switch/movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ std::__cxx11::string Movie::getTitle()
return _title;
}

double Movie::getCharge(int daysRented)
{
double result = 0;
switch(getPriceCode()) {
case Movie::REGULAR: //普通片,起步价为2元,租期超过2天的部分每天1.5元
result += 2;
if(daysRented > 2)
result += (daysRented - 2) * 1.5;
break;
case Movie::NEW_RELEASE: //新片,每天3元
result += daysRented * 3;
break;
case Movie::CHILDRENS: //儿童片,起步价1.5元,租期超过3天的部分每天1.5元
result += 1.5;
if(daysRented > 3)
result += (daysRented - 3) * 1.5;
break;
}

return result;
}

12 changes: 7 additions & 5 deletions ch1_firstCase/ch1_switch/movie.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
class Movie
{
public:
enum category {
REGULAR = 0, //普通影片
NEW_RELEASE, //新片
CHILDRENS //儿童影片
};

Movie(std::string title = "empty", int price = 0);

int getPriceCode();
void setPriceCode(int arg);
std::string getTitle();
double getCharge(int daysRented);

enum category {
REGULAR = 0, //普通影片
NEW_RELEASE, //新片
CHILDRENS //儿童影片
};

private:
std::string _title; //影片名
Expand Down
19 changes: 1 addition & 18 deletions ch1_firstCase/ch1_switch/rental.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,7 @@ Movie Rental::getMovie()

double Rental::getCharge()
{
double result = 0;
switch(getMovie().getPriceCode()) {
case Movie::REGULAR: //普通片,起步价为2元,租期超过2天的部分每天1.5元
result += 2;
if(getDaysRented() > 2)
result += (getDaysRented() - 2) * 1.5;
break;
case Movie::NEW_RELEASE: //新片,每天3元
result += getDaysRented() * 3;
break;
case Movie::CHILDRENS: //儿童片,起步价1.5元,租期超过3天的部分每天1.5元
result += 1.5;
if(getDaysRented() > 3)
result += (getDaysRented() - 3) * 1.5;
break;
}

return result;
return getMovie().getCharge(_daysRented);
}

int Rental::getFrequentRenterPoints()
Expand Down

0 comments on commit 159b11d

Please sign in to comment.