programming in JAVA GoBank has been asked to go back and reprocess the loan paym
ID: 3698996 • Letter: P
Question
programming in JAVA GoBank has been asked to go back and reprocess the loan payments for a car loan of $18,875. The car loan is a 0% interest loan. The loan is for 60 months. The customer does not believe the bank has the correct outstanding balance for her loan ($12,500). The customer has copies of 20 payments made to the bank. A clerk makes a copy of the payments and enters them into a file. To verify the banks total, read the file and write a program using a CarLoan class that extends the BankAccount class (found on page 671 - 672 in the textbook). The CarLoan class has the following features. 1) An overload constructor that initializes the car loan balance. (Hint: Uses super to set the balance.) Create a CarLoanClient class that Create CarLoan object (see CheckingAccountClient line 9 on page 667). Read the file one payment at a time (Include exception handling code see example on page 755). Use the payment read from the file to pass the information to method to calculate the outstanding balance. Hint: Use the withdrawal method. Printout the payment number, payment amount and outstanding balance. Continue to process for each payment until the end of the file.
Create a text file with the following numbers. Monthly Payment 315 451 315 374 353 315 450 484 412 315 495 476 321 315 406 329 326 356 315 325
Explanation / Answer
Please find my implementation:
//Given BankAccount class
// BankAccount.java
import java.text.DecimalFormat;
public class BankAccount {
public final DecimalFormat MONEY = new DecimalFormat("$#,##0.00");
protected double balance;
public BankAccount() {
balance = 0.0;
}
public BankAccount(double startBalance) {
deposit(startBalance);
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount >= 0.0) {
balance += amount;
} else {
System.out.println("Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount >= 0.0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Withdrawal amount must be positive." + "and cannot be greater than balance");
}
}
public String toString() {
return ("Balance is " + MONEY.format(balance));
}
}
------------------------------------------------------------------------------------------------------------
payment.txt
315
451
315
374
353
315
450
484
412
315
495
476
321
315
406
329
326
356
315
325
------------------------------------------------------------------------------------------
CarLoan.java
public class CarLoan extends BankAccount {
public CarLoan(double startBalance) {
super(startBalance);
}
}
-----------------------------------------------------------------------------------------------------------------
CarLoanClient.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CarLoanClient {
public static void main(String args[]) {
CarLoan object = new CarLoan(18875);
Scanner in = null;
try {
in = new Scanner(new File("payment.txt"));
} catch (FileNotFoundException e) {
System.out.println("Cannot open file");
}
int paymentNum = 1;
while (in.hasNext()) {
double next = in.nextDouble();
object.withdraw(next);
System.out.println("Payment number: " + paymentNum++ + ", amount: " + next + ", Outstanding balance: " + object.balance);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.