public static bool IsPinValid(ref bool cardHasBeenEntered, ref CardModel chosenCard) { string input; int InsertedPin; byte[] intBytes; bool correctPassword; while (true) { correctPassword = true; Console.WriteLine("Insert the pin number of the selected card"); input = Console.ReadLine(); if (input.ToLower().Contains("cancel")) { cardHasBeenEntered = false; break; } if (!int.TryParse(input, out InsertedPin)) { Console.WriteLine("Inserted value not accepted, please try again\n"); continue; } intBytes = BitConverter.GetBytes(InsertedPin); string savedPasswordHash = chosenCard.PasswordHash; byte[] hashBytes = Convert.FromBase64String(savedPasswordHash); byte[] salt = new byte[16]; Array.Copy(hashBytes, 0, salt, 0, 16); var pbkdf2 = new Rfc2898DeriveBytes(intBytes, salt, 100000); byte[] hash = pbkdf2.GetBytes(20); for (int i = 0; i < 20; i++) { if (hashBytes[i + 16] != hash[i]) { correctPassword = false; chosenCard.NumberOfFailedAttempts += 1; Console.WriteLine($"Inserted pin is incorrect, amount of tries remaining {3 - chosenCard.NumberOfFailedAttempts}\n"); if (chosenCard.NumberOfFailedAttempts == 3) { chosenCard.IsCardLocked = true; Console.WriteLine("Card has been locked since there have been 3 failed attempts\n"); } break; } } if (chosenCard.IsCardLocked || correctPassword) { break; } } if (chosenCard.IsCardLocked || !cardHasBeenEntered) { return(false); } else { return(true); } }
static void SelectOptions(ref bool cardHasBeenEntered, IDictionary <int, string> ATMoptions, ref CardModel chosenCard, ref WalletModel wallet) { string input; while (cardHasBeenEntered) { //Tell the user to chose from a list of options Console.WriteLine("Choose from a list of options...."); //loop through the options declared on line 21, and print foreach (KeyValuePair <int, string> option in ATMoptions) { Console.WriteLine($"{option.Key}: - {option.Value}"); } input = Console.ReadLine(); if (input.ToLower().Contains("cancel")) { cardHasBeenEntered = false; break; } bool validChosenAction = int.TryParse(input, out int chosenAction); if (!validChosenAction) { continue; } // If the chosen value is value go into the specifics of the options ChoosenActionSwitch(chosenAction, ref cardHasBeenEntered, ref chosenCard, ref wallet); } }
static void Main(string[] args) { //Get our data, Creating the cards. List <CardModel> cards = new List <CardModel>() { new CardModel(1, 12345, 1111, 250.00, "Lloyds"), new CardModel(2, 55555, 2222, 100.50, "HSBC"), new CardModel(3, 99999, 3333, 1000, "Bank Of England"), }; //Create the list of options used on line 55 IDictionary <int, string> ATMoptions = new Dictionary <int, string> { { 1, "Cancel" }, { 2, "View Balance" }, { 3, "Withdraw Wibbly Dollars" }, }; //This ensures the app is always running while (true) { bool cardHasBeenEntered = false; Console.WriteLine("Free Cash Withdrawal, Please Enter a Card..."); //This is a discard, once the try prase returns true it will discard the returned value _ = int.TryParse(Console.ReadLine(), out int enteredCardId); //Find the card with the entered cardId from the above ReadLine() CardModel chosenCard = cards.Find(c => c.CardId == enteredCardId); //If card is not equal to null, meaning it has found the card entered, go to the next step if (chosenCard != null) { //set the card entered to true and go to next step cardHasBeenEntered = true; while (cardHasBeenEntered) { //Tell the user to chose from a list of options Console.WriteLine("Choose from a list of options...."); //loop through the options declared on line 21, and print foreach (KeyValuePair <int, string> option in ATMoptions) { Console.WriteLine($"{option.Key}: - {option.Value}"); } bool validChosenAction = int.TryParse(Console.ReadLine(), out int chosenAction); // If the chosen value is value go into the specifics of the options if (validChosenAction) { switch (chosenAction) { case 1: //returns the user to the welcome page and clears the console cardHasBeenEntered = false; Console.Clear(); break; case 2: //Displays the value of the current card/account Console.WriteLine($"W: {chosenCard.Balance} Wibbly Dollars."); break; case 3: //ask the user to enter an amount to withdraw Console.WriteLine($"Select an amount to withdraw..."); bool validWithdrawal = int.TryParse(Console.ReadLine(), out int ammount); //if its valid go to method if (validWithdrawal) { WithdrawWibblyDollars(ammount, chosenCard); } break; default: break; } } } } }
static void ChoosenActionSwitch(int chosenAction, ref bool cardHasBeenEntered, ref CardModel chosenCard, ref WalletModel wallet) { string input; switch (chosenAction) { case 1: //returns the user to the welcome page and clears the console cardHasBeenEntered = false; Console.Clear(); break; case 2: //Displays the value of the current card/account Console.WriteLine($"W: {chosenCard.Balance} Wibbly Dollars.\nWallet balance: {wallet.Balance}"); break; case 3: //This could be put into a method but it would be harder to read, lets leave it here while (true) { //ask the user to enter an amount to withdraw Console.WriteLine($"Select an amount to withdraw..."); input = Console.ReadLine(); if (input.ToLower().Contains("cancel")) { cardHasBeenEntered = false; break; } if (int.TryParse(input, out int ammount)) { if (ammount < 0) { Console.WriteLine("Inserted value can not be negative\n"); } else { WithdrawWibblyDollars(ammount, chosenCard, wallet); break; } } else { Console.WriteLine($"Inserted value isnt acccepted, try again\n"); } } break; case 4: //This could be put into a method but it would be harder to read, lets leave it here while (true) { //ask the user to enter an amount to withdraw Console.WriteLine($"Select an amount to deposit..."); input = Console.ReadLine(); if (input.ToLower().Contains("cancel")) { cardHasBeenEntered = false; break; } if (int.TryParse(input, out int ammount)) { if (ammount < 0) { Console.WriteLine("Inserted value can not be negative\n"); } else { DepositWibblyDollars(ammount, chosenCard, wallet); break; } } else { Console.WriteLine($"Inserted value isnt acccepted, try again\n"); } } break; default: Console.WriteLine($"Inserted value isnt acccepted, try again\n"); break; } }