Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.concurrent.ThreadLocalRandom;
- public class BankingSystem {
- static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- selectChoice();
- String[] strArray = new String[3];
- routeChoice(strArray);
- choiceAfterLoggedIn();
- }
- public static void selectChoice() {
- System.out.println("1. Create an account");
- System.out.println("2. Log into an account");
- System.out.println("0. Exit");
- }
- public static void routeChoice(String [] choices) {
- for (int i = 0; i < choices.length; i++ ){
- choices[i] = scanner.nextLine();
- switch (i) {
- case 1 : createAccount();
- break;
- case 2 : ValidateCardNumberAndPin();
- break;
- case 3 : choiceAfterLoggedIn();
- System.out.println("You have successfully logged out");
- }
- }
- }
- public static void createAccount(){
- System.out.print("Your card number has been created");
- System.out.println("Your card number :" + generateRandomNumber());
- System.out.println("Your card PIN: " + generatePin());
- }
- //ThreadLocalRandom.current().nextLong(min, max);
- //min <= randomLongNumber < max
- public static String generateRandomNumber() {
- //int BIN = new Random(400000).nextInt();
- int BIN = ThreadLocalRandom.current().nextInt(40000, 50000);
- String random = Integer.toString(BIN);
- Random randomobj = new Random();
- //long seed = 1000000000;
- //randomobj.setSeed(seed);
- //new Random().nextInt((10-5+1))+5; // [5,10], upper bound included
- Long random10digits = ThreadLocalRandom.current().nextLong(10000000000L,90000000000L);
- String random1 = Long.toString(random10digits);
- return random + random1;
- }
- //generate a pin in a range from 0000 to 9999
- public static String generatePin() {
- Random rnd = new Random();
- int Pin = rnd.nextInt(9999);
- String pin = Integer.toString(Pin);
- return pin;
- }
- public static void LogIntoAccount() {
- ValidateCardNumberAndPin();
- }
- // you can use HashMap to store the acc number and pin. then you can use getOrDefault to get the pin from the acc number (if u have done it in this way HashMap<acc, pin> )
- // and if that value is not present you can return a default value.
- // Hashmap makes it easy also a good practice for it, example cardNumber = 400k (400 000) + 10x random digit 0-9
- // cards.get(inputCardNumber).equals(inputPIN) ... looking for pairs matching with the input where cards is my Hashmap
- // public static void ValidateCardNumberAndPin() {
- // System.out.println("Enter your card number: ");
- // String userCardNo = scanner.nextLine();
- // System.out.println("Enter your PIN: ");
- // String userPIN = scanner.nextLine();
- // if (userCardNo == generateRandomNumber() && userPIN == generatePin()) {
- // System.out.println("You have successfully logged in!");
- // choiceAfterLoggedIn();
- // } else {
- // System.out.println("Wrong card number of PIN!");
- // LogIntoAccount();
- // }
- // }
- //using hashmap
- public static HashMap<String, String> StoreUserCardAndPin() {
- int BIN = ThreadLocalRandom.current().nextInt(40000, 50000);
- String random = Integer.toString(BIN);
- Random randomobj = new Random();
- Long random10digits = ThreadLocalRandom.current().nextLong(10000000000L,90000000000L);
- String random1 = Long.toString(random10digits);
- String CardNo = random + random1;
- Random rnd = new Random();
- int Pin = rnd.nextInt(9999);
- String pin = Integer.toString(Pin);
- HashMap<String, String> maptoAccPin = new HashMap<>();
- maptoAccPin.put(CardNo, pin);
- return maptoAccPin;
- }
- public static void ValidateCardNumberAndPin() {
- System.out.println("Enter your card number: ");
- String userCardNo = scanner.nextLine();
- System.out.println("Enter your PIN: ");
- String userPIN = scanner.nextLine();
- System.out.println("the keys to this customer account is :" + StoreUserCardAndPin());
- if (StoreUserCardAndPin().entrySet().equals(userCardNo) && StoreUserCardAndPin().entrySet().contains(userPIN)) {
- System.out.println("You have successfully logged in!");
- choiceAfterLoggedIn();
- }else {
- System.out.println("Wrong card number of PIN!");
- LogIntoAccount();
- }
- }
- public static void choiceAfterLoggedIn() {
- String[] selection = new String[3];
- System.out.println("1. Balance");
- System.out.println("2. Log out");
- System.out.println("3. Exit");
- for (int i = 0; i < selection.length; i++) {
- selection[i] = scanner.nextLine();
- if ( selection[i].equals(1)) {
- System.out.println("Balance: " + "0");
- }
- if (selection[i].equals(2)) {
- System.out.println("You have successfully logged out");
- } else {
- System.out.println("Bye!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement