In: Computer Science
Question: In C#: For today's lab you will be creating a store inventory management system. Your program will have to have the following elements. It should allow any number of customers to shop at the store using names to identify each one. The user should be able to select which one is the current customer. The system must contain a list of at least 50 items (repeats are allowed) that can be purchased by the customers. For a customer to make a purchase they must transfer the items they wish to buy to their own shopping cart. (A Shopping cart list should be maintained for each customer). The user should be able to switch between customers without losing the contents of each customer's cart. The user can select complete purchase and will be presented with a total for that user’s purchases. Customers should be able to remove items from their cart and return them to the stores inventory. A customer’s cart should be removed after her/his purchase is complete. NOTE: The code structure and guidelines are light because this exercise is designed to test your critical thinking skills and see how you apply the skills you’ve learned throughout the duration of this class.
Use the following guidelines to complete this application:
Classes
Classes should be used to represent Inventory Items
Customers
List(s)
Lists should be used to represent The stores inventory Customers shopping carts
Dictionary
A Dictionary should be used to Track all of the customers - identified by their name
User Options
The user should have the following options:
Select current shopper - list of all shoppers and option to create another shopper
View store inventory - list everything the store is selling
View cart - list of everything in the current Customers cart
Add item to cart - allow the user to select an item to add to the current Customer’s cart and remove it from the store’s inventory. (Can be combined with the View store option if you wish) Remove item from cart - allow the user to select an item to add to the stores inventory and remove it from the current Customer’s cart. (can be combined with the View cart option if you wish)
Complete purchase - Total up the cost of all of the items in the user’s cart and display that value. Remove the customer from the dictionary of customers
Exit - Exit the program
Input
All input should be validated and limited to the options presented to the user The user should not be able to crash the program Program should continue to run until the user chooses to exit File IO
Generate a "receipt" for the customer when they purchase their items.
The receipt contains: Customer name Time of transaction List of items purchased (name & cost) Total cost of items Feel free to add a sub-total and tax (not required). A new receipt file is generated for each completed purchase. Do not overwrite or append to a previous file. The file name: Indicates which customer completed the purchase. What order the receipts were generated. There is more than one good solution to this requirement, do not seek help for this feature - use critical thinking.
CODE:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShopperCart
{
// Inventory class
class Inventory
{
public string Name { get; set; }
public double Price { get; set; }
}
// Customer Class
class Customer
{
public string Name { get; set; }
public List<Inventory> Cart { get; set; }
}
class Program
{
// global scoped inventory collection
private static Dictionary<string, Inventory>
_inventoryCol;
// global scoped customer collection
private static Dictionary<string, Customer>
_customerCol;
static void Main(string[] args)
{
// intialise the customer collection
_customerCol = new Dictionary<string, Customer>();
// load inventory collection
LoadInventory();
// start of the menu
// take name to add to our system
Console.WriteLine("Please enter the customer name add or switch to
your collection");
string tempName = Console.ReadLine().Trim();
// add name and make it acrive customer
string activeCutomer=AddCustomer(tempName);
// menu loop
while (true)
{
//Display the inventory list
ShowInventory();
Console.WriteLine("Please enter the Inventory name to add to your
collection");
string tempInven = Console.ReadLine().Trim();
// add selected inventory item to customer cart
AddInventoryToCustomer(_customerCol[activeCutomer],tempInven);
// Menu to ask user to see the cart
Console.WriteLine("Type Yes to view Cart, nothing to
continue");
string viewCart = Console.ReadLine().Trim();
if (viewCart.ToLower() == "yes")
ViewCurrentCustCart(activeCutomer);
// Menu to ask user to generate the receipt
Console.WriteLine("Type Yes to generate script, nothing to
continue");
string genReceipt = Console.ReadLine().Trim();
if(genReceipt.ToLower()=="yes")
GenerateReceipt(activeCutomer);
// Menu to ask user to remove particular customer
Console.WriteLine("Want to remove any customer from the customer
list?, Type name to remove, nothing to continue");
string deleteName = Console.ReadLine().Trim();
if (!string.IsNullOrEmpty(deleteName))
RemoveFromCustomerCollection(deleteName);
// Menu to ask user to exit the menu
Console.WriteLine("Press X to exit, any other key to continue
adding item to cart");
string exitKey = Console.ReadLine().Trim();
if (exitKey.ToLower() == "x")
break;
}
}
// removes user from the customer collection
private static void RemoveFromCustomerCollection(string
deleteName)
{
if (_customerCol.ContainsKey(deleteName))
{
_customerCol.Remove(deleteName);
Console.WriteLine("Successfully removed Customer: {0} from our
system",deleteName);
}
else
{
Console.WriteLine("Supplied Customer to remove is not in our
system");
}
}
// generate script based on the cart items and names it to the
cutomer Receipt CustomerName_currentDateTime.txt
private static void GenerateReceipt(string activeCutomer)
{
// check the presence
if (_customerCol[activeCutomer].Cart.Count > 0)
{
string content = string.Format("Name : {0}{1}", activeCutomer,
Environment.NewLine);
content+=
string.Format("Name\t\tPrice{0}",Environment.NewLine);
double total=0;
foreach (Inventory item in _customerCol[activeCutomer].Cart)
{
total += item.Price;
content += string.Format("{0}\t\t{1}{2}", item.Name, item.Price,
Environment.NewLine);
}
content += string.Format("Total\t\t{0}", total);
string path = string.Format("Receipt_{0}_{1}.txt", activeCutomer,
DateTime.Now.ToLongDateString());
File.WriteAllText(path, content);
// write to text file
Console.WriteLine("Receipt with name : {0} generated succesfully.",
path);
}
}
// view the current status of Cart for active customer
private static void ViewCurrentCustCart(string activeCutomer)
{
if (_customerCol[activeCutomer].Cart.Count > 0)
{
Console.WriteLine("Name\t\tPrice");
foreach (Inventory item in _customerCol[activeCutomer].Cart)
{
Console.WriteLine("{0}\t\t{1}", item.Name, item.Price);
}
}
}
// Add inventory item to the active customer's cart
private static void AddInventoryToCustomer(Customer customer,
string tempInven)
{
if (_inventoryCol.ContainsKey(tempInven))
{
customer.Cart.Add(_inventoryCol[tempInven]);
}
else
{
Console.WriteLine("Provided Inventory Item: {} is not present in
our system, please try again");
}
}
// Add customer to our customer collection
private static string AddCustomer(string name)
{
if(_customerCol.ContainsKey(name))
{
Console.WriteLine("Customer is already in our system, we are making
it active.");
return name;
}
else
{
Console.WriteLine("Customer is new to our system, we are adding it
and making it active.");
_customerCol.Add(name, new Customer() { Name = name, Cart = new
List<Inventory>() });
return name;
}
}
// system initiated inventory loader
private static void LoadInventory()
{
List<string> items = new List<string>()
{"Phone","Battery","Laptop","Book","Mouse","Wallet","Item1","bottle","paper","can","itemx","item
t"};
Random rand = new Random();
_inventoryCol = new Dictionary<string, Inventory>();
foreach (string item in items)
{
_inventoryCol.Add(item, new Inventory() { Name = item, Price
=rand.Next(100,12345)});
}
}
// display the inventory list
private static void ShowInventory()
{
if (_inventoryCol.Count > 0)
{
Console.WriteLine("Name\t\tPrice");
foreach (KeyValuePair<string,Inventory> item in
_inventoryCol)
{
Console.WriteLine("{0}\t\t{1}", item.Value.Name,
item.Value.Price);
}
}
}
}
}
RESULT:


Receipt:
Name : Alex
Name Price
Wallet 8335
bottle 6137
can 8112
Total 22584