Build a class called BankAccount that manages checking andsavings accounts. The class has three private member fields: acustomer name (String), the customer's savings account balance(double), and the customer's checking account balance (double).
Implement the following Constructor and instance methods:
public class BankAccount { // TODO: Build BankAccount class with methods listedabove /* Type your code here. */
// main public static void main(String args[]) { BankAccount account = newBankAccount("Mickey", 500.00, 1000.00); account.setChecking(500); account.setSavings(500); account.withdrawSavings(100); account.withdrawChecking(100); account.transferToSavings(300);
System.out.println(account.getName()); // Expected Mickey System.out.printf("$%.2f\n",account.getChecking()); // Expected 100.0 System.out.printf("$%.2f\n",account.getSavings()); // Expected 700.0 } }
This is the code that I used and got an 8/10. Pleaselet me know the error, thank you!
public class BankAccount { // TODO: Build BankAccount class with methods listedabove private String name; private double savings; private double checking; public BankAccount(String newName, double amt1, doubleamt2) { name = newName; savings = amt1; checking = amt2; } public void setName(String newName){ this.name = newName; } public String getName() { return name; } public void setChecking(double amt){ this.checking = amt; } public double getChecking(){ return checking; } public void setSavings(double amt){ this.savings = amt; } public double getSavings(){ return savings; } public void depositChecking(double amt) { if (amt > 0) this.checking = this.checking + amt; } public void depositSavings(double amt){ if(amt>0) this.savings = this.savings + amt; } public void withdrawChecking(double amt) { if(amt>0) this.checking = this.checking - amt; } public void withdrawSavings(double amt){ if (amt > 0) this.savings = this.savings -amt; } public void transferToSavings(double amt) { if (amt > 0) { withdrawChecking(amt); depositSavings(amt); }} // main public static void main(String args[]) { BankAccount account = newBankAccount("Mickey", 500.00, 1000.00); account.setChecking(500); account.setSavings(500); account.withdrawSavings(100); account.withdrawChecking(100); account.transferToSavings(300);
System.out.println(account.getName()); // Expected Mickey System.out.printf("$%.2f\n",account.getChecking()); // Expected 100.0 System.out.printf("$%.2f\n",account.getSavings()); // Expected 700.0 } }
Total score: 8 / 10
Only show failing tests
Download this submission
1: Unit testkeyboard_arrow_up
0 / 2 **** ERROR ON THIS PART****
Tests that BankAccount("Jane",100.00, 500.00) correctlyinitializes bank account. TestsbankAccount.getName().equals("Jane"), bankAccount.getChecking() ==100.0, and bankAccount.getSavings() == 500.0
Test feedback
getName() correctly returned Jane getChecking() incorrectlyreturned 500.0
2: Unit testkeyboard_arrow_up
2 / 2
Tests that BankAccount("Jane", 500.00, 1000.00) correctlyinitializes bank account. Tests bankAccount.depositSavings(123.00),bankAccount.getSavings() == 1123.00,bankAccount.withdrawSavings(25.00), and bankAccount.getSavings() ==1098.00.
Test feedback
depositSavings(123.00) passed: new balance is 623.0withdrawSavings(25.00) passed: new balance is 598.0
3: Unit testkeyboard_arrow_up
2 / 2
Tests that BankAccount("Jane", 750.00, 450.00) correctlyinitializes bank account. Tests bankAccount.depositChecking(75.00),bankAccount.getChecking() == 825.00,bankAccount.withdrawChecking(45.00), and bankAccount.getChecking()== 780.00.
Test feedback
depositChecking(75.00) passed: balance is 525.0withdrawChecking(45.00) passed: balance is 480.0
4: Unit testkeyboard_arrow_up
2 / 2
Tests that BankAccount("Maria", 100.00, 100.00) correctlyinitializes bank account. TestsbankAccount.depositChecking(-5.00)/bankAccount.withdrawChecking(-5.00),bankAccount.getChecking() == 100.00,bankAccount.depositSavings(-5.00)/bankAccount.withdrawSavings(-5.00);,and bankAccount.getSavings() == 100.00
Test feedback
depositChecking(-5.00) passed: negative deposits did not change100.00 checking balance withdrawChecking(-5.00) passed: negativewithdraw did not change 100.00 checking balancedepositSaving(-5.00) passed: negative deposits did not change100.00 savings balance withdrawSavings(-5.00) passed: negativewithdraw did not change 100.00 savings balance
5: Unit testkeyboard_arrow_up
2 / 2
Tests that BankAccount("James", 1000.00, 1000.00) correctlyinitializes bank account. TestsbankAccount.transferToSavings(100.00), and bankAccount.getSavings()== 1100.00, and bankAccount.getChecking == 900.00
Test feedback
transferToSavings(100.00) passed: savings is $1100.00transferToSavings(100.00) passed: checking is $900.00
Build a class called BankAccount that manages checking and savings accounts. The class has three private member fields:
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am