diff --git a/ch1_firstCase/ch1_switch/movie.cpp b/ch1_firstCase/ch1_switch/movie.cpp index 196cffc..82d3e43 100644 --- a/ch1_firstCase/ch1_switch/movie.cpp +++ b/ch1_firstCase/ch1_switch/movie.cpp @@ -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; +} + diff --git a/ch1_firstCase/ch1_switch/movie.h b/ch1_firstCase/ch1_switch/movie.h index a50d113..0dfa8d6 100644 --- a/ch1_firstCase/ch1_switch/movie.h +++ b/ch1_firstCase/ch1_switch/movie.h @@ -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; //影片名 diff --git a/ch1_firstCase/ch1_switch/rental.cpp b/ch1_firstCase/ch1_switch/rental.cpp index 1f3a48b..162deba 100644 --- a/ch1_firstCase/ch1_switch/rental.cpp +++ b/ch1_firstCase/ch1_switch/rental.cpp @@ -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()