Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C#:For today\'s lab you will be creating a store inventory management system. Yo

ID: 3749331 • Letter: C

Question

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.

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

Explanation / Answer

Solution:

There are 2 parts in the question. i have answered both the parts.

The code fulfils all the requirements mentioned and runs fine.

For part 1, the Classes are in following order.

InventoryItem

Customer

Logger

Program

For part 2, the Classes are in following order.

ILog

Logger

DoNotLog

LogToConsole

Car

Program

Please upvote my answer. Thanks in advance.

PART 1 :-

Class InventoryItem :

namespace Chegg_InventoryManagement
{
class InventoryItem
{
internal string Name { get; set; }
internal int Price { get; set; }
}
}

Class Customer :

using System.Collections.Generic;

namespace Chegg_InventoryManagement
{
class Customer
{
internal string Name { get; set; }
internal List<InventoryItem> ShoppingCart { get; set; }
}
}

Class Logger:

using System;
using System.IO;

namespace Chegg_InventoryManagement
{
static class Logger
{
internal static string logFilePath = Path.Combine(Environment.CurrentDirectory, "log.txt");

static Logger()
{
if (File.Exists(Logger.logFilePath))
{
File.Delete(Logger.logFilePath);
}
}

internal static void LogCustomerName(string customerName)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(logFilePath, true))
{
file.WriteLine("Current customer name : " + customerName);
}
}

internal static void LogItem(string itemName, string location)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(logFilePath, true))
{
file.WriteLine("Item : " + itemName + " Location : " + location);
}
}

internal static void LogCustomerPurchase(string customerName, string totalPurchase)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(logFilePath, true))
{
file.WriteLine("Customer name : " + customerName + " Total purchase : " + totalPurchase);
}
}
}
}

ClassProgram :

using System;
using System.Collections.Generic;
using System.Linq;

