Skip to content

Commit 5aa882f

Browse files
committed
Refactored using only methods in Main
1 parent bb4e777 commit 5aa882f

12 files changed

Lines changed: 68 additions & 46 deletions

File tree

Calculator/Calculator/Program.cs

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,98 @@ class Program
44
{
55
static void Main(string[] args)
66
{
7-
7+
PrintWelcomeMessage();
8+
9+
string operatorSymbol = ChooseOperatorSymbol();
10+
11+
int numberOfNumbers = ChooseNumberOfNumbers(operatorSymbol);
12+
13+
double calculationResult = PerformOneCalculation(numberOfNumbers, operatorSymbol);
14+
15+
Console.WriteLine($"The answer to your calculation is: {calculationResult}");
16+
17+
}
18+
19+
private static void PrintWelcomeMessage()
20+
{
821
Console.WriteLine("Welcome to C# Calculator!");
22+
}
23+
24+
private static string ChooseOperatorSymbol()
25+
{
926
bool operatorEntered = false;
1027
string[] operatorArray = new string[] { "+", "-", "x", "/" };
1128

1229
Console.WriteLine("Please enter a operator (choose from +, -, x, /): ");
13-
string operatorSymbol = Console.ReadLine();
30+
string userInputOperator = Console.ReadLine();
1431

15-
if (operatorArray.Contains(operatorSymbol))
32+
if (operatorArray.Contains(userInputOperator))
1633
{
1734
operatorEntered = true;
1835
}
1936

2037
while (!operatorEntered)
2138
{
2239
Console.WriteLine("Please enter a operator (+, -, x, /)");
23-
operatorSymbol = Console.ReadLine();
24-
if (operatorArray.Contains(operatorSymbol))
40+
userInputOperator = Console.ReadLine();
41+
if (operatorArray.Contains(userInputOperator))
2542
{
2643
operatorEntered = true;
2744
}
2845
else
2946
operatorEntered = false;
3047
}
31-
48+
49+
return userInputOperator;
50+
}
51+
52+
public static int ChooseNumberOfNumbers(string operatorSymbol)
53+
{
3254
Console.WriteLine($"How many numbers do you want to {operatorSymbol}?");
3355
string userInput1 = Console.ReadLine();
34-
int numberOfNumbers = int.Parse(userInput1);
56+
return int.Parse(userInput1);
57+
}
3558

36-
if (operatorEntered == true)
37-
{
38-
double[] numbers = new double[numberOfNumbers];
59+
public static double PerformOneCalculation(int numberOfNumbers, string operatorSymbol)
60+
{
61+
double[] numbers = new double[numberOfNumbers];
3962

40-
for (int i = 0; i < numberOfNumbers; i++)
41-
{
42-
Console.WriteLine("Enter a number: ");
43-
string userInput2 = Console.ReadLine();
44-
double number = double.Parse(userInput2);
45-
numbers[i] = number;
46-
}
63+
for (int i = 0; i < numberOfNumbers; i++)
64+
{
65+
Console.WriteLine("Enter a number: ");
66+
string userInput2 = Console.ReadLine();
67+
double number = double.Parse(userInput2);
68+
numbers[i] = number;
69+
}
4770

48-
foreach (double number in numbers)
49-
{
50-
Console.WriteLine(number);
51-
}
71+
foreach (double number in numbers)
72+
{
73+
Console.WriteLine(number);
74+
}
5275

53-
double result = numbers[0];
76+
double result = numbers[0];
5477

55-
for (int i = 1; i < numberOfNumbers; i++)
78+
for (int i = 1; i < numberOfNumbers; i++)
79+
{
80+
switch (operatorSymbol)
5681
{
57-
switch (operatorSymbol)
58-
{
59-
case "+":
60-
result += numbers[i];
61-
break;
62-
case "-":
63-
result -= numbers[i];
64-
break;
65-
case "x":
66-
result *= numbers[i];
67-
break;
68-
case "/":
69-
result /= numbers[i];
70-
break;
71-
default:
72-
break;
73-
}
82+
case "+":
83+
result += numbers[i];
84+
break;
85+
case "-":
86+
result -= numbers[i];
87+
break;
88+
case "x":
89+
result *= numbers[i];
90+
break;
91+
case "/":
92+
result /= numbers[i];
93+
break;
94+
default:
95+
break;
7496
}
75-
76-
Console.WriteLine($"The answer to your calculation is: {result}");
77-
}
97+
}
98+
return result;
7899
}
100+
79101
}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
196 Bytes
Binary file not shown.

Calculator/Calculator/obj/Debug/net9.0/Calculator.AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[assembly: System.Reflection.AssemblyCompanyAttribute("Calculator")]
1414
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
1515
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
16-
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+094fcfecfa2da4ab481ecea93f3a43e7a6418141")]
16+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bb4e777b8a43630b6516f125529cc943c7c6beee")]
1717
[assembly: System.Reflection.AssemblyProductAttribute("Calculator")]
1818
[assembly: System.Reflection.AssemblyTitleAttribute("Calculator")]
1919
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
63ea61418fc7743c32e039b2b1f2d58cc02c7d915e763e42beba601f9ff96eab
1+
247bd62b5b49426f8260c35398dc348a3e92c9a9ed26c3ff6dccc522779b2eee
0 Bytes
Binary file not shown.
196 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"documents":{"C:\\Users\\hanna\\RiderProjects\\CSharpPreReading\\*":"https://raw.githubusercontent.com/hduns/CSharpPreReading/094fcfecfa2da4ab481ecea93f3a43e7a6418141/*"}}
1+
{"documents":{"C:\\Users\\hanna\\RiderProjects\\CSharpPreReading\\*":"https://raw.githubusercontent.com/hduns/CSharpPreReading/bb4e777b8a43630b6516f125529cc943c7c6beee/*"}}
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)