Skip to content

Commit

Permalink
新建Price类,提供相关行文,加上子类的对应具体函数
Browse files Browse the repository at this point in the history
  • Loading branch information
chentuo14 committed Mar 7, 2019
1 parent 96a5b5c commit 7eecf5b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 29 deletions.
6 changes: 4 additions & 2 deletions ch1_firstCase/ch1_switch/ch1_switch.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ CONFIG -= qt
SOURCES += main.cpp \
customer.cpp \
movie.cpp \
rental.cpp
rental.cpp \
price.cpp

include(deployment.pri)
qtcAddDeployment()

HEADERS += \
customer.h \
movie.h \
rental.h
rental.h \
price.h

8 changes: 4 additions & 4 deletions ch1_firstCase/ch1_switch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main()

//print out all movies information;
const std::vector<Movie>::size_type numMovies = movies.size();
for(int i=0;i<numMovies;++i) {
for(unsigned int i=0;i<numMovies;++i) {
Movie tempMovie = movies[i];
std::cout << " the Tile of the "<<i+1 << "("
<< tempMovie.getTitle() << "," << tempMovie.getPriceCode() << ")"
Expand All @@ -42,19 +42,19 @@ int main()

//print out all customers information
const std::vector<Customer>::size_type numCustomers = customers.size();
for(int i=0;i<numCustomers;++i) {
for(unsigned int i=0;i<numCustomers;++i) {
Customer tempCust = customers[i];
std::cout << "the " << std::to_string(i+1) << " the customer " << tempCust.getName()
<< " has rented these movies:" << std::endl;
const std::vector<Rental>::size_type numRentals = tempCust.getRentals().size();
for(int j=0;j<numRentals;++j) {
for(unsigned int j=0;j<numRentals;++j) {
std::cout << " (" << tempCust.getRentals()[j].getMovie().getTitle()
<< ", " << tempCust.getRentals()[j].getDaysRented() << ")" << std::endl;
}
}
std::cout << std::endl;

for(int i=0;i<numCustomers;++i) {
for(unsigned int i=0;i<numCustomers;++i) {
Customer tempCust = customers[i];
std::cout << tempCust.statement() << std::endl;
}
Expand Down
37 changes: 16 additions & 21 deletions ch1_firstCase/ch1_switch/movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
Movie::Movie(std::__cxx11::string title, int price)
{
_title = title;
_priceCode = price;
setPriceCode(price);
}

int Movie::getPriceCode()
{
return _priceCode;
return _price->getPriceCode();
}

void Movie::setPriceCode(int arg)
{
_priceCode = arg;
switch(arg) {
case Movie::REGULAR:
_price = new RegularPrice();
break;
case Movie::CHILDRENS:
_price = new ChildrensPrice();
break;
case Movie::NEW_RELEASE:
_price = new NewReleasePrice();
break;
default:
std::cout << "Incorrect Price Code"<<std::endl;
}
}

std::__cxx11::string Movie::getTitle()
Expand All @@ -23,24 +35,7 @@ std::__cxx11::string Movie::getTitle()

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;
return _price->getCharge(daysRented);
}

int Movie::getFrequentRenterPoints(int daysRented)
Expand Down
6 changes: 4 additions & 2 deletions ch1_firstCase/ch1_switch/movie.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <iostream>
#include <string>
#include "price.h"

class Price;
class Movie
{
public:
Expand All @@ -23,8 +25,8 @@ class Movie


private:
std::string _title; //影片名
int _priceCode; //价格码
std::string _title; //影片名
Price *_price; //价格码
};


Expand Down
38 changes: 38 additions & 0 deletions ch1_firstCase/ch1_switch/price.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "price.h"

int ChildrensPrice::getPriceCode()
{
return Movie::CHILDRENS;
}

double ChildrensPrice::getCharge(int daysRented)
{
double result = 1.5;
if(daysRented > 3)
result += (daysRented - 3) * 1.5;
return result;
}

int NewReleasePrice::getPriceCode()
{
return Movie::NEW_RELEASE;
}

double NewReleasePrice::getCharge(int daysRented)
{
double result = daysRented * 3;
return result;
}

int RegularPrice::getPriceCode()
{
return Movie::REGULAR;
}

double RegularPrice::getCharge(int daysRented)
{
double result = 2;
if(daysRented > 2)
result += (daysRented - 2) * 1.5;
return result;
}
31 changes: 31 additions & 0 deletions ch1_firstCase/ch1_switch/price.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef PRICE_H
#define PRICE_H

#include "movie.h"

class Price
{
public:
virtual int getPriceCode() = 0;
virtual double getCharge(int daysRented) = 0;
};

class ChildrensPrice : public Price
{
int getPriceCode();
double getCharge(int daysRented);
};

class NewReleasePrice : public Price
{
int getPriceCode();
double getCharge(int daysRented);
};

class RegularPrice : public Price
{
int getPriceCode();
double getCharge(int daysRented);
};

#endif // PRICE_H

0 comments on commit 7eecf5b

Please sign in to comment.