You have been hired by a consultant firm to work on a shop p
You have been hired by a consultant firm to work on a shop program. It will have a very simple interface with 4 options (Shop.java): Setup shop Buy items List of items purchased Checkout The idea is that the user should follow the sequence of setting up shop then letting the customer purchase items and check out. Each of the options should utilize at least one method each (4 methods minimum for the project). Setup Shop (I) 1. Ask for the number of items to sell 2. For each item a. Ask the name of the item (one word) b. Ask the price of the item 3. Discount a. Ask for the threshold (over which amount to give discount) b. Ask for the rate (how much % discount) 4. User can run setup multiple times so keep the latest version Buy Items (II) 1. If setup is not done yet then tell customer to setup shop first 2. For each item a. Ask the amount they wish to purchase 3. User can purchase multiple times so only process the latest order (think of init() from labs) List of Items (III) 1. If setup is not done yet then tell customer to setup shop first 2. If setup is done but the customer hasnt bought anything then ask to buy first 3. For each item purchased (non-zero amount) a. Display amount purchase and price per item Check Out (IV) 1. If setup is not done yet then tell customer to setup shop first 2. If setup is done but the customer hasnt bought anything then ask to buy first 3. Display the summary a. Sub Total b. Discount c. Total 4. End the program Your job is to fool-proof that program so if the customer does not follow the ascribed order then there should be guidance on what to do first. Here is a sample of this behavior: This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:2 Shop is not setup yet! This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:4 Shop is not setup yet! This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want: The program will keep asking the 4 main questions until the user decides to setup Shop. Here is a sample run with one misstep by the customer: This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:1 Please enter the number of items: 2 Enter name of product 0 :Shampoo Enter price of product 0 :13.13 Enter name of product 1 :Conditioner Enter price of product 1 :10.99 Please enter the amount to qualify for discount: 100 Please enter the discount rate(0.1 for 10%): .05 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:3 Try again: You have not bought anything This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:2 Enter the amount of Shampoo : 10 Enter the amount of Conditioner : 20 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:3 10 count of Shampoo @ 13.13 = $131.3 20 count of Conditioner @ 10.99 = $219.8 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout The program will keep asking the 4 main questions until the user decides to setup Shop. Here is a sample run with one misstep by the customer: This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:1 Please enter the number of items: 2 Enter name of product 0 :Shampoo Enter price of product 0 :13.13 Enter name of product 1 :Conditioner Enter price of product 1 :10.99 Please enter the amount to qualify for discount: 100 Please enter the discount rate(0.1 for 10%): .05 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:3 Try again: You have not bought anything This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:2 Enter the amount of Shampoo : 10 Enter the amount of Conditioner : 20 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:3 10 count of Shampoo @ 13.13 = $131.3 20 count of Conditioner @ 10.99 = $219.8 This program supports 4 functions: 1. Setup Shop 2. Buy 3. List Items 4. Checkout Please choose the function you want:4 Thanks for Sub Total: -Discount: Total : coming! $351.1 $17.555000000000003 $333.545
Solution
using System; using System.Collections.Generic; using System.Linq; using System.Web; using WingtipToys.Models; namespace WingtipToys.Logic { public class ShoppingCartActions : IDisposable { public string ShoppingCartId { get; set; } private ProductContext _db = new ProductContext(); public const string CartSessionKey = \"CartId\"; public void AddToCart(int id) { // Retrieve the product from the database. ShoppingCartId = GetCartId(); var cartItem = _db.ShoppingCartItems.SingleOrDefault( c => c.CartId == ShoppingCartId && c.ProductId == id); if (cartItem == null) { // Create a new cart item if no cart item exists. cartItem = new CartItem { ItemId = Guid.NewGuid().ToString(), ProductId = id, CartId = ShoppingCartId, Product = _db.Products.SingleOrDefault( p => p.ProductID == id), Quantity = 1, DateCreated = DateTime.Now }; _db.ShoppingCartItems.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity. cartItem.Quantity++; } _db.SaveChanges(); } public void Dispose() { if (_db != null) { _db.Dispose(); _db = null; } } public string GetCartId() { if (HttpContext.Current.Session[CartSessionKey] == null) { if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name)) { HttpContext.Current.Session[CartSessionKey] = HttpContext.Current.User.Identity.Name; } else { // Generate a new random GUID using System.Guid class. Guid tempCartId = Guid.NewGuid(); HttpContext.Current.Session[CartSessionKey] = tempCartId.ToString(); } } return HttpContext.Current.Session[CartSessionKey].ToString(); } public List