public void AddArticle(string articleName, string articlePrice, string articleQuantity) { int i; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // Check if article name already exist and inform user and then interrupt if (inventoryTable[i, 0] == articleName) { message.Write("Article already exist!"); break; } if (String.IsNullOrEmpty(inventoryTable[i, 0])) { // Next available empty row is found inventoryTable[i, 0] = articleName; inventoryTable[i, 1] = articlePrice; inventoryTable[i, 2] = articleQuantity; message.Write("Article is now added!"); break; } } return; }
public void DeleteArticle(string articleName) { int i, j, deleteRow; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // End loop if no more articles if (String.IsNullOrEmpty(inventoryTable[i, 0])) { break; } if (inventoryTable[i, 0] == articleName) { // Article is found and will be deleted deleteRow = i; for (j = deleteRow + 1; j < maxInventoryRow; j++) { inventoryTable[j - 1, 0] = inventoryTable[j, 0]; inventoryTable[j - 1, 1] = inventoryTable[j, 1]; inventoryTable[j - 1, 2] = inventoryTable[j, 2]; // Interrupt when there are no more articles in the table if (String.IsNullOrEmpty(inventoryTable[j, 0])) { break; } } message.Write("Article is now deleted!"); break; } } return; }
public void AddUser(string userName, string password) { int i; var message = new MessageToUser(); var bank = new Bank(); for (i = 0; i < maxUserRow; i++) { // Check if username already exist and inform user and then interrupt if (userTable[i, 0] == userName) { message.Write("User already exist! Please use another Username!"); break; } if (String.IsNullOrEmpty(userTable[i, 0])) { // Next available empty row is found userTable[i, 0] = userName; userTable[i, 1] = password; message.Write("User is now added!"); // Also add account with balance zero bank.AddAccount(userName); break; } } return; }
public void PrintArticles() { int i; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // End loop if no more articles if (String.IsNullOrEmpty(inventoryTable[i, 0])) { // If there was something to print, then wait for user input before break if (i > 0) { message.Write("End of list!"); } break; } // Print the article info if (i == 0) { // Write header before printing first article Console.WriteLine("{0,-30} {1,-10} {2,-10}\n", "Article", "Price", "Quantity"); } Console.WriteLine("{0,-30} {1,-10} {2,-10}", inventoryTable[i, 0], inventoryTable[i, 1], inventoryTable[i, 2]); } return; }
public void AddAccount(string userName) { int i; var message = new MessageToUser(); for (i = 0; i < maxBankRow; i++) { // Check if username already exist and interrupt in such case if (bankTable[i, 0] == userName) { message.Write("No account created! The user has already an account!"); break; } // If user not exist, then add user and the amount in the end of the table if (String.IsNullOrEmpty(bankTable[i, 0])) { // Next available empty row is found bankTable[i, 0] = userName; bankTable[i, 1] = "0"; message.Write("New account is now added!\n"); break; } } return; }
public int AddAmount(string userName, int amount) { int i, balance; bool userExist = false; var message = new MessageToUser(); for (i = 0; i < maxBankRow; i++) { // Check if username exist and then add amount to existing balance if (bankTable[i, 0] == userName) { balance = int.Parse(bankTable[i, 1]); // If balance will be negative , no withdrawel is possible if ((balance + amount) <= 0) { message.Write("There is not enough balance on your account! Balance = " + bankTable[i, 1] + "\n"); return(0); } balance += amount; bankTable[i, 1] = balance.ToString(); message.Write("New balance of the account is: " + bankTable[i, 1] + "\n"); userExist = true; break; } } // If user is not found, then notify user if (userExist == false) { message.Write("No account exist with that Username!\n"); amount = 0; } return(Math.Abs(amount)); }
public void PrintAccounts() { int i; var message = new MessageToUser(); for (i = 0; i < maxBankRow; i++) { // End loop if no more articles if (String.IsNullOrEmpty(bankTable[i, 0])) { // If there was something to print, then wait for user input before break if (i > 0) { message.Write("End of list!"); } break; } // Print the account info if (i == 0) { // Write header before printing first account Console.WriteLine("{0,-30} {1,-10}\n", "User", "Balance"); } Console.WriteLine("{0,-30} {1,-10}", bankTable[i, 0], bankTable[i, 1]); } return; }
public bool ValidateUser(string userName, string password) { int i; var message = new MessageToUser(); for (i = 0; i < maxUserRow; i++) { // End loop if no more users if (String.IsNullOrEmpty(userTable[i, 0])) { message.Write("User not found!"); return(false); } if (userTable[i, 0] == userName) { // User is found, check if password is OK if (userTable[i, 1] == password) { message.Write("Your are now logged in!"); return(true); } else { message.Write("Password not correct!"); return(false); } } } return(false); }
public void DeleteUser(string userName) { int i, j, deleteRow; var message = new MessageToUser(); for (i = 0; i < maxUserRow; i++) { // End loop if now more users if (String.IsNullOrEmpty(userTable[i, 0])) { break; } if (userTable[i, 0] == userName) { // User is found and will be deleted deleteRow = i; for (j = deleteRow + 1; j < maxUserRow; j++) { userTable[j - 1, 0] = userTable[j, 0]; userTable[j - 1, 1] = userTable[j, 1]; // Interrupt when there are no more users in the table if (String.IsNullOrEmpty(userTable[j, 0])) { break; } } message.Write("User is now deleted!"); break; } } return; }
public void PrintShopCart() { int i; var message = new MessageToUser(); for (i = 0; i < maxShopRow; i++) { // End loop if no more articles if (String.IsNullOrEmpty(shopCart[i, 0])) { // If there was something to print, then wait for user input before break if (i > 0) { message.Write("End of list!"); } // If there are no items in shopCart, inform user else if (i == 0) { message.Write("There are no items in your shopping cart!"); } break; } // Print the shopcart info if (i == 0) { // Write header before printing first shopcart line Console.WriteLine("{0,-30} {1,-15} {2,-10}\n", "Article name", "No of items", "Total price"); } Console.WriteLine("{0,-30} {1,-15} {2,-10}", shopCart[i, 1], shopCart[i, 2], shopCart[i, 3]); } return; }
public void PrintUsers() { int i; var message = new MessageToUser(); for (i = 0; i < maxUserRow; i++) { // End loop if no more users if (String.IsNullOrEmpty(userTable[i, 0])) { // If there was something to print, then wait for user input before break if (i > 0) { message.Write("End of list!"); } break; } // Print the user info if (i == 0) { // Write header before printing first user Console.WriteLine("{0,-20} {1,-20}\n", "Username", "Password"); } Console.WriteLine("{0,-20} {1,-20}", userTable[i, 0], userTable[i, 1]); } return; }
private static void InventoryHandling(string currentUserName) { var choice = ""; var inventory = new Inventory(); var message = new MessageToUser(); string articleName, articlePrice, articleQuantity; while (choice == "") { ClearConsole(); Console.WriteLine("Inventory handling menu ({0})\n", currentUserName); Console.WriteLine("A - Add an article"); Console.WriteLine("B - Delete an article"); Console.WriteLine("C - Print all articles"); Console.WriteLine("D - Back to Main menu"); Console.WriteLine("\nWhat do you want to do?"); Console.Write("Select A, B, C or D: "); choice = Console.ReadLine(); choice = choice.ToUpper(); switch (choice) { case "A": // Ask for name, price and quantity of an article to be added Console.Write("Type name of article: ? "); articleName = Console.ReadLine(); Console.Write("Type price of article: ? "); articlePrice = Console.ReadLine(); Console.Write("Type quantity of article: ? "); articleQuantity = Console.ReadLine(); // Add user to table inventory.AddArticle(articleName, articlePrice, articleQuantity); break; case "B": // Ask for article to delete Console.Write("Type name of article you want to delete: ? "); articleName = Console.ReadLine(); // Delete user from table inventory.DeleteArticle(articleName); break; case "C": inventory.PrintArticles(); break; case "D": // Back to main menu return; default: message.Write("Wrong option selected - Please try again!"); break; } choice = ""; } }
private static void UserHandling(string currentUserName) { var choice = ""; var user = new User(); var message = new MessageToUser(); string userName, password; while (choice == "") { ClearConsole(); Console.WriteLine("User handling menu ({0})\n", currentUserName); Console.WriteLine("A - Add a user"); Console.WriteLine("B - Delete a user"); Console.WriteLine("C - Print all users"); Console.WriteLine("D - Back to Main menu"); Console.WriteLine("\nWhat do you want to do?"); Console.Write("Select A, B, C or D: "); choice = Console.ReadLine(); choice = choice.ToUpper(); switch (choice) { case "A": // Ask for username and password to add Console.Write("Type Username you want to add: ? "); userName = Console.ReadLine(); Console.Write("Type Password: ? "); password = Console.ReadLine(); // Add user to table user.AddUser(userName, password); break; case "B": // Ask for username to delete Console.Write("Type the Username you want to delete: ? "); userName = Console.ReadLine(); // Delete user from table user.DeleteUser(userName); break; case "C": user.PrintUsers(); break; case "D": // Back to main menu return; default: message.Write("Wrong option selected - Please try again!"); break; } choice = ""; } }
public bool ArticleExist(string articleName) { int i; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // Check if article exist if (inventoryTable[i, 0] == articleName) { return(true); } } // Article does not exist and then return false return(false); }
public int QuantityInStock(string articleName) { int i; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // Check if article exist and if there is enough items in stock if (inventoryTable[i, 0] == articleName) { return(int.Parse(inventoryTable[i, 2])); } } // Article does not exist; Notify user and return 0 message.Write("Article does not exist!"); return(0); }
public bool ChangeQuantity(string articleName, int noItems) { int i, currentQuantity, newQuantity; var message = new MessageToUser(); for (i = 0; i < maxInventoryRow; i++) { // Check if article exist and adjust quantity if found if (inventoryTable[i, 0] == articleName) { currentQuantity = int.Parse(inventoryTable[i, 2]); newQuantity = currentQuantity + noItems; inventoryTable[i, 2] = newQuantity.ToString(); return(true); } } return(false); }
public void DeleteAccount(string userName) { int i, j, balance, deleteRow; var message = new MessageToUser(); for (i = 0; i < maxBankRow; i++) { // End loop if now more accounts if (String.IsNullOrEmpty(bankTable[i, 0])) { break; } if (bankTable[i, 0] == userName) { // Account is found and will be deleted if balance is zero balance = int.Parse(bankTable[i, 1]); if (balance != 0) { // Balance is not zero and account will not be deleted message.Write("Account not deleted since balance is not zero!"); break; } // deleteRow = i; for (j = deleteRow + 1; j < maxBankRow; j++) { bankTable[j - 1, 0] = bankTable[j, 0]; bankTable[j - 1, 1] = bankTable[j, 1]; // Interrupt when there are no more accounts in the table if (String.IsNullOrEmpty(bankTable[j, 0])) { break; } } message.Write("Account is now deleted!"); break; } } return; }
private static void BankHandling(string currentUserName) { var choice = ""; var bank = new Bank(); var user = new User(); var message = new MessageToUser(); string addUserName, amount; while (choice == "") { ClearConsole(); Console.WriteLine("Bank handling menu ({0})\n", currentUserName); Console.WriteLine("A - Add an account"); Console.WriteLine("B - Add money to account"); Console.WriteLine("C - Withdraw money from account"); Console.WriteLine("D - Print all user accounts"); Console.WriteLine("E - Back to Main menu"); Console.WriteLine("\nWhat do you want to do?"); Console.Write("Select A, B, C, D or E: "); choice = Console.ReadLine(); choice = choice.ToUpper(); switch (choice) { case "A": // Ask for username Console.Write("Type Username to add account for: ? "); addUserName = Console.ReadLine(); // Add an account for this user bank.AddAccount(addUserName); break; case "B": Console.WriteLine("Type the amount to add: ?"); amount = Console.ReadLine(); // If nothing was entered the inform user else add amount if (String.IsNullOrEmpty(amount)) { message.Write("No amount was entered!"); } else { // Add the amount to the account for this user bank.AddAmount(currentUserName, int.Parse(amount)); } break; case "C": Console.Write("Type the amount you want to withdraw: ? "); amount = Console.ReadLine(); // If nothing was entered the inform user else withdraw amount if (String.IsNullOrEmpty(amount)) { message.Write("No amount was entered!"); } else { // Withdraw the amount from the account for this user // And set the shopAmount that the user can shop for shopAmount = bank.AddAmount(currentUserName, -(int.Parse(amount))); } break; case "D": bank.PrintAccounts(); break; case "E": // Back to main menu return; default: message.Write("Wrong option selected - Please try again!"); break; } choice = ""; } }
static void Main(string[] args) { var choice = ""; string userName = "", password = ""; var message = new MessageToUser(); var inventory = new Inventory(); var user = new User(); var bank = new Bank(); // Create list of a) Users and b) Articles and c) Bank accounts for users user.CreateUsers(); inventory.CreateArticles(); bank.CreateAccounts(); //Login LoginUser(ref userName, ref password); // Write main menu and wait for input while (choice == "") { ClearConsole(); Console.WriteLine("Welcome to Rikard's Vending Machine ({0})\n", userName); Console.WriteLine("Main menu\n"); Console.WriteLine("A - Shop"); Console.WriteLine("B - Bank"); Console.WriteLine("C - Inventory"); Console.WriteLine("D - User handling"); Console.WriteLine("E - Change user"); Console.WriteLine("F - Exit"); Console.WriteLine("\nWhat do you want to do?"); Console.Write("Select A, B, C, D or E: "); choice = Console.ReadLine(); choice = choice.ToUpper(); switch (choice) { case "A": Shopping(userName); break; case "B": BankHandling(userName); break; case "C": if (user.IsUserAdmin(userName) == true) { InventoryHandling(userName); } else { message.Write("You do not have access to this function."); } break; case "D": if (user.IsUserAdmin(userName) == true) { UserHandling(userName); } else { message.Write("You do not have access to this function."); } break; case "E": LoginUser(ref userName, ref password); break; case "F": ClearConsole(); message.Write("Thanks for shopping at Rikard's Vending Machine!\nSee you soon and Welcome back!"); Environment.Exit(0); break; default: message.Write("Wrong option selected - Please try again!"); break; } choice = ""; } }
private static void Shopping(string currentUserName) { var choice = ""; string articleName, inputNoItems; int noItems, buyAmount; var shop = new Shop(); var inventory = new Inventory(); var message = new MessageToUser(); while (choice == "") { ClearConsole(); Console.WriteLine("Shopping menu ({0})\n", currentUserName); Console.WriteLine("A - Show list of articles I can buy"); Console.WriteLine("B - Buy article"); Console.WriteLine("C - Show my Shopping cart"); Console.WriteLine("D - Back to Main menu"); Console.WriteLine("\nWhat do you want to do?"); Console.Write("Select A, B, C or D: "); choice = Console.ReadLine(); choice = choice.ToUpper(); switch (choice) { case "A": inventory.PrintArticles(); break; case "B": Console.Write("What article do you want to buy: ?"); articleName = Console.ReadLine(); // Check first if article exist if (inventory.ArticleExist(articleName) == false) { message.Write("There is no article with that name!"); break; } Console.Write("How many items do you want to buy: ?"); inputNoItems = Console.ReadLine(); // If nothing was entered in noItems, inform user if (String.IsNullOrEmpty(inputNoItems)) { message.Write("No value was entered!"); } // If no money, inform user else if (shopAmount == 0) { message.Write("You have no money! Please withdraw from bank!"); } else { // Try to execute the shopping request noItems = int.Parse(inputNoItems); buyAmount = shop.BuyArticle(currentUserName, articleName, noItems, shopAmount); // If shopping is successful then reduce shopAmount with the "buy amount" if (buyAmount != 0) { shopAmount = shopAmount - buyAmount; } } break; case "C": shop.PrintShopCart(); message.Write("Available amount for shopping = " + shopAmount); break; case "D": // Back to main menu return; default: message.Write("Wrong option selected - Please try again!"); break; } choice = ""; } }
// BuyArticle performs a buy if conditions are OK // The functions returns the "buy amount" if OK, otherwise returns 0; public int BuyArticle(string userName, string articleName, int noItems, int shopAmount) { int i, currentStock, articlePrice, currentNoItems, currentTotalPrice, newNoItems, newTotalPrice; bool buyOK = false; var message = new MessageToUser(); var inventory = new Inventory(); // Check first if article exist if (inventory.ArticleExist(articleName) == false) { message.Write("There is no article with that name!"); return(0); } // Check if no of articles are available in inventory (stock); If not - notify user and return currentStock = inventory.QuantityInStock(articleName); if (currentStock <= noItems) { message.Write("There are not enough articles in Stock!"); return(0); } // Check if user has enough money to buy; If not - notify user and return articlePrice = inventory.ArticlePrice(articleName); if (shopAmount < articlePrice * noItems) { message.Write("You have not enough money to buy this quantity!"); return(0); } // Add article to shopCart for (i = 0; i < maxShopRow; i++) { // Check if article already exist and then add total quantity and price if (shopCart[i, 0] == userName && shopCart[i, 1] == articleName) { currentNoItems = int.Parse(shopCart[i, 2]); currentTotalPrice = int.Parse(shopCart[i, 3]); newNoItems = currentNoItems + noItems; newTotalPrice = currentTotalPrice + noItems * articlePrice; shopCart[i, 2] = newNoItems.ToString(); shopCart[i, 3] = newTotalPrice.ToString(); buyOK = true; break; } if (String.IsNullOrEmpty(shopCart[i, 0])) { // Next available empty row is found shopCart[i, 0] = userName; shopCart[i, 1] = articleName; shopCart[i, 2] = noItems.ToString(); newTotalPrice = noItems * articlePrice; shopCart[i, 3] = newTotalPrice.ToString(); buyOK = true; break; } } // Reduce quantity in inventory if successful buy if (buyOK == true) { inventory.ChangeQuantity(articleName, -noItems); } return(noItems * articlePrice); }