1

In the following code have used the main method to print the account number and updated balance after every transaction, or until a Q is entered.

In the methodUpdate balance which, given the balance of the account, the type of transaction( D = deposit, W = withdrawl, Q = quit) and the amount for the transaction, computers and returns the new account balance after depositing or withdrawing the given amount number.

However now I have ran into problems and not sure how to fix this code I have produced

import java.util.Scanner ;
public class CustomerAccount
{
public static void main( String[] args ) 
{
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the account number: ") ;
    String accountNumber = in.nextLine() ; 
    System.out.print("Please enter the initial balance: ") ;
    int startingBal = in.nextInt() ;
    int updatedBal = startingBal ;
    System.out.print("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ") ;
    String type = in.nextLine() ;
    while(!"Q".equals(type)) 
    {
        System.out.print("Please enter the amount to be deposited or withdrawn: ") ;
        int adjustment = in.nextInt();
        if(type.equals("D"))
        {
            updatedBal = updatedBalance(depositAmt);
        }
        else 
            updatedBal = updatedBalance(withdrawAmt);
        }
        System.out.println("Account Number: " + accountNumber + "    UpdatedBalance: $" + updatedBal) ;
    }
}
public static int updatedBalance(int amount)
{
    amount = amount + adjustment ;
    return amount;
}
}

gives the follwoing output:

[File: /CustomerAccount.java Line: 28, Column: 19] class, interface, or enum expected
[File: /CustomerAccount.java Line: 31, Column: 9] class, interface, or enum expected
[File: /CustomerAccount.java Line: 32, Column: 5] class, interface, or enum expected
10
  • use do-while loop, first get the input work on it, then check loop condition. Commented Jun 29, 2016 at 17:41
  • After you fix the brace thingy, you will run into an issue which can be fixed by looking at the solutions here ==> stackoverflow.com/questions/13102045/… Commented Jun 29, 2016 at 17:44
  • yes now it can not find the variables depositAmt, withdrawAmt and adjustment Commented Jun 29, 2016 at 17:45
  • 3
    @timNorth - Honestly, I think you should think about going through a basic tutorial or something? Commented Jun 29, 2016 at 17:50
  • 1
    yes I think that is a good idea Commented Jun 29, 2016 at 17:56

1 Answer 1

2

Here is a little 'improved' version of your code but still there are a lot of things to do. It seems you are a beginner, and the main modification I've made is making an abstraction. I've added a new class named "CustomerAccount" and which stores;

  • Data about the account
  • Methods like Deposit and Withdraw functionality

So, now the account data and functionality is encapsulated with the CustomerAccount class.

The entry point of the program becomes the CustomerAccountHandler class.

Here is the code and output, hope that these all helps on your learning process;

A - CustomerAccount Class

public class CustomerAccount {

    private final String accountId; // account number is final, it should never be updated
    private int amount;

    // Constructor with two paremeters
    public CustomerAccount(String accountId, int initialAmount) {
        this.accountId = accountId;
        this.amount = initialAmount;
    }

    // Funcionality : deposits to the amount
    // Returns      : latest amount
    public int deposit(int adjustment) {
        this.amount = amount + adjustment;

        return this.amount;
    }

    // Funcionality : witdraws from the amount
    // Returns      : latest amount
    public int withdraw(int adjustment) {
        this.amount = amount - adjustment;

        return this.amount;
    }

    // getters

    public String getAccountId() {
        return this.accountId;
    }

    public int getAmount() {
        return this.amount;
    }

}

B - Demo Code

import java.util.Scanner;

public class CustomerAccountHandler {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the account number: ");
        String accountId = in.nextLine();
        System.out.print("Please enter the initial balance: ");
        int startingBal = Integer.parseInt(in.nextLine());

        // Create the account here
        CustomerAccount account = new CustomerAccount(accountId, startingBal);

        int updatedBal, adjustment; // create here, we will use it a lot
        String type;

        while (true) {
            System.out.println("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ");
            type = in.nextLine();

            if(type.equals("Q")) {
                System.out.println("Quitting transactions...");
                break;
            } else if(type.equals("W") || type.equals("D")) {
                System.out.println("Please enter the amount to be deposited or withdrawn: ");
                adjustment = Integer.parseInt(in.nextLine());

                if (type.equals("D")) {
                    updatedBal = account.deposit(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                } else if(type.equals("W")) {
                    updatedBal = account.withdraw(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                }
            } else {
                System.out.println("Please enter a valid Command: D,W,Q");
            }
        }

        System.out.println("\nAccount Number: " + account.getAccountId() + "    UpdatedBalance: $" + account.getAmount());
    }
}

C - Sample Output

Please enter the account number: Levent1299
Please enter the initial balance: 150
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
W
Please enter the amount to be deposited or withdrawn: 
30
Updated Balanced : 120
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
D
Please enter the amount to be deposited or withdrawn: 
80
Updated Balanced : 200
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
Q
Quitting transactions...

Account Number: Levent1299    UpdatedBalance: $200

Always use encapsulation pillar of Object Oriented Programming. There should be a different class for a different domain, CustomerAccount class in this case, stores the customer data and customer methods and encapsulates them.

Sign up to request clarification or add additional context in comments.

1 Comment

@timNorth I love to practice via here and glad if it really helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.