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
192 changes: 76 additions & 116 deletions Refactoring/Tusc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,136 +10,78 @@ namespace Refactoring
{
public class Tusc
{
public static void Start(List<User> usrs, List<Product> prods)
public static void Start(List<User> users, List<Product> products)
{
// Write welcome message
Console.WriteLine("Welcome to TUSC");
Console.WriteLine("---------------");

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

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

// Validate Username
bool valUsr = false; // Is valid user?
if (!string.IsNullOrEmpty(name))
{
for (int i = 0; i < 5; i++)
{
User user = usrs[i];
// Check that name matches
if (user.Name == name)
{
valUsr = true;
}
}

// if valid user
if (valUsr)
User user = validUserName(users, name);
if (user != null)
{
// Prompt for user input
Console.WriteLine("Enter Password:");
string pwd = Console.ReadLine();

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

// Check that name and password match
if (user.Name == name && user.Pwd == pwd)
{
valPwd = true;
}
}
string password = Console.ReadLine();

// if valid password
if (valPwd == true)
user = validPassword(users, name, password);
if (user != null)
{
loggedIn = true;

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

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

// Check that name and password match
if (usr.Name == name && usr.Pwd == pwd)
{
bal = usr.Bal;

// Show balance
Console.WriteLine();
Console.WriteLine("Your balance is " + usr.Bal.ToString("C"));
}
}
double bal = user.Bal;
Console.WriteLine("\nYour balance is " + bal.ToString("C"));

// 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");
printProductsList(products);

// Prompt for user input
Console.WriteLine("Enter a number:");
string answer = Console.ReadLine();
int num = Convert.ToInt32(answer);
num = num - 1; /* Subtract 1 from number
num = num + 1 // Add 1 to number */
int num = Convert.ToInt32(answer) - 1;

// Check if user entered number that equals product count
if (num == 7)
{
// Update balance
foreach (var usr in usrs)
foreach (var usr in users)
{
// Check that name and password match
if (usr.Name == name && usr.Pwd == pwd)
if (usr.Name == name && usr.Pwd == password)
{
usr.Bal = bal;
}
}

// Write out new balance
string json = JsonConvert.SerializeObject(usrs, Formatting.Indented);
string json = JsonConvert.SerializeObject(users, Formatting.Indented);
File.WriteAllText(@"Data/Users.json", json);

// Write out new quantities
string json2 = JsonConvert.SerializeObject(prods, Formatting.Indented);
string json2 = JsonConvert.SerializeObject(products, Formatting.Indented);
File.WriteAllText(@"Data/Products.json", json2);


// Prevent console from closing
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
Console.WriteLine("\nPress Enter key to exit");
Console.ReadLine();
return;
}
else
{
Console.WriteLine();
Console.WriteLine("You want to buy: " + prods[num].Name);
Console.WriteLine("\nYou want to buy: " + products[num].Name);
Console.WriteLine("Your balance is " + bal.ToString("C"));

// Prompt for user input
Expand All @@ -148,83 +90,101 @@ public static void Start(List<User> usrs, List<Product> prods)
int qty = Convert.ToInt32(answer);

// Check if balance - quantity * price is less than 0
if (bal - prods[num].Price * qty < 0)
if (bal - products[num].Price * qty < 0)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You do not have enough money to buy that.");
Console.ResetColor();
printMessage(ConsoleColor.Red, "", "You do not have enough money to buy that.");
continue;
}

// Check if quantity is less than quantity
if (prods[num].Qty <= qty)
if (products[num].Qty <= qty)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("Sorry, " + prods[num].Name + " is out of stock");
Console.ResetColor();
printMessage(ConsoleColor.Red, "", "Sorry, " + products[num].Name + " is out of stock");
continue;
}

// Check if quantity is greater than zero
if (qty > 0)
{
// Balance = Balance - Price * Quantity
bal = bal - prods[num].Price * qty;
bal = bal - products[num].Price * qty;

// Quanity = Quantity - Quantity
prods[num].Qty = prods[num].Qty - qty;

Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You bought " + qty + " " + prods[num].Name);
Console.WriteLine("Your new balance is " + bal.ToString("C"));
Console.ResetColor();
products[num].Qty = products[num].Qty - qty;
printMessage(ConsoleColor.Green, "You bought " + qty + " " + products[num].Name, "Your new balance is " + bal.ToString("C"));
}
else
{
// Quantity is less than zero
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine();
Console.WriteLine("Purchase cancelled");
Console.ResetColor();
printMessage(ConsoleColor.Yellow, "", "Purchase cancelled");
}
}
}
}
else
{
// Invalid Password
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You entered an invalid password.");
Console.ResetColor();

printMessage(ConsoleColor.Red, "", "You entered an invalid password.");
goto Login;
}
}
else
{
// Invalid User
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You entered an invalid user.");
Console.ResetColor();

printMessage(ConsoleColor.Red, "", "You entered an invalid user.");
goto Login;
}
}

// Prevent console from closing
Console.WriteLine();
Console.WriteLine("Press Enter key to exit");
Console.WriteLine("\nPress Enter key to exit");
Console.ReadLine();
}

public static User validUserName(List<User> users, String input)
{
foreach (User user in users)
{
if (user.Name == input)
{
return user;
}
}

return null;
}

public static User validPassword(List<User> users, String name, String password)
{
foreach (User user in users)
{
if (user.Name == name && user.Pwd == password)
{
return user;
}
}

return null;
}

public static void printProductsList(List<Product> products)
{
Console.WriteLine("\nWhat would you like to buy?");
foreach (Product product in products)
{
Console.WriteLine(products.IndexOf(product) + 1 + ": " + product.Name + " (" + product.Price.ToString("C") + ")");
}
Console.WriteLine(products.Count + 1 + ": Exit");
}

public static void printMessage(ConsoleColor color, String messageOne, String messageTwo)
{
Console.Clear();
Console.ForegroundColor = color;
Console.WriteLine(messageOne);
Console.WriteLine(messageTwo);
Console.ResetColor();
}
}

}
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>