Reputation: 65
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
Upvotes: 1
Views: 394
Reputation: 11602
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;
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;
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;
}
}
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());
}
}
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.
Upvotes: 2