using System; using System.Collections.Generic; using System.Text; using Business.Business.Sales; using Models.Models; namespace Display.Views { public class ManagerView : BaseView { StockController stockctrl; DealController dealctrl; /// /// Constructor that accepts a user object. /// /// /// User object is used for stock and deal checking. /// Initialises stock and deal controllers. /// public ManagerView(User currentUser):base(currentUser) { stockctrl = new StockController(currentUser); dealctrl = new DealController(currentUser); } /// /// Shows all available commands. /// /// /// Inherits all available commands from the base view. /// The main menu. /// public override void ShowAvaliableCommands() { base.ShowAvaliableCommands(); Console.WriteLine("2. Stock"); } /// /// Asks the user to choose which group of action to use. /// /// /// If user inputs the digit 1, returns selling handles. /// If user inputs the digit 2, returns managing handles. /// If user inputs something else, the operation is invalid. /// public override void ActionHandle() { try { while (true) { ShowAvaliableCommands(); Console.Write("> "); int input = int.Parse(Console.ReadLine()); if (input == 0) { Environment.Exit(0); } else if (input == 1) { SaleHandle(); } else if (input == 2) { ManageHandle(); } else Console.WriteLine("Invalid operation"); } } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Selection menu with manager actions. /// /// /// Requires role level 2 (Manager). /// public void ManageHandle() { bool running = true; while (running) { Console.WriteLine(); Console.WriteLine("=== STOCK ==="); Console.WriteLine("0. Back"); Console.WriteLine(); Console.WriteLine("Stock Managment"); Console.WriteLine("1. List all stocks"); Console.WriteLine("2. Get stock by ID"); Console.WriteLine("3. List stocks by time"); Console.WriteLine("4. Add stock"); Console.WriteLine("5. Delete"); Console.WriteLine("6. List deals by user"); Console.Write("> "); try { int choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: GetAll(); break; case 2: Get(); break; case 3: GetByTime(); break; case 4: Add(); break; case 5: Delete(); break; case 6: GetByUser(); break; case 0: running = false; break; default: Console.WriteLine("Invalid Option!"); break; } } catch (Exception e) { Console.WriteLine(e.Message); } } } /// /// Lists all information about stock from the database. /// public void GetAll() { try { Console.WriteLine("Getting all stock..."); Console.WriteLine("ID - Product ID - Amount - Time"); foreach (var item in stockctrl.GetAll()) { Console.WriteLine($"{item.Id} - {item.ProductId} - {item.Amount} - {item.Time}"); } } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Lists all registered information about stocks from the database. /// public void Get() { try { Console.Write("Enter stock id: "); int id = int.Parse(Console.ReadLine()); var result = stockctrl.Get(id); Console.WriteLine("ID - Product ID - Amount - Time"); Console.WriteLine($"{result.Id} - {result.ProductId} - {result.Amount} - {result.Time}"); } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Gets stock by its start time and end time. /// /// /// Inputs start time and end time. /// Lists all information about stocks from the database in real time. /// public void GetByTime() { try { Console.WriteLine("Getting stock by time..."); Console.Write("Enter start time: "); DateTime startTime = DateTime.Parse(Console.ReadLine()); Console.Write("Enter end time: "); DateTime endTime = DateTime.Parse(Console.ReadLine()); Console.WriteLine("ID - Product ID - Amount - Time"); foreach (var item in stockctrl.GetByTime(startTime, endTime)) { Console.WriteLine($"{item.Id} - {item.ProductId} - {item.Amount} - {item.Time}"); } } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Adding a stock using the product id or name. /// /// /// Entering product name and amount. /// If the result is true, returns a stock with product id, amount and a real time. /// Else returns a stock with product name, amount and a real time. /// public void Add() { try { Console.WriteLine("Adding stock by product id or name..."); Console.Write("Enter product ID or name: "); string product = Console.ReadLine(); Console.Write("Enter stock amount: "); double amount = double.Parse(Console.ReadLine()); bool result = int.TryParse(product, out int productId); if (result) { stockctrl.Add(productId, amount, DateTime.Now); } else { stockctrl.Add(product, amount, DateTime.Now); } } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Deletes a stock from the database. /// public void Delete() { try { Console.WriteLine("Deleting stock..."); Console.Write("Enter stock id: "); int id = int.Parse(Console.ReadLine()); stockctrl.Delete(id); } catch (Exception e) { Console.WriteLine(e.Message); } } /// /// Gets deals by the user who made them. /// /// /// Inputs username or user ID. /// Lists all deals made by a user from the database. /// public void GetByUser() { try { Console.WriteLine("Getting stock by time..."); Console.Write("Enter username or user ID: "); string input = Console.ReadLine(); int.TryParse(input, out int inputId); ICollection output; if (inputId != 0) { output = dealctrl.GetByUser(inputId); } else { output = dealctrl.GetByUser(input); } Console.WriteLine("Deal ID - Product ID - Amount - Time"); foreach (var item in output) { Console.WriteLine($"{item.Id} - {item.ProductId} - {item.Amount} - {item.Time}"); } } catch (Exception e) { Console.WriteLine(e.Message); } } } }