C++

Cuko T.

asked • 12/21/20

oop classes and object question

Classes & UML Design

Objectives

To practice on UML

To practice on Class, its attributes and methods

To practice on Single Responsibility Principle and Singleton Pattern

Activities

The users will provide an input and output file from the command line. Write a program that reads

commands from the input file and prints output to the output file.

The input file contains the basic commands.

The command list;

-------------------------------------------------------------

start_engine;

stop_engine;

absorb_fuel <quantity>;

give_back_fuel <quantity>;

add_fuel_tank <capacity>;

list_fuel_tanks;

print_fuel_tank_count;

remove_fuel_tank <tank_id>;

connect_fuel_tank_to_engine <tank_id>;

disconnect_fuel_tank_from_engine <tank_id>;

list_connected_tanks;

print_total_fuel_quantity;

print_total_consumed_fuel_quantity;

print_tank_info <tank_id>;

fill_tank <tank_id> <fuel_quantity>;

open_valve <tank_id>;

close_valve <tank_id>;

break_fuel_tank <tank_id>;

repair_fuel_tank <tank_id>;

wait <seconds>;

stop_simulation;

--------------------------------------------------------------

 The program needs to run until it takes a “stop_simulation;” command.

 There is only one engine. The engine’s attributes are;

o fuel_per_second: double // it will always be 5.5

o status: boolean // true means running

 The engine has its internal tank to store fuel. Internal tank capacity will be 55.0

 There is no max tank count (Unlimited)

 Each command takes 1 second. So, after the command is executed, the engine consumes

some fuel if it is running.

 wait command consumes fuel along given seconds if the engine is running.

 print_tank_info command prints all information about the selected tank.

 The engine absorbs fuel from a connected tank when the internal tank capacity goes below

20.0. The connected tank is selected randomly, and if there is no enough fuel in it, another

tank will be selected.

 The engine has to return fuel in its internal tank to the connected tanks before it is

stopped. Returned fuel needs to go connected tank, which has the minimum fuel in it.

 There are several fuel tanks. Tank’s attributes are;

o capacity: double

o fuel_quantity: double

o broken: boolean

 The engine needs a minimum of one connected tank to start; otherwise, the engine can not start.

 Each tank has a valve to connect the tanks and the engine.

Task List;

1. Draw a UML diagram about the system.

2. Implement the classes. The classes need to include possible attributes and methods.

3. Simulate the system with several input files not only given example input file.

Problem Solving Tips

1. UML and source code has to match

2. Do not implement logic in Main. Do it in Class, which is responsible.

3. Have a look at the example input file.

----------------------------------------------------------------------------------

start_engine;

add_fuel_tank 100;

add_fuel_tank 150;

add_fuel_tank 100;

add_fuel_tank 250;

add_fuel_tank 100;

fill_tank 1 100;

fill_tank 2 150;

fill_tank 3 100;

connect_fuel_tank_to_engine 1;

connect_fuel_tank_to_engine 2;

connect_fuel_tank_to_engine 3;

fuel_tank_to_engine 4;

remove_fuel_tank 5;

connect_fuel_tank_to_engine 5;

disconnect_fuel_tank_from_engine 4;

give_back_fuel <quantity>;

open_valve 1;

open_valve 2;

fill_tank 1 100;

fill_tank 2 150;

fill_tank 3 100;

start_engine;

wait 5;

list_fuel_tanks;

print_fuel_tank_count;

list_connected_tanks;

print_total_fuel_quantity;

print_total_consumed_fuel_quantity;

print_tank_info 1;

print_tank_info 2;

close_valve <tank_id>;

wait 5;

fill_tank 1 100;

fill_tank 2 150;

fill_tank 3 100;

print_tank_info 1;

print_tank_info 2;

print_tank_info 3;

stop_engine;

print_tank_info 1;

print_tank_info 2;

stop_simulation;

Patrick B.

Also, the Engine and it's Fuel Tank Manager implemented as Singletons
Report

12/22/20

1 Expert Answer

By:

Patrick B. answered • 12/22/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Patrick B.

Though I am revealing my age, there is an old video game called "Scramble" that uses this very same logic. The jet must "refuel" by blowing up oil and gas tanks during gameplay. Another example is the video game "Phoenix" which uses the same logic, though with a protective shield.
Report

12/22/20

Cuko T.

First of all, thank you very much for your answer and suggestions. My explanation for incomprehensible points is as follows: 6) Cannot be used if the tank is broken 7) After connecting the tank to the engine, if the valve is not opened, the tank is broken. 8) After the tank leaves the engine, its value should be turned off. otherwise it breaks down. 9) If the fuel in the tanks and engine runs out, it ends with a stop engine notification. I created 4 classes and headers as part of the project: 1) engine 2) tank 3) valve 4) fileinput (I use it with fstream to read and print the input txt file.) I used a structure like I wrote below here but I don't know how to fill it. #include #include #include #include "File.h" #include "Engine.h" #include "Tank.h" #include "Valve.h" using namespace std; /** * @brief:This function reads commands from a file. * @param:fstream& commands-commands which are read from file. */ void FileInput::is_command(fstream& commands) { string str; if (!commands.is_open()) { cout << "File does not exist!" << endl; system("pause"); exit(0); } if (commands.peek() == ifstream::traits_type::eof()) { cout << "File is empty!" << endl; system("pause"); exit(0); } while (commands >> str) { if (str == "start_engine;") { cout << "Engine starts." << endl; } else if (str == "stop_engine;") { cout << "Engine stops." << endl; } else if (str == "give_back_fuel") { } else if (str == "add_fuel_tank") { } else if (str == "list_fuel_tanks;") { } else if (str == "remove_fuel_tank") { } else if (str == "connect_fuel_tank_to_engine") { } else if (str == "disconnect_fuel_tank_from_engine") { } else if (str == "open_valve") { } else if (str == "close_valve") { } else if (str == "break_fuel_tank") { } else if (str == "repair_fuel_tank") { } else if (str == "stop_simulation;") { cout << "End of the simulation." << endl; exit(0); } else { cout << "This command does not exist!" << endl; } } }
Report

12/22/20

Patrick B.

My email is patrick dot baldwin dot 1 at wyzant dot com ----------------------------------------------------------------------------------- we need to discuss more things: (1) Does the tank have only 1 value or ANY number of valves? (2) what to do if invalid command or syntax error?
Report

12/22/20

Cuko T.

ı send you a mail mr. Patrick
Report

12/25/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.