Created
October 10, 2018 20:55
-
-
Save Dawiducik/d9a1b7cd38f0a57ec228366e8245e21a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System; | |
namespace Application | |
{ | |
public class Numbers | |
{ | |
private List<float> _numbers = new List<float>(); | |
private void DisplayMenu() | |
{ | |
Console.Clear(); | |
Console.CursorVisible = false; | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine("1. Add"); | |
Console.WriteLine("2. Show"); | |
Console.WriteLine("3. Find"); | |
Console.WriteLine("4. Stats"); | |
RedText("5. Extit"); | |
Console.WriteLine(); | |
} | |
private void GreenText(string text) | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine(text); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
private void RedText(string text) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(text); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
private void DisplayStats() | |
{ | |
Console.WriteLine($"Total number of entries {_numbers.Count}"); | |
Console.WriteLine($"Sum: {_numbers.Sum()}"); | |
Console.WriteLine($"Średnia: {_numbers.Sum() / _numbers.Count}"); | |
Console.WriteLine($"Minimalna liczba: {_numbers.Min()}"); | |
Console.WriteLine($"Maksymalna liczba: {_numbers.Max()}"); | |
} | |
private void DebugMode() | |
{ | |
Random rnd = new Random(); | |
for (int i = 0; i < 50; i++) | |
{ | |
_numbers.Add(rnd.Next(200)); | |
} | |
} | |
public void Initialize(string[] args) | |
{ | |
ConsoleKeyInfo key; | |
char choice; | |
//Debug | |
if (args.Length > 0 && args[0] == "-d") | |
{ | |
DebugMode(); | |
} | |
DisplayMenu(); | |
do | |
{ | |
key = Console.ReadKey(); | |
Console.Clear(); | |
choice = key.KeyChar; | |
Console.CursorVisible = true; | |
switch (choice) | |
{ | |
case '1': //add | |
GreenText("Type number:"); | |
string newNumber = Console.ReadLine(); | |
float takenNumber = float.Parse(newNumber, System.Globalization.CultureInfo.InvariantCulture); | |
_numbers.Add(takenNumber); | |
DisplayMenu(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"Added number {takenNumber}"); | |
break; | |
case '2': //show | |
DisplayMenu(); | |
GreenText("List of entries (scroll down to see all):"); | |
if (_numbers.Count() == 0) | |
{ | |
RedText("No entries!"); | |
break; | |
} | |
else | |
{ | |
string bar = '+' + new string('-', 3) + '+' + new string('-', 14) + '+'; | |
Console.WriteLine(bar); | |
for (int i = 0; i < _numbers.Count(); i++) | |
{ | |
string formatedDisplay = String.Format("|{0, -3}|{1, -14}|", i + 1, _numbers[i]); | |
Console.WriteLine(formatedDisplay); | |
} | |
Console.WriteLine(bar); | |
Console.SetCursorPosition(0, 0); | |
break; | |
} | |
case '3': //search | |
GreenText("Type number to find: "); | |
float findNumber = Convert.ToSingle(Console.ReadLine()); | |
DisplayMenu(); | |
if(_numbers.Contains(findNumber)) | |
{ | |
GreenText($"Found number '{findNumber}' {_numbers.Count(x => x == findNumber)} times!"); | |
} | |
else | |
{ | |
RedText($"Not found any {findNumber}'s"); | |
} | |
break; | |
case '4': //statistics | |
DisplayMenu(); | |
DisplayStats(); | |
break; | |
case '5': //close | |
RedText("Exiting program"); | |
break; | |
default: | |
DisplayMenu(); | |
RedText("ERROR! Pick option from 1 to 5"); | |
break; | |
} | |
} | |
while (choice != '5'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Application | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var numbers = new Numbers(); | |
numbers.Initialize(args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment