Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include "trip.h" | ||
|
||
int main() | ||
{ | ||
Trip t( | ||
"Rom", | ||
6 /* days */, | ||
350 /* euros */ | ||
); | ||
|
||
// call getter-functions | ||
std::cout << "Reise nach " << t.get_destination() << " kostet " << t.get_price() << "€\n"; | ||
|
||
/* | ||
* Attributes of Trip are private, not visible to us. | ||
* The next line produces the following error | ||
* | ||
* main.cpp:26:7: error: 'price' is a private member of 'Trip' | ||
* t.price = 1; | ||
* ^ | ||
* ./trip.h:17:18: note: declared private here | ||
* unsigned int price; | ||
* ^ | ||
*/ | ||
t.price = 1; | ||
|
||
/* | ||
* Attributes of Rating are public, we can read them directly | ||
* They are however constant, meaning they can't be reassigned | ||
* The following error will be produced | ||
* | ||
* main.cpp:42:15: error: cannot assign to non-static data member 'quality' with const-qualified type 'const double' | ||
* r.quality = 5; | ||
* ~~~~~~~~~ ^ | ||
* ./rating.h:14:18: note: non-static data member 'quality' declared const here | ||
* const double quality; | ||
* ~~~~~~~~~~~~~^~~~~~~ | ||
*/ | ||
Rating r = Rating::input_rating(); | ||
std::cout << "Average rating is " << ((r.quality + r.pricing + r.cleanliness + r.accomodation) / 4) << "\n"; | ||
r.quality = 5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "rating.h" | ||
|
||
#include <iostream> | ||
|
||
/* | ||
* Ask user to rate a specific category (prompt) | ||
* It checks that the input is between POOR and EXCELLENT | ||
* User will be asked to input a valid number when check failed | ||
*/ | ||
int prompt_rating(std::string prompt) | ||
{ | ||
std::string line; | ||
|
||
while(true) | ||
{ | ||
std::cout << prompt; | ||
std::getline(std::cin, line); | ||
|
||
try { | ||
int result = std::stoi(line); | ||
if(result >= POOR && result <= EXCELLENT) | ||
// success! it's within range | ||
return result; | ||
|
||
std::cout << "Bewertung muss eine Zahl von " << POOR << " bis " << EXCELLENT << " sein!\n\n"; | ||
} catch(...) | ||
{ | ||
std::cout << "Eingabe muss eine (korrekte) Zahl sein\n\n"; | ||
} | ||
} | ||
} | ||
|
||
Rating::Rating(double _quality, double _pricing, double _cleanliness, double _accomodation) | ||
: quality(_quality), pricing(_pricing), cleanliness(_cleanliness), accomodation(_accomodation) | ||
{ | ||
// everything is already initialized | ||
} | ||
|
||
Rating::Rating() | ||
: Rating(0, 0, 0, 0) | ||
{ | ||
// other constructor called | ||
} | ||
|
||
Rating Rating::input_rating() | ||
{ | ||
double quality = (double) prompt_rating("Qualitat : "); | ||
double pricing = (double) prompt_rating("Preis-Leistung: "); | ||
double cleanliness = (double) prompt_rating("Sauberkeit : "); | ||
double accomodation = (double) prompt_rating("Komfort : "); | ||
|
||
return Rating(quality, pricing, cleanliness, accomodation); | ||
} | ||
|
||
void Rating::print() | ||
{ | ||
std::cout << "Qualitat : " << quality << "\n"; | ||
std::cout << "Preis-Leistung: " << pricing << "\n"; | ||
std::cout << "Sauberkeit : " << cleanliness << "\n"; | ||
std::cout << "Komfort : " << accomodation << "\n"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef H_RATING | ||
#define H_RATING | ||
|
||
enum Rating_Scale { POOR = 1, OK, GOOD, VERY_GOOD, EXCELLENT }; | ||
|
||
class Rating | ||
{ | ||
public: | ||
// const has to be statically initalized | ||
// (behind column after constructor declaration) | ||
// variables cannot be reassigned! | ||
const double quality; | ||
const double pricing; | ||
const double cleanliness; | ||
const double accomodation; | ||
|
||
Rating(double _quality, double _pricing, double _cleanliness, double _accomodation); | ||
|
||
Rating(); | ||
|
||
void print(); | ||
|
||
// return Rating based on user input | ||
static Rating input_rating(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "trip.h" | ||
|
||
#include <iostream> | ||
|
||
Trip::Trip(std::string _dest, unsigned int _duration, unsigned int _price) | ||
: destination(_dest), duration(_duration), price(_price) | ||
{ | ||
// everything already initialized, nothing to do here | ||
} | ||
|
||
void Trip::add_rating(Rating r) | ||
{ | ||
ratings.push_back(r); | ||
} | ||
|
||
Rating Trip::get_rating() | ||
{ | ||
// return default rating if nothing exists | ||
if(ratings.size() == 0) | ||
return Rating(); | ||
|
||
// sum up all ratings | ||
// variables are const, so we have to take care of all variables ourselves | ||
double quality = ratings[0].quality; | ||
double pricing = ratings[0].pricing; | ||
double cleanliness = ratings[0].cleanliness; | ||
double accomodation = ratings[0].accomodation; | ||
|
||
// already have index 0 from initialization, start at 1 | ||
for(int i = 1; i < ratings.size(); i++) | ||
{ | ||
quality += ratings[i].quality; | ||
pricing += ratings[i].pricing; | ||
cleanliness += ratings[i].cleanliness; | ||
accomodation += ratings[i].accomodation; | ||
} | ||
|
||
// calculate average | ||
quality /= ratings.size(); | ||
pricing /= ratings.size(); | ||
cleanliness /= ratings.size(); | ||
accomodation /= ratings.size(); | ||
|
||
return Rating(quality, pricing, cleanliness, accomodation); | ||
} | ||
|
||
void Trip::print() | ||
{ | ||
std::cout << "Reiseziel : " << destination << "\n"; | ||
std::cout << "Reisedauer (Tage): " << duration << "\n"; | ||
std::cout << "Kosten (Euro) : " << price << "\n\n"; | ||
|
||
std::cout << "Bewertungen (" << ratings.size() << ")\n"; | ||
Rating avg = get_rating(); | ||
avg.print(); | ||
} | ||
|
||
std::string Trip::get_destination() | ||
{ | ||
return destination; | ||
} | ||
|
||
unsigned int Trip::get_duration() | ||
{ | ||
return duration; | ||
} | ||
|
||
unsigned int Trip::get_price() | ||
{ | ||
return price; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef H_TRIP | ||
#define H_TRIP | ||
|
||
#include <string> | ||
#include <vector> | ||
#include "rating.h" | ||
|
||
class Trip | ||
{ | ||
private: | ||
// name of the location | ||
std::string destination; | ||
|
||
// duration in days | ||
unsigned int duration; | ||
|
||
// price in euros (no cents) | ||
unsigned int price; | ||
|
||
std::vector<Rating> ratings; | ||
|
||
public: | ||
Trip(std::string _dest, unsigned int _duration, unsigned int _price); | ||
|
||
void add_rating(Rating r); | ||
|
||
// it doesn't matter if return type is infront or behind function name (trailing return type) | ||
// in this case I think it's visually more appealing to have all getter function names start in the same column | ||
auto get_destination() -> std::string; | ||
auto get_duration() -> unsigned int; | ||
auto get_price() -> unsigned int; | ||
auto get_rating() -> Rating; | ||
|
||
void print(); | ||
}; | ||
|
||
|
||
#endif |