namespace Chegg_InventoryManagement
{
class Program
{
const string NEWKeyword = "NEW";

static List<InventoryItem> StoreInventory = new List<InventoryItem>
{
new InventoryItem{Name = "a1", Price = 1},
new InventoryItem{Name = "a2", Price = 2},
new InventoryItem{Name = "a3", Price = 3},
new InventoryItem{Name = "a4", Price = 4},
new InventoryItem{Name = "a5", Price = 5},
new InventoryItem{Name = "a6", Price = 6},
new InventoryItem{Name = "a7", Price = 7},
new InventoryItem{Name = "a8", Price = 8},
new InventoryItem{Name = "a9", Price = 9},
new InventoryItem{Name = "a10", Price = 10},
new InventoryItem{Name = "a11", Price = 11},
new InventoryItem{Name = "a12", Price = 12},
new InventoryItem{Name = "a13", Price = 13},
new InventoryItem{Name = "a14", Price = 14},
new InventoryItem{Name = "a15", Price = 15},
new InventoryItem{Name = "a16", Price = 16},
new InventoryItem{Name = "a17", Price = 17},
new InventoryItem{Name = "a18", Price = 18},
new InventoryItem{Name = "a19", Price = 19},
new InventoryItem{Name = "a20", Price = 20},
new InventoryItem{Name = "a21", Price = 21},
new InventoryItem{Name = "a22", Price = 22},
new InventoryItem{Name = "a23", Price = 23},
new InventoryItem{Name = "a24", Price = 24},
new InventoryItem{Name = "a25", Price = 25},
new InventoryItem{Name = "a26", Price = 26},
new InventoryItem{Name = "a27", Price = 27},
new InventoryItem{Name = "a28", Price = 28},
new InventoryItem{Name = "a29", Price = 29},
new InventoryItem{Name = "a30", Price = 30},
new InventoryItem{Name = "a31", Price = 31},
new InventoryItem{Name = "a32", Price = 32},
new InventoryItem{Name = "a33", Price = 33},
new InventoryItem{Name = "a34", Price = 34},
new InventoryItem{Name = "a35", Price = 35},
new InventoryItem{Name = "a36", Price = 36},
new InventoryItem{Name = "a37", Price = 37},
new InventoryItem{Name = "a38", Price = 38},
new InventoryItem{Name = "a39", Price = 39},
new InventoryItem{Name = "a40", Price = 40},
new InventoryItem{Name = "a41", Price = 41},
new InventoryItem{Name = "a42", Price = 42},
new InventoryItem{Name = "a43", Price = 43},
new InventoryItem{Name = "a44", Price = 44},
new InventoryItem{Name = "a45", Price = 45},
new InventoryItem{Name = "a46", Price = 46},
new InventoryItem{Name = "a47", Price = 47},
new InventoryItem{Name = "a48", Price = 48},
new InventoryItem{Name = "a49", Price = 49},
new InventoryItem{Name = "a50", Price = 50},
new InventoryItem{Name = "a51", Price = 51},
new InventoryItem{Name = "a52", Price = 52},
new InventoryItem{Name = "a53", Price = 53},
new InventoryItem{Name = "a54", Price = 54},
new InventoryItem{Name = "a55", Price = 55},
new InventoryItem{Name = "a56", Price = 56},
new InventoryItem{Name = "a57", Price = 57},
new InventoryItem{Name = "a58", Price = 58},
new InventoryItem{Name = "a59", Price = 59},
new InventoryItem{Name = "a60", Price = 60}
};

static List<Customer> Customers = new List<Customer>();

static Dictionary<string, Customer> ActiveCustomers = new Dictionary<string, Customer>();
static string OptionSeleted;
static Customer CurrentCustomer = null;

static void Main(string[] args)
{
Console.WriteLine("Welcome to the Store");
while (AskUser())
{

}
}

private static bool AskUser()
{
OptionSeleted = GetUserOption();
switch (OptionSeleted)
{
case "Select current shopper":
GetCustomer();
break;

case "View store inventory":
ViewStoreinventory();
break;

case "View cart":
ViewCart();
break;

case "Add item to cart":
AddItemToCart();
break;

case "Remove item from cart":
RemoveItemFromCart();
break;

case "Complete purchase":
CompletePurchase();
break;

case "Exit":
return false;
}
return true;
}

private static string GetUserOption()
{
Console.WriteLine("Select any option below(Select corresponding number) : ");
Console.WriteLine("1 - Select current shopper");
Console.WriteLine("2 - View store inventory");
Console.WriteLine("3 - View cart");
Console.WriteLine("4 - Add item to cart");
Console.WriteLine("5 - Remove item from cart");
Console.WriteLine("6 - Complete purchase");
Console.WriteLine("7 - Exit");

string option = Console.ReadLine().ToLower();
switch (option)
{
case "1":
case "select current shopper":
return "Select current shopper";

case "2":
case "view store inventory":
return "View store inventory";

case "3":
case "view cart":
return "View cart";

case "4":
case "add item to cart":
return "Add item to cart";

case "5":
case "remove item from cart":
return "Remove item from cart";

case "6":
case "complete purchase":
return "Complete purchase";

case "7":
case "exit":
return "Exit";
}

return "Exit";
}

private static void GetCustomer()
{
Console.WriteLine("Here is the list of customers - ");
if (Customers.Count > 0)
{
foreach (var customer in Customers)
{
Console.WriteLine(customer.Name);
}
Console.WriteLine("");
Console.WriteLine("Select any of the above customers by entering the Name");
Console.WriteLine("OR");
}
else
{
Console.WriteLine("Currently there are no customers");
}
Console.WriteLine("Add a new customer by entering " + NEWKeyword);

string input = Console.ReadLine();

if (input.Equals(NEWKeyword, StringComparison.InvariantCultureIgnoreCase))
{
Console.Write("Please enter the customer name : ");
string name = Console.ReadLine();
while (Customers.Exists(customer => customer.Name == name))
{
if(name.Equals(NEWKeyword))
{
Console.WriteLine("Customer name cannot be NEW");
}
else
{
Console.WriteLine("Customer already exists");
}
Console.Write("Please enter the customer name : ");
name = Console.ReadLine();
}
Customer newCustomer = new Customer { Name = name, ShoppingCart = new List<InventoryItem>() };
Customers.Add(newCustomer);
CurrentCustomer = newCustomer;
}
else
{
CurrentCustomer = Customers.FirstOrDefault(customer => customer.Name == input);
}

if (CurrentCustomer == null)
{
Console.WriteLine("No such customer found!");
AskUser();
}
if (!ActiveCustomers.ContainsKey(CurrentCustomer.Name))
{
ActiveCustomers.Add(CurrentCustomer.Name, CurrentCustomer);
}
Console.WriteLine("Current customer name : " + CurrentCustomer.Name);
Logger.LogCustomerName(CurrentCustomer.Name);
}

private static void ViewStoreinventory()
{
Console.WriteLine("Items in Store - ");
foreach (var inventoryItem in StoreInventory)
{
Console.WriteLine("Item : " + inventoryItem.Name + " Price : " + inventoryItem.Price);
}
Console.WriteLine("");
}

private static void ViewCart()
{
if (CurrentCustomer == null)
{
Console.WriteLine("No customer selected!");
return;
}

Console.WriteLine("Current customer name : " + CurrentCustomer.Name);
Console.WriteLine("Items in shopping cart - ");
if (CurrentCustomer.ShoppingCart.Count == 0)
{
Console.WriteLine("No items in shopping cart!");
}
foreach (var inventoryItem in CurrentCustomer.ShoppingCart)
{
Console.WriteLine("Item : " + inventoryItem.Name + " Price : " + inventoryItem.Price);
}
Console.WriteLine("");
}

private static void AddItemToCart()
{
if (CurrentCustomer == null)
{
Console.WriteLine("No customer selected!");
return;
}

Console.Write("Enter item name :- ");
string itemName = Console.ReadLine();

InventoryItem inventoryItem = StoreInventory.FirstOrDefault(i => i.Name == itemName);
if(inventoryItem == null)
{
Console.WriteLine("No such item found!");
return;
}
if(ActiveCustomers.Keys.Contains(CurrentCustomer.Name))
{
CurrentCustomer.ShoppingCart.Add(inventoryItem);
StoreInventory.Remove(inventoryItem);
}
else
{
Console.WriteLine("No customer selected!");
}
ViewCart();
Logger.LogItem(inventoryItem.Name, "ShoppingCart");
}

private static void RemoveItemFromCart()
{
if (CurrentCustomer == null)
{
Console.WriteLine("No customer selected!");
return;
}

Console.Write("Enter item name :- ");
string itemName = Console.ReadLine();

InventoryItem inventoryItem = CurrentCustomer.ShoppingCart.FirstOrDefault(i => i.Name == itemName);
if (inventoryItem == null)
{
Console.WriteLine("No such item found!");
AskUser();
}
StoreInventory.Add(inventoryItem);
CurrentCustomer.ShoppingCart.Remove(inventoryItem);
ViewCart();
Logger.LogItem(inventoryItem.Name, "Store Inventory");
}

private static void CompletePurchase()
{
if (CurrentCustomer == null)
{
Console.WriteLine("No customer selected!");
return;
}

int totalValue = 0;
foreach(var item in CurrentCustomer.ShoppingCart)
{
totalValue += item.Price;
}

Console.WriteLine("Total Value : " + totalValue);
Console.WriteLine("Purchase Complete");
ActiveCustomers.Remove(CurrentCustomer.Name);
Logger.LogCustomerPurchase(CurrentCustomer.Name, totalValue.ToString());
}
}
}

