-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cs
172 lines (150 loc) · 7.14 KB
/
Menu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment2b
{
internal class Menu
{
private int selectedIndex = 0;
public void Start()
{
Console.Clear(); //Makes sure that the screen is clear when start method is called
DisplayOptions(); //Show title with options
UserSelection(); //User will use the up, down and enter keys to select option
Continue();
}
private void ShowTitle() //This is the main title for the converter
{
Console.WriteLine("---------------------------");
Console.WriteLine();
Console.WriteLine(" TEMPERATURE CONVERTER");
Console.WriteLine();
Console.WriteLine("---------------------------");
Console.WriteLine();
}
private void DisplayOptions() //This method uses a cursor (>) to show what alternative is selected
{
string[] options = { //These are the options:
"Show Celsius to Fahrenheit Table",
"Show Fahrenheit to Celcius Table",
"Convert a specific Celsius temperature to Fahrenheit",
"Convert a specific Fahrenheit temperature to Celsius",
"Exit" };
Console.WriteLine("These are your options:");
Console.WriteLine(); //Blankspace
for (int i = 0; i < options.Length; i++)
{
string currentOption = options[i];
string prefix;
if (i == selectedIndex) //If the current option is the selectedIndex, write an > before option.
{
prefix = ">"; //A symbol for where the cursor is
}
else
{
prefix = " "; //blank space
}
Console.WriteLine($"{prefix} {currentOption}"); //Displays the prefix(>cursor or blank) and the options
}
Console.WriteLine(); //Blankspace
Console.WriteLine("Use UP, DOWN and ENTER to select an option!");
}
private void UserSelection() //This method refreshes the screen with the cursor at the selected option when a key is pressed
{
ConsoleKey keyPressed;
do
{
Console.Clear();
ShowTitle();
DisplayOptions();
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
keyPressed = keyInfo.Key;
//Updates SelectedIndex variable based on arrow keys.
if (keyPressed == ConsoleKey.UpArrow)
{
selectedIndex--;
if (selectedIndex < 0)
selectedIndex = 0; //This makes sure that selectedIndex can't be less than 0 (the first option)
}
else if (keyPressed == ConsoleKey.DownArrow)
{
selectedIndex++;
if (selectedIndex > 4)
selectedIndex = 4; //This makes sure that selectedIndex can't be more than 4 (the last option)
}
} while (keyPressed != ConsoleKey.Enter); //If the user hits enter, this method will end
}
private void Continue()
{
Console.Clear(); //Clears the screen
ShowTitle();
TemperatureConverter objTemperatureConverter = new(); //Create an object of the temp converter so that it can be used
switch (selectedIndex)
{
case 0:
Console.WriteLine("-- CELSIUS TO FAHRENHEIT TABLE --");
Console.WriteLine(); // Blankline
objTemperatureConverter.ShowCelsiusToFahrenheitTable(); //Show the Celsius to Fahrenheit table
Console.WriteLine("Press ENTER to go back to the previous screen");
Console.ReadLine();
Start(); //Returns to the start screen
break;
case 1:
Console.WriteLine("-- FAHRENHEIT TO CELCIUS TABLE --");
Console.WriteLine(); //Blankline
objTemperatureConverter.ShowFahrenheitToCelsiusTable(); //Show the Fahrenheit to Celcius table
Console.WriteLine("Press ENTER to go back to the previous screen");
Console.ReadLine();
Start(); //Returns to the start screen
break;
case 2:
Console.WriteLine("-- CONVERT A SPECIFIC CELSIUS TEMPERATURE TO FAHRENHEIT --");
Console.WriteLine(); //Blankline
objTemperatureConverter.ConvertCelsiusToFahrenheit(); //Show the Celsius to Fahrenheit converter
Console.WriteLine(); //Blankline
Console.WriteLine("Press ESCAPE to go back");
Console.WriteLine("Press any other key to convert a new temperature");
ConsoleKey keyPressedCase2;
ConsoleKeyInfo keyInfoCase2 = Console.ReadKey(true); //Variable for user input
keyPressedCase2 = keyInfoCase2.Key;
if (keyPressedCase2 == ConsoleKey.Escape) //If escape is pressed go to start screen
{
Start();
}
else //If any other button is pressed clear the screen, show title and restart this case
{
Console.Clear();
ShowTitle();
goto case 2;
}
break;
case 3:
Console.WriteLine("-- CONVERT A SPECIFIC FAHRENHEIT TEMPERATURE TO CELSIUS --");
Console.WriteLine(); //Blankline
objTemperatureConverter.ConvertFahrenheitToCelsius(); //Show the Fahrenheit to Celsius converter
Console.WriteLine(); //Blankline
Console.WriteLine("Press ESCAPE to go back");
Console.WriteLine("Press any other key to convert a new temperature");
ConsoleKey keyPressedCase3;
ConsoleKeyInfo keyInfoCase3 = Console.ReadKey(true); //Variable for user input
keyPressedCase3 = keyInfoCase3.Key;
if (keyPressedCase3 == ConsoleKey.Escape) //If escape is pressed go to start screen
{
Start();
}
else //If any other button is pressed clear the screen, show title and restart this case
{
Console.Clear();
ShowTitle();
goto case 3;
}
break;
default:
Console.Clear();
break;
}
}
}
}