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
List(s)
Dictionary
User Options
Input
File IO
The programming language is C#.
Executable
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
For today's lab you will be creating a store inventory management system. Your program will have...