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
26 changes: 26 additions & 0 deletions Refactoring/FileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace Refactoring
{
class FileManager
{
public static List<T> LoadJsonFile<T>(string fileName)
{
var fileText = File.ReadAllText(fileName);

return JsonConvert.DeserializeObject<List<T>>(fileText);
}

public static void SaveJsonFile<T>(string fileName, List<T> list)
{
string json = JsonConvert.SerializeObject(list, Formatting.Indented);
File.WriteAllText(fileName, json);
}
}
}
12 changes: 11 additions & 1 deletion Refactoring/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class Product
[JsonProperty("Price")]
public double Price;
[JsonProperty("Quantity")]
public int Qty;
public int Quantity;

internal double GetPrice(int selectedQuantity)
{
return (Price * selectedQuantity);
}

internal bool HasEnoughInventory(int selectedQuantity)
{
return (Quantity >= selectedQuantity);
}
}
}
10 changes: 4 additions & 6 deletions Refactoring/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Refactoring
{
Expand All @@ -12,12 +9,13 @@ public class Program
public static void Main(string[] args)
{
// Load users from data file
List<User> users = JsonConvert.DeserializeObject<List<User>>(File.ReadAllText(@"Data/Users.json"));
List<User> users = FileManager.LoadJsonFile<User>(@"Data/Users.json");

// Load products from data file
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(File.ReadAllText(@"Data/Products.json"));
List<Product> products = FileManager.LoadJsonFile<Product>(@"Data/Products.json");

Tusc.Start(users, products);
var tusc = new Tusc(users, products);
tusc.Start();
}
}
}
2 changes: 2 additions & 0 deletions Refactoring/Refactoring.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileManager.cs" />
<Compile Include="Product.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tusc.cs" />
<Compile Include="User.cs" />
<Compile Include="UserInterface.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
Loading