Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Refactoring/Refactoring.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core">
<HintPath>..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.core.interfaces">
<HintPath>..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.core.interfaces.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="nunit.util">
<HintPath>..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\nunit.util.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NUnit.VisualStudio.TestAdapter">
<HintPath>..\packages\NUnitTestAdapter.WithFramework.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
119 changes: 74 additions & 45 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,31 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Refactoring
{
public class Tusc
{

public static void Start(List<User> usrs, List<Product> prods)
{
// Write welcome message
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");

DisplayWelcomeMessage();

// Login
Login:
bool loggedIn = false; // Is logged in?

// Prompt for user input
Console.WriteLine();
Console.WriteLine("Enter Username:");
string name = Console.ReadLine();

string name = GetUserName();

// Validate Username
bool valUsr = false; // Is valid user?

if (!string.IsNullOrEmpty(name))
{
for (int i = 0; i < 5; i++)
foreach (User user in usrs)
{
User user = usrs[i];
// Check that name matches
if (user.Name == name)
{
Expand All @@ -42,16 +40,12 @@ public static void Start(List<User> usrs, List<Product> prods)
// if valid user
if (valUsr)
{
// Prompt for user input
Console.WriteLine("Enter Password:");
string pwd = Console.ReadLine();
string pwd = GetCredentials();

// Validate Password
bool valPwd = false; // Is valid password?
for (int i = 0; i < 5; i++)
foreach (User user in usrs)
{
User user = usrs[i];

// Check that name and password match
if (user.Name == name && user.Pwd == pwd)
{
Expand All @@ -64,46 +58,25 @@ public static void Start(List<User> usrs, List<Product> prods)
{
loggedIn = true;

// Show welcome message
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("Login successful! Welcome " + name + "!");
Console.ResetColor();
DisplayLoginMessage(name);

// Show remaining balance
double bal = 0;
for (int i = 0; i < 5; i++)
{
User usr = usrs[i];

double bal = 0;
foreach (User user in usrs)
{
// Check that name and password match
if (usr.Name == name && usr.Pwd == pwd)
if (user.Name == name && user.Pwd == pwd)
{
bal = usr.Bal;
bal = user.Bal;

// Show balance
Console.WriteLine();
Console.WriteLine("Your balance is " + usr.Bal.ToString("C"));
ShowBalanceMessaege();
}
}

// Show product list
while (true)
{
// Prompt for user input
Console.WriteLine();
Console.WriteLine("What would you like to buy?");
for (int i = 0; i < 7; i++)
{
Product prod = prods[i];
Console.WriteLine(i + 1 + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
}
Console.WriteLine(prods.Count + 1 + ": Exit");

// Prompt for user input
Console.WriteLine("Enter a number:");
string answer = Console.ReadLine();
string answer = GetUserInput(prods);
int num = Convert.ToInt32(answer);
num = num - 1; /* Subtract 1 from number
num = num + 1 // Add 1 to number */
Expand Down Expand Up @@ -226,5 +199,61 @@ public static void Start(List<User> usrs, List<Product> prods)
Console.WriteLine("Press Enter key to exit");
Console.ReadLine();
}

private static string GetUserInput(List<Product> prods)
{
// Prompt for user input
Console.WriteLine();
Console.WriteLine("What would you like to buy?");
foreach (Product prod in prods)
{
Console.WriteLine( (prods.FindIndex(prod.Equals) + ": " + prod.Name + " (" + prod.Price.ToString("C") + ")");
}
Console.WriteLine(prods.Count + 1 + ": Exit");

// Prompt for user input
Console.WriteLine("Enter a number:");
string answer = Console.ReadLine();
return answer;
}

private static void ShowBalanceMessaege()
{
// Show balance
Console.WriteLine();
Console.WriteLine("Your balance is " + usr.Bal.ToString("C"));
}

private static void DisplayLoginMessage(string name)
{
// Show welcome message
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("Login successful! Welcome " + name + "!");
Console.ResetColor();
}

private static string GetCredentials()
{
// Prompt for user input
Console.WriteLine("Enter Password:");
string pwd = Console.ReadLine();
return pwd;
}

private static string GetUserName()
{
Console.WriteLine();
Console.WriteLine("Enter Username:");
string name = Console.ReadLine();
return name;
}

private static void DisplayWelcomeMessage()
{
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");
}
}
}
1 change: 1 addition & 0 deletions Refactoring/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
<package id="NUnitTestAdapter.WithFramework" version="2.0.0" targetFramework="net45" />
</packages>