What is the code? Create an Account class (Account.java) that a bank might use t
ID: 3586453 • Letter: W
Question
What is the code?
Create an Account class (Account.java) that a bank might use to represent a customer's bank account.
Specification
1. Include a private variable of type int to represent the account balance.
2. Provide a constructor that has a parameter for initial balance and uses it to initialize the account balance.
3. Provide 3 member functions:
credit - adds an amount passed as an argument to the current balance.
debit - withdraws an amount passed as an argument from the current balance.
getBalance - returns the balance amount.
4. Create a program (AccountTest.java) that creates two Account objects and validate the methods of the class Account. (Call the methods passing the appropriate values to make sure they produce the expected results.)
Explanation / Answer
Account.java
Public class Account {
private String name;
private double balance;
public Account(String name, double balance) {
this.name = name;
if (balance > 0.0)
this.balance = balance;
}
public void credit(double creditAmount) {
if (creditAmount > 0.0)
balance = balance + creditAmount;
}
public double getBalance() {
return balance;
}
// New method,debit
public void debit(double amt) {
if (balance >= amt)
balance = balance - amt;
if (balance < amt)
System.out.println("Withdrawal amount exceeded account balance.");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
AccountTest.java
Import.java.util.Scanner;
public class AccountTest {
public static void main(String[] args) {
Account account1 = new Account("Jane Green", 50.00);
Account account2 = new Account("John Blue", -7.53);
// display initial balance of each object
System.out.printf("%s balance: $%.2f%n",
account1.getName(), account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n",
account2.getName(), account2.getBalance());
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
// 1 - Credit
System.out.print("Enter credit amount for account1: ");
double creditAmount = input.nextDouble();
System.out.printf("%nadding %.2f to account1 balance%n%n",
creditAmount);
account1.credit(creditAmount);
// View
displayAccount(account1);
displayAccount(account2);
// 1 -debit
System.out.print("Enterdebital amount for account1: ");
doubledebitalAmount = input.nextDouble();
System.out.printf("%nwithdrawing %.2f from account1 balance%n%n",
debitalAmount);
account1.withdraw(withdrawalAmount);
// View
displayAccount(account1);
displayAccount(account2);
// 2 - Credit
System.out.print("Enter credit amount for account2: ");
creditAmount = input.nextDouble();
System.out.printf("%nadding %.2f to account2 balance%n%n",
creditAmount);
account2.credit(creditAmount);
// View
displayAccount(account1);
displayAccount(account2);
// 2 -debit
System.out.print("Enterdebital amount for account2: ");
debitalAmount = input.nextDouble();
System.out.printf("%nwithdrawing %.2f from account2 balance%n%n",
debitalAmount);
account2.withdraw(withdrawalAmount);
// View
displayAccount(account1);
displayAccount(account2);
}
public static void displayAccount(Account acc) {
System.out.printf("%s balance: $%.2f%n",
acc.getName(), acc.getBalance());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.