Untitled

Anonymous
Plain Text
11/03/2025 3:35 AM
5.1 KB
18
Indexable
Never
import java.util.Scanner;

public class Client_Pisk {
    private static SavingsAccount_Pisk[] sa = new SavingsAccount_Pisk[100];
    private static int count = 0;
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("\nJBank Main Menu");
            System.out.println("[1] New Account");
            System.out.println("[2] Balance Inquiry");
            System.out.println("[3] Deposit");
            System.out.println("[4] Withdraw");
            System.out.println("[5] Client Profile");
            System.out.println("[6] Close Account");
            System.out.println("[7] Exit");
            System.out.print("Enter choice: ");
            String input = sc.nextLine().trim();
            int choice = toInt(input);

            switch (choice) {
                case 1 -> newAccount();
                case 2 -> balanceInquiry();
                case 3 -> deposit();
                case 4 -> withdraw();
                case 5 -> clientProfile();
                case 6 -> closeAccount();
                case 7 -> { System.out.println("Thank you for using JBank."); return; }
                default -> System.out.println("Invalid choice. Try again.");
            }
        }
    }

    private static int toInt(String s) {
        try { return Integer.parseInt(s); } catch (Exception e) { return 0; }
    }

    private static double toDouble(String s) {
        try { return Double.parseDouble(s); } catch (Exception e) { return -1; }
    }

    private static void newAccount() {
        System.out.print("Enter Name (letters only): ");
        String name = sc.nextLine().trim();
        while (!name.matches("[a-zA-Z ]+")) {
            System.out.print("Invalid name. Enter again: ");
            name = sc.nextLine().trim();
        }

        System.out.print("Enter Address: ");
        String addr = sc.nextLine().trim();

        System.out.print("Enter Birthday (YYYY-MM-DD): ");
        String bday = sc.nextLine().trim();

        System.out.print("Enter Contact Number (digits only): ");
        String contact = sc.nextLine().trim();
        while (!contact.matches("\\d+")) {
            System.out.print("Invalid contact. Enter again: ");
            contact = sc.nextLine().trim();
        }

        System.out.print("Initial Deposit (min Php 5000): ");
        double dep = toDouble(sc.nextLine().trim());
        while (dep < 5000) {
            System.out.print("Minimum Php 5000 required. Enter again: ");
            dep = toDouble(sc.nextLine().trim());
        }

        SavingsAccount_Pisk acc = new SavingsAccount_Pisk(name, addr, bday, contact, dep);
        sa[count++] = acc;
        System.out.println("Account successfully created!");
        System.out.println("Account Number: " + acc.getAccountNo());
    }

    private static int findAccount(int no) {
        for (int i = 0; i < count; i++) {
            if (sa[i] != null && sa[i].validateAcctNumber(no)) return i;
        }
        return -1;
    }

    private static void balanceInquiry() {
        System.out.print("Enter Account Number: ");
        int no = toInt(sc.nextLine().trim());
        int idx = findAccount(no);
        if (idx == -1) System.out.println("Account not found.");
        else sa[idx].balanceInquiry();
    }

    private static void deposit() {
        System.out.print("Enter Account Number: ");
        int no = toInt(sc.nextLine().trim());
        int idx = findAccount(no);
        if (idx == -1) { System.out.println("Account not found."); return; }
        System.out.print("Enter Amount to Deposit: ");
        double amt = toDouble(sc.nextLine().trim());
        sa[idx].deposit(amt);
    }

    private static void withdraw() {
        System.out.print("Enter Account Number: ");
        int no = toInt(sc.nextLine().trim());
        int idx = findAccount(no);
        if (idx == -1) { System.out.println("Account not found."); return; }
        System.out.print("Enter Amount to Withdraw: ");
        double amt = toDouble(sc.nextLine().trim());
        sa[idx].withdraw(amt);
    }

    private static void clientProfile() {
        System.out.print("Enter Account Number: ");
        int no = toInt(sc.nextLine().trim());
        int idx = findAccount(no);
        if (idx == -1) { System.out.println("Account not found."); return; }
        sa[idx].getClientDetails();
        System.out.println("Balance: Php " + String.format("%.2f", sa[idx].getBalance()));
    }

    private static void closeAccount() {
        System.out.print("Enter Account Number: ");
        int no = toInt(sc.nextLine().trim());
        int idx = findAccount(no);
        if (idx == -1) { System.out.println("Account not found."); return; }
        System.out.print("Are you sure you want to close this account? (y/n): ");
        String ans = sc.nextLine().trim();
        if (ans.equalsIgnoreCase("y")) {
            sa[idx].closeAccount();
        } else {
            System.out.println("Account closure canceled.");
        }
    }
}
Editor is loading...
Leave a Comment