PART 2 :-

Class ILog :

namespace Chegg_CarManager
{
interface ILog
{
void Log(string info);
void LogD(string info);
void LogW(string info);
}
}

Class Logger :

namespace Chegg_CarManager
{
abstract class Logger : ILog
{
protected static int lineNumber;

public abstract void Log(string info);

public abstract void LogD(string info);

public abstract void LogW(string info);
}
}

Class DoNotLog :

namespace Chegg_CarManager
{
class DoNotLog : Logger
{
public override void Log(string info)
{
}

public override void LogD(string info)
{
}

public override void LogW(string info)
{
}
}
}

Class LogToConsole :

using System;

namespace Chegg_CarManager
{
class LogToConsole : Logger
{
public override void Log(string info)
{
Console.WriteLine(lineNumber + " : " + info);
lineNumber++;
}

public override void LogD(string info)
{
Console.WriteLine(lineNumber + " : " + "DEBUG - " + info);
lineNumber++;
}

public override void LogW(string info)
{
Console.WriteLine(lineNumber + " : " + "WARNING - " + info);
lineNumber++;
}
}
}

Class Car :

namespace Chegg_CarManager
{
class Car
{
string make;
string model;
string color;
float mileage;
int year;

internal Car(string make, string model, string color, float mileage, int year)
{
Logger logger = Program.GetLogger();
if (logger != null)
{
this.make = make;
logger.LogD(make);
this.model = model;
logger.LogD(model);
this.color = color;
logger.LogD(color);
this.mileage = mileage;
logger.LogD(mileage.ToString());
this.year = year;
logger.LogD(year.ToString());
}
}

internal void Drive(float distance)
{
this.mileage = distance;
Logger logger = Program.GetLogger();
if (logger != null)
{
logger.LogW(distance.ToString());
}
}
}
}

