--USING C# ONLY-- You will create your own on-line shopping store in the console application. You can choose different products to sell in your store (at least 8 products). You will create an product inventory text file. The program will display two options at the beginning of the program, Customer and Manager. Customer: If this is a new customer, the program will allow the customer to register with their information and assign a unique ID number to the new customer. The customer will enter the customer id every time when they are shopping in your store and the program will keep tracking the total amount the customer spends each time to determine the discount level. You can create your discount level rule or use the rule in the assignment 4. The program has to keep tracking the products inventory. If the customer selects the product that is out of stock, the program has to display a warning message and let customer continue to shop. After the customer finishes selecting the products, the program will calculate the subtotal, apply the discount, calculate the tax, and the total. The program will display a receipt on the screen and save it to a text file with customer information. (Just like a standard store receipt). Manager: The manager option has to be password protected. The program will give the manager two options: 1. Display the inventory: display all the products stock information. 2. Restock the products: add more stock for each product. You have to create different classes and utilize the inheritance features. You have to create text files to keep all the information the program needs. You can check some on-line stores to get some ideas (Amazon, Walmart …). Make sure your program does not end unexpectedly and let user have the option to continue.
ANSWER :
GIVEN DATA
CODE:
using System;
namespace OnlineShopping
{
class Program
{
static void Main(string[] args)
{
BusinessLogic BL = new BusinessLogic();
int choice;
do
{
Console.WriteLine("Main Menu :: ");
Console.WriteLine("Press <1> for Customer");
Console.WriteLine("Press <2> for Manager");
Console.WriteLine("Press <3> for Exit");
Console.Write("Choice : ");
choice =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(),
"Choice");
if (choice == 1)
{
BL.ProcessCustomer();
}
else if (choice == 2)
{
BL.ProcessManager();
}
else if (choice == 3)
{
break;
}
else
{
Console.WriteLine("Error :: Invalid Choice!");
}
Console.WriteLine("");
} while (choice != 3);
}
}
}
-------------------- Receipt.cs --------------------
using System;
using System.Collections.Generic;
namespace OnlineShopping
{
class Receipt
{
public int OrderNumber { get; set; } = 1234;
public DateTime OrderDate { get; set; }
public Customer Customer { get; set; }
public List<Inventory> Items { get; set; }
public double Subtotal { get; set; }
public double Tax { get; set; }
public double Discount { get; set; }
public double Total { get; set; }
public void ProcessCalculation()
{
for (int i = 0; i < Items.Count; i++)
{
Subtotal += Items[i].Quantity * Items[i].Price;
}
Tax = Subtotal * (Inventory.Tax / 100.0);
if (Customer.DiscountAllotted != 0)
{
Discount = Subtotal * (Customer.DiscountAllotted / 100.0);
}
Total = (Subtotal + Tax) - Discount;
this.Customer.TotalSpend += Total;
}
}
}
-------------------- Manager.cs --------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineShopping
{
class Manager
{
public string Name { get; set; }
public string Password { get; private set; } = "password123";
public bool IsManagerAuthorised()
{
Console.Write("Enter manager password : ");
string password = Console.ReadLine();
if (Password == password)
{
return true;
}
return false;
}
public static void
ShowStockInformation(List<Inventory> Items)
{
if (Items.Count > 0)
{
string header = "Code".PadRight(6) + "Name".PadRight(20) +
"Quantity".PadRight(10) + "Price";
Console.WriteLine(header);
Console.WriteLine("".PadRight(44, '-'));
for (int i = 0; i < Items.Count; i++)
{
Inventory inv = Items[i];
string code = inv.ItemNumber.ToString().PadRight(6);
string name = inv.Name.PadRight(20);
string quantity = inv.Quantity.ToString().PadLeft(8);
string price = "$".PadLeft(3) + inv.Price.ToString("0.00");
string line = code + name + quantity + price;
Console.WriteLine(line);
}
}
else
{
Console.WriteLine("Error :: No items in the inventory!");
}
}
public List<Inventory>
Restock(List<Inventory> Items)
{
if (Items.Count > 0)
{
int choice;
do
{
Console.WriteLine("\nSub Menu :: ");
Console.WriteLine("Press <1> for new inventory item");
Console.WriteLine("Press <2> for existing inventory
item");
Console.WriteLine("Press <3> Goto Main menu");
Console.Write("Enter your choice : ");
choice =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(),
"Choice");
if (choice == 1)
{
Inventory inv = new Inventory();
Console.Write("Enter item number : ");
inv.ItemNumber =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(), "item
number");
Console.Write("Enter item name : ");
inv.Name = Console.ReadLine();
Console.Write("Enter item price : $");
inv.Price = BusinessLogic.AcceptUntilValidInput(Console.ReadLine(),
"item price");
Console.Write("Enter item quantity : ");
inv.Quantity =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(), "item
quantity");
Items.Add(inv);
Console.WriteLine("Item updated successfully!");
}
else if (choice == 2)
{
Console.Write("Enter item number :");
int itemNumber =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(), "item
number");
var item = Items.Find(x => x.ItemNumber == itemNumber);
if (item != null)
{
Console.Write("Enter item quantity : ");
int Quantity =
(int)BusinessLogic.AcceptUntilValidInput(Console.ReadLine(), "item
quantity");
Items.Find(x => x.ItemNumber == item.ItemNumber).Quantity +=
Quantity;
Console.WriteLine("Item updated successfully!");
}
else
{
Console.WriteLine("Error :: Item doesn't exist!");
}
}
else if (choice == 3)
{
break;
}
else
{
Console.WriteLine("Error :: Invalid choice!");
}
} while (choice != 3);
}
return Items;
}
}
}
-------------------- Inventory.cs --------------------
namespace OnlineShopping
{
class Inventory
{
public const int Tax = 8;
public int ItemNumber { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
}
}
-------------------- Customer.cs --------------------
using System;
using System.IO;
namespace OnlineShopping
{
class Customer
{
public int Id { get; set; } = 999;
public string Name { get; set; }
public string Address { get; set; }
public double TotalSpend { get; set; }
public int DiscountAllotted { get; set; } = 0;
public bool IsCustomerExist(int Id)
{
string filePath = BusinessLogic.FilenameCustomer;
if (File.Exists(filePath) &&
File.ReadAllLines(filePath).Length > 0)
{
string[] lines = File.ReadAllLines(filePath);
for (int i = 0; i < lines.Length; i++)
{
string[] line = lines[i].Split('\t');
int custId = Convert.ToInt32(line[0]);
if (custId == Id)
{
this.Id = custId;
this.Name = line[1];
this.Address = line[2];
this.DiscountAllotted = Convert.ToInt32(line[3]);
this.TotalSpend = Convert.ToDouble(line[4]);
return true;
}
}
}
else
{
File.WriteAllText(filePath, "");
}
return false;
}
public Customer EnterNewCustomerDetails()
{
string filepath = BusinessLogic.FilenameCustomer;
if (File.Exists(filepath) &&
File.ReadAllLines(filepath).Length > 0)
{
string[] lines = File.ReadAllLines(filepath);
string lastLine = lines[lines.Length-1];
string customerId = lastLine.Split('\t')[0];
Id = Convert.ToInt32(customerId) + 1;
}
else
Id++;
Console.Write("Please enter your name : ");
Name = Console.ReadLine();
Console.Write("Please enter your address : ");
Address = Console.ReadLine();
TotalSpend = DiscountAllotted = 0;
Console.WriteLine("Customer details successfully added!");
Console.WriteLine("Your Customer id is " + Id);
return this;
}
public int ProcessDiscountAlloted()
{
if (this.TotalSpend >= 2000)
this.DiscountAllotted = 10;
else if (this.TotalSpend >= 1500)
this.DiscountAllotted = 8;
else if (this.TotalSpend >= 1000)
this.DiscountAllotted = 6;
else if (this.TotalSpend >= 500)
this.DiscountAllotted = 5;
else
this.DiscountAllotted = 0;
return this.DiscountAllotted;
}
}
}
-------------------- BusinessLogic.cs --------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace OnlineShopping
{
class BusinessLogic
{
public static string baseDirectory,
FilenameInventory,
FilenameCustomer,
FilenameOrder;
public BusinessLogic()
{
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
FilenameInventory = Path.Combine(baseDirectory, "Inventory
Details", "ProductInventory.txt");
FilenameCustomer = Path.Combine(baseDirectory, "Customer Details",
"Customers.txt");
FilenameOrder = Path.Combine(baseDirectory, "Order
Details");
if
(!Directory.Exists(Path.GetDirectoryName(FilenameInventory)))
Directory.CreateDirectory(Path.GetDirectoryName(FilenameInventory));
if
(!Directory.Exists(Path.GetDirectoryName(FilenameCustomer)))
Directory.CreateDirectory(Path.GetDirectoryName(FilenameCustomer));
if (!Directory.Exists(FilenameOrder))
Directory.CreateDirectory(FilenameOrder);
ReadFromFileInventory();
ReadFromFileCustomer();
}
public List<Inventory> Items { get; set; }
public List<Customer> Customers { get; set; }
public static double AcceptUntilValidInput(string input, string
name)
{
double value;
while (true)
{
if (double.TryParse(input, out value))
{
return value;
}
else
{
Console.WriteLine("Error :: Invalid {0}, Try again!", name);
Console.Write("Enter {0} again : ", name);
input = Console.ReadLine();
}
}
}
public void ReadFromFileCustomer()
{
Customers = new List<Customer>();
if (File.Exists(FilenameCustomer))
{
string[] lines = File.ReadAllLines(FilenameCustomer);
for (int i = 0; i < lines.Length; i++)
{
string[] line = lines[i].Split('\t');
Customer cust = new Customer()
{
Id = Convert.ToInt32(line[0]),
Name = line[1],
Address = line[2],
DiscountAllotted = Convert.ToInt32(line[3]),
TotalSpend = Convert.ToDouble(line[4])
};
Customers.Add(cust);
}
}
else
File.WriteAllText(FilenameCustomer, "");
}
public void ReadFromFileInventory()
{
if (File.Exists(FilenameInventory))
{
Items = new List<Inventory>();
string[] lines = File.ReadAllLines(FilenameInventory);
for (int i = 0; i < lines.Length; i++)
{
string[] line = lines[i].Split('\t');
Inventory inv = new Inventory();
inv.ItemNumber = Convert.ToInt32(line[0]);
inv.Name = line[1];
inv.Quantity = Convert.ToInt32(line[2]);
inv.Price = Convert.ToDouble(line[3]);
Items.Add(inv);
}
}
else
{
Console.WriteLine("Error :: File doesn't exist!");
}
}
public void WriteToFileInventory()
{
if (!File.Exists(FilenameInventory))
{
File.WriteAllText(FilenameInventory, "");
}
string[] lines = new string[Items.Count];
for (int i = 0; i < Items.Count; i++)
{
Inventory inv = Items[i];
string line = inv.ItemNumber + "\t" + inv.Name + "\t" +
inv.Quantity + "\t" + inv.Price.ToString("0.00");
lines[i] = line;
}
File.WriteAllLines(FilenameInventory, lines);
}
public void WriteToFileReceipt(Receipt receipt)
{
string filePath = "";
DirectoryInfo d = new DirectoryInfo(FilenameOrder);
FileInfo[] Files = d.GetFiles("*.txt");
FileInfo FI = Files.OrderBy(x => x.Name).FirstOrDefault(x =>
x.Name != "");
if (FI != null)
{
receipt.OrderNumber = Convert.ToInt32(FI.Name.Split(' ')[1]);
receipt.OrderNumber++;
}
filePath = Path.Combine(FilenameOrder, "Order_ID " + receipt.OrderNumber + " For Customer_ID " + receipt.Customer.Id + ".txt");
var lines = new List<string>();
lines.Add("Receipt :: ");
lines.Add("[ABC Company]");
lines.Add("[Address]");
lines.Add("[Phone Number]");
lines.Add("Order ID : " + receipt.OrderNumber);
lines.Add("Order Date : " + receipt.OrderDate.ToString());
lines.Add("Customer ID : " + receipt.Customer.Id);
lines.Add("Customer Name : " + receipt.Customer.Name);
string header = "Name".PadRight(20) + "Quantity".PadRight(10) +
"Price($)".PadLeft(12);
lines.Add(header);
lines.Add("".PadRight(40, '-'));
for (int i = 0; i < receipt.Items.Count; i++)
{
Inventory inv = receipt.Items[i];
string line = inv.Name.PadRight(20) +
inv.Quantity.ToString().PadLeft(8) +
ToCurrencyString(inv.Price).PadLeft(14);
lines.Add(line);
}
lines.Add("");
lines.Add("Subtotal : " +
ToCurrencyString(receipt.Subtotal));
lines.Add("Tax : " + ToCurrencyString(receipt.Tax));
string discountPercentage = "Discount (" +
receipt.Customer.DiscountAllotted + "%)".PadRight(15) + ":";
lines.Add(discountPercentage +
ToCurrencyString(receipt.Discount));
lines.Add("".PadRight(20, '-'));
lines.Add("Total : " + ToCurrencyString(receipt.Total));
lines.Add("".PadRight(20, '-'));
lines.Add("Grand Total Spend : " +
ToCurrencyString(receipt.Customer.TotalSpend));
lines.Add("Future Discount Alloted : " +
receipt.Customer.ProcessDiscountAlloted() + "%");
File.WriteAllLines(filePath, lines);
Console.WriteLine("Successfully updated receipt file on location :
" + filePath);
Console.WriteLine(string.Join("\n", lines));
}
public void WriteToFileCustomer(Customer c1)
{
if (File.Exists(FilenameCustomer))
{
List<string> lines = new List<string>();
if (Customers.Count == 0)
{
c1.ProcessDiscountAlloted();
lines.Add(c1.Id + "\t" + c1.Name + "\t" + c1.Address + "\t" +
c1.DiscountAllotted + "\t" + c1.TotalSpend);
}
else
{
bool IsCustomerExist = false;
for (int i = 0; i < Customers.Count; i++)
{
Customer customer = Customers[i];
if (customer.Id == c1.Id)
{
customer = c1;
IsCustomerExist = true;
}
customer.ProcessDiscountAlloted();
lines.Add(customer.Id + "\t" + customer.Name + "\t" +
customer.Address + "\t" + customer.DiscountAllotted + "\t" +
customer.TotalSpend);
}
if (IsCustomerExist == false)
{
c1.ProcessDiscountAlloted();
lines.Add(c1.Id + "\t" + c1.Name + "\t" + c1.Address + "\t" +
c1.DiscountAllotted + "\t" + c1.TotalSpend);
}
}
File.WriteAllLines(FilenameCustomer, lines);
Console.WriteLine("Successfully updated customers file on location
: " + FilenameCustomer);
}
else
{
File.WriteAllText(FilenameCustomer, "");
}
}
private Inventory IsCodeExist(int code)
{
Inventory inv = null;
for (int i = 0; i < Items.Count; i++)
{
if (Items[i].ItemNumber == code)
{
inv = Items[i];
break;
}
}
return inv;
}
public List<Inventory> ChooseItem()
{
int code = 0;
List<Inventory> OrderedItems = new
List<Inventory>();
do
{
Console.Write("Please enter the code to pick item (0 for submit and
print receipt) : ");
code = (int)AcceptUntilValidInput(Console.ReadLine(),
"code");
if (code == 0)
break;
Inventory inventory;
Inventory item = IsCodeExist(code);
if (item != null)
{
if (item.Quantity > 0)
{
do
{
Console.Write("Enter how many quantity required : ");
int quantity = (int)AcceptUntilValidInput(Console.ReadLine(),
"quantity");
if (quantity == 0)
{
Console.WriteLine("Error :: Enter quantity greater than
zero!");
}
else if (item.Quantity >= quantity)
{
Items.Find(x => x.ItemNumber == item.ItemNumber).Quantity -=
quantity;
inventory = new Inventory()
{
ItemNumber = item.ItemNumber,
Name = item.Name,
Price = item.Price,
Quantity = quantity
};
break;
}
else
{
Console.WriteLine("Error :: Insufficient quantity in
store!");
}
} while (true);
OrderedItems.Add(inventory);
}
else
{
Console.WriteLine("Error :: Insufficient quantity!");
}
}
else
{
Console.WriteLine("Error :: Code doesn't exist!");
}
} while (code != 0);
return OrderedItems;
}
public void ProcessCustomer()
{
Customer customer = new Customer();
Console.Write("Are you a new customer? (y/n) : ");
char option = Console.ReadLine()[0];
if (option == 'n')
{
Console.Write("Enter your customer id : ");
int id = (int)AcceptUntilValidInput(Console.ReadLine(), "custumer
id");
bool IsExist = customer.IsCustomerExist(id);
if (IsExist)
{
Console.WriteLine("Customer Name : " + customer.Name);
Console.WriteLine("Customer Address : " + customer.Address);
Console.WriteLine("Customer Total Spend : " +
ToCurrencyString(customer.TotalSpend));
Console.WriteLine("Customer Discount Alloted : " +
customer.DiscountAllotted);
Manager.ShowStockInformation(Items);
var orderedItems = ChooseItem();
if (orderedItems.Count > 0)
{
Receipt receipt = new Receipt();
receipt.OrderNumber++;
receipt.OrderDate = DateTime.Now;
receipt.Customer = customer;
receipt.Items = orderedItems;
receipt.ProcessCalculation();
WriteToFileReceipt(receipt);
WriteToFileCustomer(receipt.Customer);
WriteToFileInventory();
}
}
else
{
Console.WriteLine("Error :: Customer doesn't exist!");
}
}
else if (option == 'y')
{
Customer cust = customer.EnterNewCustomerDetails();
WriteToFileCustomer(cust);
ProcessCustomer();
}
else
{
Console.WriteLine("Error :: Invalid option!");
}
}
public void ProcessManager()
{
Manager manager = new Manager();
if (manager.IsManagerAuthorised())
{
int choice;
do
{
Console.WriteLine("\nSub Menu :: ");
Console.WriteLine("Press <1> to Display all the products
stock information");
Console.WriteLine("Press <2> to Add more stock for each
product");
Console.WriteLine("Press <3> Goto Main Menu");
Console.Write("Enter your choice : ");
choice = (int)AcceptUntilValidInput(Console.ReadLine(),
"choice");
if (choice == 1)
{
Manager.ShowStockInformation(Items);
}
else if (choice == 2)
{
Items = manager.Restock(Items);
WriteToFileInventory();
}
else if (choice == 3)
{
break;
}
else
{
Console.WriteLine("Error :: Invalid Choice");
}
} while (true);
}
else
{
Console.WriteLine("Error :: Incorrect Password!");
}
}
public string ToCurrencyString(double value)
{
return value.ToString("C",
CultureInfo.CreateSpecificCulture("en-US"));
}
}
}
SCREEN SHOTS :
Get Answers For Free
Most questions answered within 1 hours.