public double AddPurchaseTransaction(int productId) { _state = eTransactionState.Purchase; ProductItem product = _db.GetProductItem(productId); if (product.Price > _runningTotal) { throw new Exception("More money is required."); } TransactionItem transactionItem = new TransactionItem(); transactionItem.ProductId = productId; transactionItem.SalePrice = product.Price; _transactionItems.Add(transactionItem); VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.PurchaseItem; operation.Price = product.Price; operation.RunningTotal = _runningTotal; operation.ProductName = product.Name; _log.LogOperation(operation); _runningTotal -= product.Price; return(_runningTotal); }
public void LogOperation(VendingOperation operation) { string filePath = Path.Combine(_folderPath, _fileName); if (!Directory.Exists(_folderPath)) { Directory.CreateDirectory(_folderPath); } System.IO.File.AppendAllText(filePath, operation.ToString()); }
public void LogOperation(VendingOperation operation) { LogItem item = new LogItem(); item.CreationDate = operation.TimeStamp; item.Amount = operation.Price; item.OperationId = (int)operation.OperationType; item.ProductId = operation.ProductId; item.UserId = operation.UserId; AddLogItem(item); }
public IList <VendingOperation> GetLogData(DateTime?startDate, DateTime?endDate) { string filePath = Path.Combine(_folderPath, _fileName); List <VendingOperation> result = new List <VendingOperation>(); //Open a StreamReader with the using statement using (StreamReader sr = new StreamReader(filePath)) { while (!sr.EndOfStream) { // Read in the line string line = sr.ReadLine(); string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); DateTime date = DateTime.Parse($"{parts[0]} {parts[1]} {parts[2]}"); VendingOperation op = new VendingOperation(); op.TimeStamp = date; if (parts[3] == "FEED") { op.OperationType = VendingOperation.eOperationType.FeedMoney; } else if (parts[3] == "GIVE") { op.OperationType = VendingOperation.eOperationType.GiveChange; } else { op.OperationType = VendingOperation.eOperationType.PurchaseItem; for (int i = 3; i < parts.Length - 2; i++) { if (i != 3) { op.ProductName += " "; } op.ProductName += parts[i]; } } op.RunningTotal = double.Parse(parts[parts.Length - 1].Substring(1)); op.Price = double.Parse(parts[parts.Length - 2].Substring(1)); if (op.TimeStamp >= startDate && op.TimeStamp <= endDate) { result.Add(op); } } } return(result); }
/// <summary> /// Writes an entry into the log and updates the running total. /// </summary> /// <param name="amountAdded">The amount of money to add to the running total.</param> /// <param name="userId">The id of the user purchasing the product</param> /// <returns>The updated running total after the money has been added.</returns> public double AddFeedMoneyOperation(double amountAdded, int userId) { VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.FeedMoney; operation.Price = amountAdded; operation.RunningTotal = RunningTotal; operation.UserId = userId; _log.LogOperation(operation); RunningTotal += amountAdded; return(RunningTotal); }
public double AddFeedMoneyOperation(double amountAdded) { _state = eTransactionState.FeedMoney; VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.FeedMoney; operation.Price = amountAdded; operation.RunningTotal = _runningTotal; _log.LogOperation(operation); _runningTotal += amountAdded; return(_runningTotal); }
public void Post([FromBody] LogAddViewModel logOp) { VendingOperation vendOp = new VendingOperation(); vendOp.OperationType = (VendingOperation.eOperationType)logOp.OperationType; vendOp.Price = logOp.Price; vendOp.TimeStamp = logOp.TimeStamp; vendOp.UserId = logOp.UserId; if (logOp.OperationType == LogAddViewModel.eOperationType.PurchaseItem) { vendOp.ProductId = logOp.ProductId; } _loggingDAO.LogOperation(vendOp); }
public Change AddGiveChangeOperation() { _state = eTransactionState.Change; double result = _runningTotal; VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.GiveChange; operation.RunningTotal = _runningTotal; _log.LogOperation(operation); _change = GetChange(); _runningTotal = 0.0; return(_change); }
/// <summary> /// Writes an entry into the log and determines the change needed. Resets the running /// total to zero. /// </summary> /// <param name="userId">The id of the user purchasing the product</param> /// <returns>The change that should be given to the user</returns> public Change AddGiveChangeOperation(int userId) { var result = GetChange(); VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.GiveChange; operation.RunningTotal = RunningTotal; operation.UserId = userId; _log.LogOperation(operation); RunningTotal = 0.0; _vendingTransactionId = BaseItem.InvalidId; return(result); }
/// <summary> /// Handles all purchase transactions. Writes a transaction to the database and an entry /// into the log file. This also updates the running total property for the vending machine. /// </summary> /// <param name="productId">The id of the product being purchased</param> /// <param name="userId">The id of the user purchasing the product</param> /// <returns>The updated running total after the purchase has been handled.</returns> public double AddPurchaseTransaction(int productId, int userId) { ProductItem product = _db.GetProductItem(productId); if (product.Price > RunningTotal) { throw new Exception("More money is required."); } TransactionItem transactionItem = new TransactionItem(); transactionItem.ProductId = productId; transactionItem.SalePrice = product.Price; if (_vendingTransactionId == BaseItem.InvalidId) { VendingTransaction vendTrans = new VendingTransaction(); vendTrans.Date = DateTime.UtcNow; vendTrans.UserId = userId; List <TransactionItem> transactionItems = new List <TransactionItem>(); transactionItems.Add(transactionItem); _vendingTransactionId = _db.AddTransactionSet(vendTrans, transactionItems); } else { transactionItem.VendingTransactionId = _vendingTransactionId; _db.AddTransactionItem(transactionItem); } VendingOperation operation = new VendingOperation(); operation.OperationType = VendingOperation.eOperationType.PurchaseItem; operation.Price = product.Price; operation.RunningTotal = RunningTotal; operation.ProductName = product.Name; operation.ProductId = product.Id; operation.UserId = userId; _log.LogOperation(operation); RunningTotal -= product.Price; return(RunningTotal); }
public IList <VendingOperation> GetLogData(DateTime?startDate, DateTime?endDate) { List <VendingOperation> result = new List <VendingOperation>(); var items = GetLogItems(startDate, endDate); foreach (var item in items) { var vendOp = new VendingOperation(); vendOp.OperationType = (VendingOperation.eOperationType)item.OperationId; vendOp.Price = item.Amount; vendOp.ProductName = item.ProductName; vendOp.TimeStamp = item.CreationDate; vendOp.ProductId = item.ProductId; vendOp.UserId = item.UserId; result.Add(vendOp); } return(result); }
/// <summary> /// Creates each type of vending operation and a purchase operation for each product in the system /// </summary> /// <param name="db">Database interface used to create the data</param> /// <param name="log">Log interface used to create the file data</param> public static void PopulateLogFileWithOperations(IVendingService db, ILogService log) { OperationTypeItem type = new OperationTypeItem(); type.Id = (int)VendingOperation.eOperationType.Invalid; type.Name = VendingOperation.eOperationType.Invalid.ToString(); log.AddOperationTypeItem(type); type.Id = (int)VendingOperation.eOperationType.FeedMoney; type.Name = VendingOperation.eOperationType.FeedMoney.ToString(); log.AddOperationTypeItem(type); type.Id = (int)VendingOperation.eOperationType.GiveChange; type.Name = VendingOperation.eOperationType.GiveChange.ToString(); log.AddOperationTypeItem(type); type.Id = (int)VendingOperation.eOperationType.PurchaseItem; type.Name = VendingOperation.eOperationType.PurchaseItem.ToString(); log.AddOperationTypeItem(type); var users = db.GetUserItems(); var products = db.GetProductItems(); double runningTotal = 0.0; foreach (var user in users) { VendingOperation op = new VendingOperation(); op.OperationType = VendingOperation.eOperationType.FeedMoney; op.Price = 20.0; op.RunningTotal = runningTotal += op.Price; op.TimeStamp = DateTime.UtcNow; op.UserId = user.Id; log.LogOperation(op); op.OperationType = VendingOperation.eOperationType.FeedMoney; op.Price = 10.0; op.RunningTotal = runningTotal += op.Price; op.TimeStamp = DateTime.UtcNow; op.UserId = user.Id; log.LogOperation(op); foreach (var product in products) { op = new VendingOperation(); op.OperationType = VendingOperation.eOperationType.PurchaseItem; op.Price = product.Price; op.RunningTotal = runningTotal - op.Price; op.TimeStamp = DateTime.UtcNow; op.UserId = user.Id; op.ProductId = product.Id; log.LogOperation(op); } op.OperationType = VendingOperation.eOperationType.GiveChange; op.Price = runningTotal; runningTotal = 0.0; op.RunningTotal = runningTotal; op.TimeStamp = DateTime.UtcNow; op.UserId = user.Id; op.ProductId = BaseItem.InvalidId; log.LogOperation(op); } }
public void LogOperation(VendingOperation operation) { _operations.Add(operation.Clone()); }