ClassProgram :

using System;

namespace Chegg_CarManager
{
class Program
{
static Logger logger = null;
static Car currentCar = null;

static string OptionSeleted;

public static Logger GetLogger()
{
return logger;
}

static void Main(string[] args)
{
Console.WriteLine("Welcome!");
while (AskUser())
{

}
}

private static bool AskUser()
{
OptionSeleted = GetUserOption();
switch (OptionSeleted)
{
case "Disable logging":
GetDoNotLogLogger();
break;

case "Enable logging":
GetLogToConsoleLogger();
break;

case "Create a car":
CreateCar();
break;

case "Drive the car":
DriveCar();
break;

case "Destroy the car":
DestroyCar();
break;

case "Exit":
return false;
}
return true;
}

private static string GetUserOption()
{
Console.WriteLine("Select any option below(Select corresponding number or Type the option) : ");
Console.WriteLine("1 - Disable logging");
Console.WriteLine("2 - Enable logging");
Console.WriteLine("3 - Create a car");
Console.WriteLine("4 - Drive the car");
Console.WriteLine("5 - Destroy the car");
Console.WriteLine("6 - Exit");

string option = Console.ReadLine().ToLower();
switch (option)
{
case "1":
case "disable logging":
return "Disable logging";

case "2":
case "enable logging":
return "Enable logging";

case "3":
case "create a car":
return "Create a car";

case "4":
case "drive the car":
return "Drive the car";

case "5":
case "destroy the car":
return "Destroy the car";

case "6":
case "exit":
return "Exit";
}

Console.WriteLine("Invalid option!");
return "";
}

private static void GetDoNotLogLogger()
{
logger = new DoNotLog();
Console.WriteLine("Logging disabled!");
}

private static void GetLogToConsoleLogger()
{
logger = new LogToConsole();
Console.WriteLine("Logging enabled!");
}

private static void CreateCar()
{
Console.WriteLine("Please enter the new car details below : ");
Console.Write(" Please enter car make : ");
string make = Console.ReadLine();
Console.Write(" Please enter car model : ");
string model = Console.ReadLine();
Console.Write(" Please enter car color : ");
string color = Console.ReadLine();
Console.Write(" Please enter car mileage : ");
float mileage;
while(!float.TryParse(Console.ReadLine(), out mileage))
{
Console.WriteLine(" Invalid value! Please enter again.");
Console.Write(" Please enter car mileage : ");
}
Console.Write(" Please enter car year : ");
int year;
while(!int.TryParse(Console.ReadLine(), out year))
{
Console.WriteLine(" Invalid value! Please enter again.");
Console.Write(" Please enter car year : ");
}
Car newCar = new Car(make, model, color, mileage, year);
currentCar = newCar;
Console.WriteLine("New car created!");
}

private static void DriveCar()
{
if (currentCar == null)
{
Console.WriteLine("No car created yet!");
return;
}
Console.Write("Please enter the distance covered : ");
float distance;
while(!float.TryParse(Console.ReadLine(), out distance))
{
Console.WriteLine("Invalid value! Please enter again.");
Console.Write("Please enter the distance covered : ");
}
currentCar.Drive(distance);
Console.WriteLine("Car was driven!");
}

private static void DestroyCar()
{
if (currentCar == null)
{
Console.WriteLine("No car created yet!");
return;
}
currentCar = null;
}
}
}