TheRightGuy

Advice 2.0

Feb 17th, 2022 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. public class App {
  2.     private static final BankingSystem bankingSystem = new BankingSystem();
  3.  
  4.     public static void main(String[] args) {
  5.         bankingSystem.getCustomers().add(new Customer("Man", 100));
  6.         bankingSystem.getCustomers().add(new Customer("Tom", 100));
  7.         bankingSystem.getBankDetails();
  8.         getAllCustomers();
  9.     }
  10.  
  11.     private static void getAllCustomers() {
  12.         for (Customer customer : bankingSystem.getCustomers()) {
  13.             System.out.println("Name : " + customer.getName() + "\nBalance " + customer.getBalance() + "$\nAccountID " + new BigInteger(53, new Random()));
  14.         }
  15.     }
  16. }
  17.  
  18.  
  19. public class Customer {
  20.  
  21.     private final String name;
  22.     private int balance;
  23.  
  24.     public Customer(String name, int balance){
  25.         this.name = name;
  26.         this.balance = balance;
  27.     }
  28.  
  29.     public String getName() {
  30.         return name;
  31.     }
  32.  
  33.     public int getBalance() {
  34.         return balance;
  35.     }
  36.  
  37.     public void setBalance(int balance) {
  38.         this.balance = balance;
  39.     }
  40. }
  41.  
  42.  
  43. import java.math.BigInteger;
  44. import java.util.ArrayList;
  45. import java.util.Random;
  46. import java.util.Scanner;
  47.  
  48. public class BankingSystem {
  49.     private final Scanner input = new Scanner(System.in);
  50.     private final String userInput = input.nextLine();
  51.     private final ArrayList<Customer> customers = new ArrayList<>();
  52.  
  53.     public ArrayList<Customer> getCustomers() {
  54.         return customers;
  55.     }
  56.  
  57.     void getBankDetails() {
  58.         boolean hasCustomer = false;
  59.         for (Customer customer : customers) {
  60.             hasCustomer = isCustomerPresent(hasCustomer, customer);
  61.         }
  62.         if(!hasCustomer){
  63.             System.out.println("We don't have a customer with that name");
  64.         }
  65.     }
  66.  
  67.     private boolean isCustomerPresent(boolean haveCustomer, Customer value) {
  68.         if (value.getName().equals(userInput)) {
  69.             haveCustomer = true;
  70.             atmActions(value);
  71.             customerDetails(value);
  72.         }
  73.         return haveCustomer;
  74.     }
  75.  
  76.     private void customerDetails(Customer value) {
  77.         System.out.println("Name : " + value.getName() + "\nBalance " + value.getBalance() + "$\nAccountID" +  new BigInteger(53, new Random()));
  78.     }
  79.  
  80.     void atmActions(Customer customer) {
  81.         System.out.println("Hello " + customer.getName() + customerMessage());
  82.         int atm = input.nextInt();
  83.         while (atm != 3) {
  84.             balanceModification(customer, atm);
  85.             System.out.println(customerMessage());
  86.             atm = input.nextInt();
  87.         }
  88.     }
  89.  
  90. private void balanceModification(Customer customer, int atm) {
  91.         if (atm == 1) {
  92.             depositMoney(customer);
  93.         } else if (atm == 2) {
  94.             withdrawMoney(customer);
  95.         }
  96.     }
  97.  
  98.     private String customerMessage() {
  99.         return "\nIf you wish to deposit money press 1 \nIf you wish to withdraw money press 2\nTo exit press 3";
  100.     }
  101.  
  102.     private void withdrawMoney(Customer customer) {
  103.         System.out.println("How much do you want to withdraw?");
  104.         int balance = input.nextInt();
  105.         balance = balanceCondition(balance, customer.getBalance(), "You don't have enough money in the account.\nYour current balance is " + customer.getBalance());
  106.         int withdraw = customer.getBalance() - balance;
  107.         customer.setBalance(withdraw);
  108.         System.out.println("Your current balance is $" + customer.getBalance());
  109.     }
  110.  
  111.     private void depositMoney(Customer customer) {
  112.         System.out.println("How much do you want to add to your account?");
  113.         int balance = input.nextInt();
  114.         int BALANCE_LIMIT = 999999999;
  115.         balance = balanceCondition(balance, BALANCE_LIMIT, "You can not add that much money at once. Add below $999999999");
  116.         int addingToBalance = customer.getBalance() + balance;
  117.         customer.setBalance(addingToBalance);
  118.         System.out.println("Your current balance is $" + customer.getBalance());
  119.     }
  120.  
  121.     private int balanceCondition(int balance, int balanceLimit, String message) {
  122.         while (balance > balanceLimit) {
  123.             System.out.println(message);
  124.             balance = input.nextInt();
  125.         }
  126.         return balance;
  127.     }
  128. }
  129.  
Add Comment
Please, Sign In to add comment