trainings/CreatingReliableSoftwareCpp/Presentation/exercises/visitor/README.md

2.4 KiB

Linux compilation

> mkdir build
> cd build
> cmake -DCMAKE_BUILD_TYPE=Debug ..
> make

Exercise 1

  • Go into directory Core and implement a base class PrintVisitor which should implement visit method for two objects: Ship and Store
  • Create PrettyPrintVisitor which will print cargo from Ship and Sotore (just move the implementation printCargo).
  • Add the rest of necessary implementation for Ship and Store. Visitor should be taken as std::unique_ptr
  • Verify in the main.cpp if cargo print in the same way as previously.

Example:

Ship
|----------------|----------------|
|      NAME      |     AMOUNT     |
|----------------|----------------|
|Rum             | 300            |
|Banana          | 200            |
|Banana          | 150            |
|----------------|----------------|

Store
|----------------|----------------|----------------|
|      NAME      |     AMOUNT     |     PRICE      |
|----------------|----------------|----------------|
|Rum             | 1000           | 40             |
|Banana          | 1000           | 20             |
|Item            | 1000           | 30             |
|----------------|----------------|----------------|

Exercise 2

  • Create another visitor: BasicPrintVisitor which should print the cargo but without special frames, just raw info.
  • For Ship print Name and Amount
  • For Store print Name Amount and Price
  • You shouldn't modify anything in your code, just change the visitor in main.cpp and you should get a new output

Example:

Ship
Rum            300
Banana         200
Banana         150

Store
Rum            1000           40
Banana         1000           20
Item           1000           30

Exercise 3

  • Create CargoDamageVisitor. You don't need to create a base class, because we know that we will not extend this code,
  • You should create three operator() one for each cargo type: Alcohol, Fruit, Item,
  • For fruit, you should draw a number between 0 and 5 and subtract such an amount of cargo
  • For alcohol, you should draw a number between 0 and 10 and subtract half an amount of cargo
  • For item, you should draw a number between 0 and 20 and subtract one quarter of cargo
  • Visitor should be taken as a template argument by Player class
  • Apply visitor whenever you successfully deal damage to ship
  • Check in the main class if ship lost cargo after get damaged.