From 584761d8a36ca199bdd4c3fd4b758a4e377d0703 Mon Sep 17 00:00:00 2001 From: Rudi Buss <rudi.buss@th-deg.de> Date: Thu, 26 Sep 2024 19:17:38 +0200 Subject: [PATCH] Update Solution Exercise 5 - Farm.cpp --- .../Solutions/Solution Exercise 5 - Farm.cpp | 95 ++++++++++++++----- 1 file changed, 73 insertions(+), 22 deletions(-) diff --git a/Part 1/4. Control structures/Solutions/Solution Exercise 5 - Farm.cpp b/Part 1/4. Control structures/Solutions/Solution Exercise 5 - Farm.cpp index 9d740b0..af1b646 100644 --- a/Part 1/4. Control structures/Solutions/Solution Exercise 5 - Farm.cpp +++ b/Part 1/4. Control structures/Solutions/Solution Exercise 5 - Farm.cpp @@ -1,60 +1,111 @@ +#include <stdlib.h> +#include <iostream> + using namespace std; // Declaration of three global variables +int number = 0; +string type; int weight = 0; float dailyPrice = 0; float revenue = 0; +bool inStock; // Prototypes -void Data_query(); -void Calculate_revenue(); -void Data_output(); - -// Function to input the weight and price -void Data_query() { - cout << "What is the weight? " << endl; - cin >> weight; - cout << "What is the daily price? " << endl; - cin >> dailyPrice; +void Create_Animal(); +void Show_Animal(); +void Sell_Animal(); + +// Function to create +void Create_Animal() { + cout << "Please type in the number of the animal: "; + cin >> number; + cout << endl; + cout << "Please type in the type of the animal: "; + cin >> type; + cout << endl; + cout << "Please type in the weight of the animal: "; + cin >> weight; + cout << endl; + inStock = true; //Tier ist im Bestand } -// Function to calculate the revenue -// by multiplying weight & price -void Calculate_revenue() { - revenue = weight * dailyPrice; - cout << "Revenue: " << revenue << endl; +// Function to show the data +void Show_Animal() { + cout << "Animal number: " << number << endl; + cout << "Animal type: " << type << endl; + cout << "Animal weight: " << weight << endl; + cout << "In stock? (1 = yes, 0 = no): " << inStock << endl; } +//Function to sell +void Sell_Animal(int nr) +{ + if(number == nr && inStock == 1) + { + cout << "What is the daily price?"<< endl; + cin >> dailyPrice; + revenue = revenue + (weight*dailyPrice); + cout << "Total revenue: " << revenue << endl; + inStock = false; + cout << endl << endl; + } + else if (number == nr && inStock == 0) + { + cout << "Animal with the number " << nr << " is in stock." << endl; + } + else + { + cout << "Animal with the number " << nr << " is not in stock." << endl; + } +} ; + int main() { - int s = 0, input; // declares integer s to zero, is used for the menu + int s = 0, input, nr; // declares integer s to zero, is used for the menu // do-while loop that outputs the menu and calls submenus do { + system("cls"); //Clears the screen for windows-systems. For unix, use system("clear"); - you can also omit it + cout << "Welcome to the farm administration" << endl << endl << "Please make your selection in the menu...." << endl << endl << endl; // The menu - cout << "[1] Data query" << endl; - cout << "[2] Calculate revenue" << endl; + cout << "[1] Create animal" << endl; + cout << "[2] Show animal" << endl; + cout << "[3] Sell animal" << endl; cout << "[0] Exit" << endl << endl; cout << "Make a selection: "; + cin >> input; + + // Switch loop calls the individual menu items switch (input) { case 1: { system("cls"); - Data_query(); // Function to create an animal - cout << "Data has been typed in." << endl; + Create_Animal(); // Function to create an animal + cout << "Animal has been created." << endl; cin.ignore(); //In windows you can use alternatively system("pause"); cin.get(); break; + } case 2: { system("cls"); - Calculate_revenue(); + Show_Animal(); cin.ignore(); cin.get(); break; } + case 3: { + system("cls"); + cout << "Type in animal number: " << endl; + cin >> nr; + Sell_Animal(nr); + system("pause"); + break; + } + default: { cout << "Wrong input!" << endl; cin.ignore(); @@ -66,4 +117,4 @@ int main() { cout << endl << "Good Bye!" << endl; return 0; -}; +}; \ No newline at end of file -- GitLab