flexycode / CCPRGG1L_ACTIVITY_COM23P

CCPRGG1L-COMP23P Group Project for 1st semester final exam project, Exercises and Lab Activity for Professor Jay Abaleta
6 stars 4 forks source link

ROBUSTA #10

Open IYB-Mata opened 6 months ago

IYB-Mata commented 6 months ago

We have added new features and made some modifications to improve the program's robustness to reduce the error rates.

Here's the program with added new features.

package Fundamentals;
import java.util.Scanner;

public class CryptoBank {
    private static final int MAX_ACCOUNTS = 11; // edited also this from 'MAX_ACCOUNTS = 10' because the creation of accounts in array starts from 1 and skip index 0 but the computer will always count index 0 to solve the problem we just need to add 1 on maxAccount.
    private static String[] accountNames = new String[MAX_ACCOUNTS];
    private static int[] accountNumbers = new int[MAX_ACCOUNTS];
    private static double[] balances = new double[MAX_ACCOUNTS];
    private static int accountCount = 1;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        System.out.println("Welcome to ALT Crypto Bank!");

        do {
            System.out.println("\t[1.] Create New Account");
            System.out.println("\t[2.] Deposit Money");
            System.out.println("\t[3.] Withdraw Money");
            System.out.println("\t[4.] Check Balance");
            System.out.println("\t[5.] Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    createNewAccount(scanner);
                    break;
                case 2:
                    performTransaction(scanner, true); // true for deposit
                    break;
                case 3:
                    performTransaction(scanner, false); // false for withdraw
                    break;
                case 4:
                    checkBalance(scanner);
                    break;
                case 5:
                    System.out.println("Thank you for using ALT Crypto Bank!");
                    break;
                default:
                    System.out.println("Invalid choice. Please enter a valid option.");
            }
        } while (choice != 5);

        scanner.close();
    }
// CASE 1: Creating new account
    private static void createNewAccount(Scanner scanner) {
        if (accountCount >= MAX_ACCOUNTS) {
            System.out.println("Maximum account limit reached.");
            return;
        }

        System.out.print("Enter Account Name: ");
        String accountName = scanner.next();

        accountNames[accountCount] = accountName;
        accountNumbers[accountCount] = accountCount ; 
        balances[accountCount] = 0.0;

        System.out.println("Account created successfully. Your account number is: " + accountCount);
        accountCount++; // moving the incrementation after the display.
    }
    // I have added a feature here...
    private static void performTransaction(Scanner scanner, boolean isDeposit) {
        System.out.print("Enter Account Number: ");
        int accountNumber; // edited this
        // conditional statement  for scanner to know if the inputted account number is in integer otherwise will prompt again to menu.
        if(scanner.hasNextInt()) {
            accountNumber = scanner.nextInt();
        } else {
                System.out.println("Invalid input, please enter integers only.");
                scanner.next();  // consume the invalid input
                return;
            }// till here.

        if (accountNumber < 0 || accountNumber >= accountCount) {
            System.out.println("Invalid account number.");
            return;
        }
    // also here......
        System.out.print("Enter Amount: ");
        double amount; // edited this
//from here...
        if(scanner.hasNextDouble()) {
            amount = scanner.nextDouble();
        }else {
            System.out.println("Invalid input, please enter integers only");
            scanner.next(); // consume the invalid input
            return;
        } // to here.....

        if (isDeposit) {
            balances[accountNumber] += amount;
            System.out.println("Amount deposited successfully. Current Balance: " + balances[accountNumber]);
        } else {
            if (amount <= balances[accountNumber]) {
                balances[accountNumber] -= amount;
                System.out.println("Amount withdrawn successfully. Remaining Balance: " + balances[accountNumber]);
            } else {
                System.out.println("Insufficient balance or invalid amount.");
            }
        }
    }

    private static void checkBalance(Scanner scanner) {
        System.out.print("Enter Account Number: ");
        int accountNumber = scanner.nextInt();

        if (accountNumber < 0 || accountNumber >= accountCount) {
            System.out.println("Invalid account number.");
            return;
        }

        System.out.println("Account Name: " + accountNames[accountNumber] + " - Current Balance: " + balances[accountNumber]);
    }
}

Changes:

line 5: added 1 more value to MAX_ACCOUNTS to create exactly 10 bank accounts.

line 60: in the createNewAccount method, I have changed unnecessary modifications in the value of the account like "-1" on accountCount, We just simplified it by moving the incrementation after the display.

line 67-78(performTranscation method): We added a simple feature that tracks if the user's input is in integer, otherwise prompts the user back to the menu.

line 88-93: We added the same feature.

Kindly test and debug the program. Feel free to comment if there are some errors or bugs so we can fix it immediately. THAAANKKKS!

flexycode commented 6 months ago

My personal Feedback:

Great job on the new features and modifications to improve the program's robustness! Here are my comments and expressions:

  1. Comment on line 5:
  1. Comment on lines 60-61:
  1. Comment on lines 67-78:
  1. Comment on lines 88-93:

Overall, your changes and additions have greatly improved the program. Keep up the great work guys! 😎