Advertisement
Guest User

Untitled

a guest
Feb 11th, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map.Entry;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7. import java.util.concurrent.ThreadLocalRandom;
  8.  
  9. public class BankingSystem {
  10.     static Scanner scanner = new Scanner(System.in);
  11.     public static void main(String[] args) {
  12.         selectChoice();
  13.         String[] strArray = new String[3];
  14.         routeChoice(strArray);
  15.         choiceAfterLoggedIn();
  16.  
  17.     }
  18.  
  19.     public static void selectChoice() {
  20.         System.out.println("1. Create an account");
  21.         System.out.println("2. Log into an account");
  22.         System.out.println("0. Exit");
  23.     }        
  24.  
  25.  
  26.     public static void routeChoice(String [] choices) {
  27.  
  28.         for (int i = 0; i < choices.length; i++ ){
  29.             choices[i] = scanner.nextLine();
  30.             switch (i) {
  31.             case 1 : createAccount();
  32.             break;
  33.  
  34.             case 2 : ValidateCardNumberAndPin();
  35.             break;
  36.  
  37.             case 3 : choiceAfterLoggedIn();
  38.  
  39.             System.out.println("You have successfully logged out");
  40.             }
  41.         }
  42.     }
  43.  
  44.     public static void createAccount(){
  45.         System.out.print("Your card number has been created");
  46.         System.out.println("Your card number :" + generateRandomNumber());
  47.         System.out.println("Your card PIN: " + generatePin());
  48.     }
  49.  
  50.  
  51.     //ThreadLocalRandom.current().nextLong(min, max);
  52.     //min <= randomLongNumber < max
  53.     public static String generateRandomNumber() {
  54.  
  55.         //int BIN = new Random(400000).nextInt();  
  56.         int BIN = ThreadLocalRandom.current().nextInt(40000, 50000);
  57.         String random = Integer.toString(BIN);
  58.         Random randomobj = new Random();
  59.         //long seed = 1000000000;
  60.         //randomobj.setSeed(seed);
  61.         //new Random().nextInt((10-5+1))+5; // [5,10], upper bound included
  62.         Long random10digits = ThreadLocalRandom.current().nextLong(10000000000L,90000000000L);
  63.         String random1 = Long.toString(random10digits);
  64.  
  65.         return random + random1;
  66.     }
  67.  
  68.     //generate a pin in a range from 0000 to 9999
  69.     public static String generatePin() {
  70.         Random rnd = new Random();
  71.         int Pin = rnd.nextInt(9999);
  72.         String pin = Integer.toString(Pin);
  73.         return pin;
  74.     }
  75.  
  76.     public static void LogIntoAccount() {
  77.         ValidateCardNumberAndPin();
  78.     }
  79.  
  80.     //  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> )
  81.     //  and if that value is not present you can return a default value.
  82.  
  83.     //  Hashmap makes it easy also a good practice for it, example cardNumber = 400k (400 000) + 10x random digit 0-9
  84.     //  cards.get(inputCardNumber).equals(inputPIN) ... looking for pairs matching with the input where cards is my Hashmap
  85.     //  public static void ValidateCardNumberAndPin() {        
  86.     //      System.out.println("Enter your card number: ");
  87.     //      String userCardNo = scanner.nextLine();
  88.     //      System.out.println("Enter your PIN: ");
  89.     //      String userPIN = scanner.nextLine();
  90.     //      if (userCardNo == generateRandomNumber() && userPIN == generatePin()) {
  91.     //          System.out.println("You have successfully logged in!");
  92.     //          choiceAfterLoggedIn();
  93.     //      } else {
  94.     //          System.out.println("Wrong card number of PIN!");
  95.     //          LogIntoAccount();
  96.     //      }
  97.     //  }
  98.  
  99.     //using hashmap
  100.  
  101.     public static HashMap<String, String> StoreUserCardAndPin() {
  102.         int BIN = ThreadLocalRandom.current().nextInt(40000, 50000);
  103.         String random = Integer.toString(BIN);
  104.         Random randomobj = new Random();
  105.         Long random10digits = ThreadLocalRandom.current().nextLong(10000000000L,90000000000L);
  106.         String random1 = Long.toString(random10digits);
  107.         String CardNo = random + random1;
  108.         Random rnd = new Random();
  109.         int Pin = rnd.nextInt(9999);
  110.         String pin = Integer.toString(Pin);    
  111.         HashMap<String, String> maptoAccPin = new HashMap<>();     
  112.         maptoAccPin.put(CardNo, pin);
  113.        
  114.         return maptoAccPin;
  115.     }
  116.  
  117.     public static void ValidateCardNumberAndPin() {
  118.         System.out.println("Enter your card number: ");
  119.         String userCardNo = scanner.nextLine();
  120.         System.out.println("Enter your PIN: ");
  121.         String userPIN = scanner.nextLine();       
  122.         System.out.println("the keys to this customer account is  :"  + StoreUserCardAndPin());
  123.         if (StoreUserCardAndPin().entrySet().equals(userCardNo) && StoreUserCardAndPin().entrySet().contains(userPIN)) {
  124.         System.out.println("You have successfully logged in!");
  125.             choiceAfterLoggedIn();
  126.         }else {
  127.  
  128.             System.out.println("Wrong card number of PIN!");
  129.             LogIntoAccount();
  130.         }
  131.     }
  132.  
  133.  
  134.  
  135.     public static void choiceAfterLoggedIn() {
  136.         String[] selection = new String[3];
  137.         System.out.println("1. Balance");
  138.         System.out.println("2. Log out");
  139.         System.out.println("3. Exit");
  140.         for (int i = 0; i < selection.length; i++) {
  141.  
  142.             selection[i] = scanner.nextLine();
  143.             if ( selection[i].equals(1)) {
  144.                 System.out.println("Balance: " + "0");
  145.             }
  146.             if (selection[i].equals(2)) {
  147.                 System.out.println("You have successfully logged out");
  148.             } else {
  149.                 System.out.println("Bye!");
  150.             }
  151.         }
  152.     }
  153.  
  154.  
  155. }
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement