
Jade Ruby T.
asked 07/18/21How to create an OOP?
Create a new file called Account.java and construct a class called Account, which models a simple bank account. It contains the following members:
-
Two private instance variables:
accountNumber
(int), andbalance
(double) which maintains the current account balance. - Constructors (overloaded):
-
Implement an overloaded constructor that takes in as input the account number and simply sets balance to 3000.00. This prints
"First Overloaded Constructor"
as well. -
Implement another overloaded constructor that takes in an account number and a balance as arguments. This prints
"Second Overloaded Constructor"
as well. - Getters for the instance variables
- public methods
-
public void credit(double)
- adds the given amount to the balance -
public void debit(double)
- subtracts the given amount from the balance (obviously, one cannot subtract more from what is available - prints"Insufficient Funds"
if this is the case) -
public String toString()
- returns"Account Number: xxx, Balance: Php xxx.xx"
, with balance rounded to two decimal places.
Input Format
The first input is either a 1 or 2. If it is a 1, it means to create an Account via the first overloaded constructor and an account number follows. If it is a 2, 2 values follow, an account number and an amount in pesos. This is then followed by a number m representing the number of operations to be performed followed by the operations themselves as specified below: 1 - credit 2 - debit 3 - getAccountNumber 4 - getBalance 5 - toString For both operations 1 and 2, an amount in pesos follows. The other operations do not have inputs to them.
Input Sample
Output Format
For the getter operations (3 - 4), including toString(), the values they return must be printed. For debit(), when the amount to be deducted is bigger than the balance, print "Insufficient Funds" while credit() will not have any output.
Output Sample
1 Expert Answer

Patrick B. answered 07/19/21
Math and computer tutor/teacher
class Account
{
private int accountNumber;
private double balance;
public Account( int acctNum)
{
accountNumber = acctNum;
balance = 3000;
System.out.println(" First Overloaded Constructor : account # = " + accountNumber + " : balance = " + balance);
}
public Account(int acctNum, double balAmt)
{
accountNumber = acctNum;
balance=balAmt;
System.out.println(" Second Overloaded Constructor : account # = " + accountNumber + " : balance = " + balance);
}
public Account(Account account)
{
this.accountNumber = account.accountNumber;
this.balance = account.balance;
System.out.println(" Copy Constructor : account # = " + accountNumber + " : balance = " + balance);
}
int GetAccountNumber(){ return(accountNumber); }
double GetBalance() { return(balance); }
public void Credit(double creditAmt) { balance+=creditAmt; }
public void Debit(double debitAmt) { balance-=debitAmt; }
@Override public String toString()
{
return(
new String(
"Account number:" + accountNumber + ", Balance: Php " + String.format("%.2f",balance)
)
);
}
}
//****************************************************************************
import java.util.Scanner;
class AccountMain
{
private Scanner scanner;
AccountMain() { scanner = new Scanner(System.in); }
public int Menu()
{
System.out.println("*****************************************");
System.out.println(" <1> Open Account with Default Balance ");
System.out.println(" <2> Open Account with New Balance ");
System.out.println(" <3> Credit ");
System.out.println(" <4> Debit ");
System.out.println(" <5> Balance Inquiry ");
System.out.println("*****************************************");
System.out.print(" Input your selection or ZER0 to quit :>");
int x = scanner.nextInt();
return(x);
}
public void Go()
{
int rspMenu=-1;
Account account=null;
int accountNumber;
double balance;
double flAmt;
while (rspMenu!=0)
{
rspMenu = Menu();
switch (rspMenu)
{
case 1:
case 2:
{
accountNumber=-1;
while (accountNumber<0)
{
System.out.print(" Please input account # :>");
accountNumber = scanner.nextInt();
}
if (rspMenu==1)
{
account = new Account(accountNumber);
}
else
{
balance = -1;
while (balance<0)
{
System.out.print(" Please input account balance :>");
balance = scanner.nextDouble();
}
account = new Account(accountNumber,balance);
}
break;
} //case 1,2
case 3:
case 4:
{
if (account!=null)
{
flAmt = -1;
while (flAmt<=0)
{
System.out.print(" Please input amount :>");
flAmt = scanner.nextDouble();
}
if (rspMenu==3)
{
account.Credit(flAmt);
}
else
{
if (flAmt<=account.GetBalance())
{
account.Debit(flAmt);
}
else
{
System.out.println("Insufficient Funds");
}
} //rspMenu
} //acount Null
else
{
System.out.println(" Please Open Account ");
}
break;
} //cases 3,4
case 5:
{
if (account != null)
{
System.out.println(account);
}
else
{
System.out.println(" Please Open Account ");
}
break;
}
} //switch
} //while
} //Go
public static void main(String args[])
{
(new AccountMain()).Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
The sample does not agree with the directions and statement of the problem. There are FOUR (4) commands to be executed in the input while the input line shows 3. Instead it displays the account #. Therefore, I am using a menu interface. Either fix the sample input or clarify the directions. THIS PROBLEM IS SOLVED !!!! DO NOT REPOST- You will receive the same response!!07/19/21