Temperature Converter Design and implementation

Use case diagram

We can identify the following use cases from the description of this program
These are shown in the diagram below.

189c7e34

Now you would have to think which objects can do those use cases - this analysis will give you some candidate classes

Class diagram

From the above use case diagram we identify at least two classes - we need a class to handle interaction with the user that is showing the menu and taking input value to convert the temperature and the second class to convert the temperature according to what was selected in the menu.

TempConverterCD.png

Sequence diagram

To try to assign responsibilities we draw a sequence diagram to understand how the objects will send messages to each other to fulfill a use case and give a working software.
Here you basically have to understand which object will call which other object(s) and in what order are messages sent between the objects to fulfill the requirements or a use case

TempConvertSeqDiag.png

Implementation

TempConverterMenu.cpp - this has handles the user interaction - showing the menu and taking user input - these two responsiblities are closely related

#include <iostream>
#include "TemperatureConverter.cpp"
using namespace std;
 
class TempConverterMenu {
    private:
        int userChoice;
    public:
        TempConverterMenu();
        void showMenu();
        void promptForValue();
};
 
TempConverterMenu :: TempConverterMenu(){
    userChoice = -1;
}
 
void TempConverterMenu :: showMenu(){
    cout << "Temperature Converter" << endl;
    cout << "1. Convert from Centigrade to Fahrenheit" << endl;
    cout << "2. Convert from Fahrenheit to Centigrade" << endl;
    cout << "Press 1 or 2: ";
    cin >> userChoice;
    cout << userChoice;
 
    promptForValue();
}
 
void TempConverterMenu::promptForValue(){
    float temp = 0;
    TemperatureConverter tc;
    if(userChoice == 1) {
        cout << "\nEnter temperature in Centigrade to convert:";
        cin >> temp;
        float f = tc.convertCentigradeToFahrenheit(temp);
        cout << temp << "C = " << f << "F" << endl;
    }
    if(userChoice == 2) {
        cout << "\nEnter temperature in Fahrenheit to convert:";
        cin >> temp;
        float c = tc.convertFahrenheitToCentigrade(temp);
        cout << temp << "F = " << c << "C" << endl;
    }
}
 
int main(){
    TempConverterMenu tcm;
    tcm.showMenu();
}

Temperature Converter.cpp - the class which converts between Centigrade and Fahrenheit and vice versa

#include <iostream>
using namespace std;
 
class TemperatureConverter {
    public:
        float convertCentigradeToFahrenheit(float);
        float convertFahrenheitToCentigrade(float);
 
};
 
float TemperatureConverter :: convertCentigradeToFahrenheit (float centigrade) {
    float fahrenheit = (centigrade * (1.8)) + 32;
    //cout << fahrenheit;
    return fahrenheit;
}
 
float TemperatureConverter :: convertFahrenheitToCentigrade(float fahrenheit) {
    float centigrade = 0;
    centigrade = (fahrenheit - 32) * (0.55);
    return centigrade;
}
 
//for testing the class methods
/*
int main(){
    TemperatureConverter tc;
    float rtemp = tc.convertFahrenheitToCentigrade(100);
    cout << rtemp << endl;
    cout << tc.convertCentigradeToFahrenheit(40) << endl;
}*/
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License