0% found this document useful (0 votes)
23 views4 pages

Store Updated

Uploaded by

uofcstudent1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Store Updated

Uploaded by

uofcstudent1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Newtonsoft.Json;
//using Newtonsoft.Json.Serialization;

namespace ICT711_Day5_classes
{
public class Store : IStore
{
public string StoreName { get; set; }
public string Address { get; set; }
[JsonIgnore] // Don't store this data inside the store file. Will be stored in a separate file
public List<ICustomer> Customers { get; set; }
[JsonIgnore] // Don't store this data inside the store file. Will be stored in a separate file
public List<IAssociate> Associates { get; set; }
[JsonIgnore] // Don't store this data inside the store file. Will be stored in a separate file
public Inventory Inventory { get; set; }
[JsonIgnore] // Don't store this data inside the store file. Will be stored in a separate file
public List<ISale> Sales { get; set; }
[JsonProperty]
public static string AssociatesFileName { get; set; } = "associates.json";
[JsonProperty]
public static string CustomersFileName { get; set; } = "customers.json";
[JsonProperty]
public static string InventoryFileName { get; set; } = "inventory.json";
[JsonProperty]
public static string SalesFileName { get; set; } = "sales.json";
public static string StoreFileName { get; set; } = "store.json";

public Store()
{
this.StoreName = "New Store Name"; //JEN: Added
this.Address = "Somewhere address"; //JEN: Added
}

public void LoadAssociates()


{
if (!File.Exists(AssociatesFileName))
{
Associates = new List<Associate>().ConvertAll(a => (IAssociate)a);
}
string jsonString = File.ReadAllText(AssociatesFileName);
var ass = JsonConvert.DeserializeObject<List<Associate>>(jsonString, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
Associates = ass.ConvertAll(c => (IAssociate)c); // Typecasting for each element
//return;
// throw new NotImplementedException();

public void LoadCustomers()


{
if(!File.Exists(CustomersFileName))
{
Customers = new List<Customer>().ConvertAll(a => (ICustomer)a);
return;
}
string jsonString = File.ReadAllText(CustomersFileName);
var cs = JsonConvert.DeserializeObject<List<Customer>>(jsonString, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
Customers = cs.ConvertAll(c => (ICustomer)c);
// return;
// throw new NotImplementedException();
}

public void LoadInventory()


{
if (!File.Exists(InventoryFileName))
{
Inventory = new Inventory();
return;
}

string jsonString = File.ReadAllText(InventoryFileName);


var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
};
Inventory = JsonConvert.DeserializeObject<Inventory>(jsonString, settings);
}

public void LoadSales()


{
if (!File.Exists(SalesFileName))
{
Sales = new List<ISale>(); // Initialize an empty list if the sales file does not exist.
return;
}

string jsonString = File.ReadAllText(SalesFileName);


var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
};
Sales = JsonConvert.DeserializeObject<List<ISale>>(jsonString, settings);
}

public void SaveAssociates()


{
string jsonString = JsonConvert.SerializeObject(Associates, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
File.WriteAllText(AssociatesFileName, jsonString);
return;
//throw new NotImplementedException();
}

public void SaveCustomers()


{
string jsonString = JsonConvert.SerializeObject(Customers, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
File.WriteAllText(CustomersFileName, jsonString);
return;
}

public void SaveInventory()


{
string jsonString = JsonConvert.SerializeObject(Inventory, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
File.WriteAllText(InventoryFileName, jsonString);
return;
}

public void SaveSales()


{
string jsonString = JsonConvert.SerializeObject(Sales, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
File.WriteAllText(SalesFileName, jsonString);
return;
}

public void SaveStoreInfo(string StoreDataFileName = null)


{
Dictionary<string, string> StoreDetails = new Dictionary<string, string>();
StoreDetails[StoreName] = Address;

string jsonString = JsonConvert.SerializeObject(StoreDetails, Formatting.Indented, new JsonSerializerSettings


{
TypeNameHandling = TypeNameHandling.Auto
});
File.WriteAllText("store.json", jsonString);

public static Store CreateStore() // Factory method


{
if (!File.Exists(StoreFileName)) // in case no file exists
{
return new Store();
}
string jsonString = File.ReadAllText(StoreFileName);
return JsonConvert.DeserializeObject<Store>(jsonString, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});

}
}
}

You might also like