0% found this document useful (0 votes)
21 views9 pages

CODE

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)
21 views9 pages

CODE

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/ 9

INVENTORY

using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Net.NetworkInformation; //JEN: Removed
//using System.Reflection.Metadata.Ecma335; //JEN: Removed
using static ICT711_Day5_classes.ProductGarment; //JEN: Added

namespace ICT711_Day5_classes
{
public class Inventory : IInventory
{
private List<Product> products;

public List<Product> Products


{
get
{
if (products == null) products = new List<Product>();
return products;
}

set { products = value; }


}

//JEN: Revised
public List<IProduct> this[string search]
{
get
{
List<IProduct> productsOut = new List<IProduct>();

foreach (var p in this.Products)


if (p.ProductName.ToLower().Contains(search.ToLower())) productsOut.Add(p);

return productsOut;
}
}

public IProduct this[int productId]


{
get
{
foreach (var p in this.Products)
{
if (p.ProductId == productId) return p;
}

throw new Exception("Invalid productId");


}
}

/*public Inventory()
{
products = new List<Product>();
}
public List<IProduct> this[string search]
{
get
{
return products.Where(p => p.ProductName.Contains(search)).ToList<IProduct>();
}
}
public IProduct this[int productID]
{
get
{
return products.FirstOrDefault(p => p.ProductId == productID);
}
}

public List<IProduct> Products //=> Products.Cast<IProduct>().ToList();


{
get { return products.Cast<IProduct>().ToList(); }
}
List<Product> IInventory.Products => throw new NotImplementedException(); */

public bool AddProduct(Product product)


{
foreach (var p in Products)
{
if (p.ProductId == product.ProductId)
{
p.Add(product);
return true;
};
}

Products.Add(product);
return true;
}
/*public bool AddProduct(Product product)
{
//throw new NotImplementedException();

Product existingProduct = products.FirstOrDefault(p => p.ProductId == product.ProductId);


if (existingProduct != null)
{
existingProduct.Quantity += product.Quantity;
return false;
}
else
{
products.Add(product);
return true;
}
}*/

public bool RemoveProduct(Product product)


{
foreach (var p in this.Products)
{
if (p.ProductId == product.ProductId)
{
if (p.Quantity >= product.Quantity)
{
p.Remove(product);
return true;
}
else throw new ArgumentOutOfRangeException("Quantity", "There is not enough quantity.");
};
}

throw new Exception("Product is not in inventory.");


}

/*public bool RemoveProduct(Product product)


{
// throw new NotImplementedException();

// FIRST CODE
Product existingProduct = products.FirstOrDefault(p => p.ProductId == product.ProductId);
if (existingProduct == null)
{
throw new InvalidOperationException("Product is not in inventory.");
}

if (existingProduct is ProductGarment existingGarment && product is ProductGarment removeGarment)


{
foreach (var entry in removeGarment.SizePriceQuantity)
{
string size = entry.Key;
int quantity = entry.Value.quantity;
decimal price = entry.Value.price;

if (!existingGarment.SizePriceQuantity.ContainsKey(size) ||
existingGarment.SizePriceQuantity[size].quantity < quantity)
{
throw new ArgumentOutOfRangeException($"There is not enough quantity of size {size}");
}

existingGarment.AddQuantity(size, -quantity, price);


}
}

else
{
if (existingProduct.Quantity < product.Quantity)
{
throw new ArgumentOutOfRangeException((existingProduct.Quantity.ToString()), $"There is not enough
quantity.");
}
if (existingProduct.Quantity == product.Quantity)
{
products.Remove(existingProduct);
}
else
{
existingProduct.Quantity -= product.Quantity;
}
}
return true;
}*/

public static Inventory operator +(Inventory inventory, Product product)


{

inventory.AddProduct(product);
return inventory;
}

public static Inventory operator -(Inventory inventory, Product product)


{

inventory.RemoveProduct(product);
return inventory;
}

}
}

SALE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ICT711_Day5_classes
{
public class Sale : ISale

{
public int Id { get; set; }
public DateTime Date { get; set; }
public int CustomerId { get; set; }
public int AssociateId { get; set; }

public Sale()
{
this.Id = new Random().Next();
}

public Sale(int customerId, int associateId, SaleStatus status)


{
this.Id = new Random().Next();
this.CustomerId = customerId;
this.AssociateId = associateId;
this.Status = status;
}

public Sale(int id, DateTime date, int customerId, int associateId, SaleStatus status)
{
this.Id = id;
this.Date = date;
this.CustomerId = customerId;
this.AssociateId = associateId;
this.Status = status;
}

private List<Product> productsList;

public List<Product> ProductsList


{
get
{
if (productsList == null) productsList = new List<Product>();
return productsList;
}
}

public SaleStatus Status { get; set; }

public int AddProduct(Product product, IInventory inventory)


{
Product inventoryProduct = (Product)inventory[product.ProductId];
if (inventoryProduct == null) throw new Exception("Invalid product");
if (inventoryProduct.Quantity < product.Quantity) throw new Exception("Insufficient quantity");

foreach (var p in ProductsList)


{
if (p.ProductId == product.ProductId)
{

inventory.RemoveProduct(product);
p.Add(product);

return 1;
};
}

inventory.RemoveProduct(product);
ProductsList.Add(product);

return 1;
}

public decimal GetTotal()


{
decimal total = 0;

foreach (Product product in ProductsList)


{
total += product.Price * product.Quantity;
}

return total;
}

public int RemoveProduct(Product product, IInventory inventory)


{
foreach (Product saleProduct in ProductsList)
{
if (saleProduct.ProductId == product.ProductId)
{
if (saleProduct.Quantity < product.Quantity) throw new Exception("Insufficient quantity");

saleProduct.Remove(product);
inventory.AddProduct(product);
return 1;
}
}

throw new Exception("Invalid product");


}

}
}

STORE
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