In: Computer Science
Using Java
Summary
Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report.
Project Description
You’ll read and write files containing objects of the Loan class. Here are the details of that class:
Instance Variables: | customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) |
Methods:
The setters for the annual interest percentage, number of years, and loan amount invoke both “calculate…” methods before they exit
The “calculate…” methods compute the named value, then store that amount in the associated instance variable. They are private (helper) methods.
Constructors:
Calculations:
monthly interest rate = annual interest percentage / 1200 monthly
payment = (loan amount * monthly interest rate)
/ total loan payments = monthly payment * number of years * 12 |
* Note: Round all dollar amounts to 2 decimal places
Here is what you should do in main
Use this data to create five Loan objects
Annual Interest Loan Customer Name Percentage Years Amount Loan Date Bob Smith 6.5% 30 318,000 Sep 1, 2015 Alicia Herman 4.2% 15 248,000 Oct 15, 2013 Julie Franciosa 8.5% 10 30,000 Apr 14, 2010 Julio Quiros 15.0% 3 50,000 June 23, 2017 Frank Larsen 8.9% 5 23,000 Mar 8, 2016 |
Use this algorithm:
Note: After writing the first three Loan objects to a file (in step 2), close the output stream, then re-open the output stream before step 4 appending the last two Loan objects to the existing file
An example of the
output from your program
Annual Monthly Total Customer Name Prcnt Yrs Loan-Amount Payment Loan Payments Loan-Date ---------------- ----- --- ----------- --------- ------------ ------------- Bob Smith 6.5 30 318,000.00 2,009.98 723,592.80 Sep 1, 2015 Alicia Herman 4.2 15 248,000.00 1,859.38 334,688.40 Oct 15, 2013 Julie Franciosa 8.5 10 30,000.00 446.35 53,562.00 Apr 14, 2010 Julio Quiros 15.0 3 50,000.00 1,660.72 59,785.92 June 23, 2017 Frank Larsen 8.9 5 23,000.00 476.33 28.579.80 Mar 8, 2016 =========== ========= ============ 669,000.00 6,452.76 1,200,208.92 |
//Java code
import java.text.NumberFormat; public class Loan { private String customerName; private double annualInterest; private int numberOfYears; private double loanAmount; private String loanDate; private double monthlyPayment; private double totalPayments; //getters public String getCustomerName() { return customerName; } public double getAnnualInterest() { return annualInterest; } public int getNumberOfYears() { return numberOfYears; } public double getLoanAmount() { return loanAmount; } public String getLoanDate() { return loanDate; } public double getMonthlyPayment() { return monthlyPayment; } public double getTotalPayments() { return totalPayments; } //setters public void setCustomerName(String customerName) { this.customerName = customerName; } public void setAnnualInterest(double annualInterest) { this.annualInterest = annualInterest; calculateMonthlyPayment(); calculateTotalPayments(); } public void setNumberOfYears(int numberOfYears) { this.numberOfYears = numberOfYears; calculateMonthlyPayment(); calculateTotalPayments(); } public void setLoanAmount(double loanAmount) { this.loanAmount = loanAmount; calculateMonthlyPayment(); calculateTotalPayments(); } public void setLoanDate(String loanDate) { this.loanDate = loanDate; } private void calculateMonthlyPayment() { double monthlyInterestRate = annualInterest/1200.0; monthlyPayment = (loanAmount*monthlyInterestRate)/(1-Math.pow((1+monthlyInterestRate),-(numberOfYears*12))); } private void calculateTotalPayments() { this.totalPayments = this.numberOfYears*this.monthlyPayment*12.0; } ///no args constructor /*** * a “no-arg” constructor sets the customer name to a default value, * the loan date to “no date”, and * all numeric variables to zero. This method invokes the “full” constructor */ public Loan() { this("",0,0,0,"no date"); } //full constructor /** * a “full” constructor takes the customer name, annual * interest percentage, number of years, loan amount, * and loan date. It invokes the appropriate setters, * @param customerName * @param annualInterest * @param numberOfYears * @param loanAmount * @param loanDate */ public Loan(String customerName, double annualInterest, int numberOfYears, double loanAmount, String loanDate) { setAnnualInterest(annualInterest); setCustomerName(customerName); setLoanAmount(loanAmount); setLoanDate(loanDate); setNumberOfYears(numberOfYears); } public void display() { System.out.println(String.format("%16s %5s %6s %15s %17s %17s %17s",customerName,String.format("%.2f",annualInterest),numberOfYears, NumberFormat.getCurrencyInstance().format(loanAmount),NumberFormat.getCurrencyInstance().format(monthlyPayment),NumberFormat.getCurrencyInstance().format(totalPayments),loanDate)); } @Override public String toString() { return customerName+" "+annualInterest+" "+numberOfYears+" "+loanAmount+" "+loanDate; } }
//==================================
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.NumberFormat; import java.util.Scanner; public class LoanTest { public static void main(String[] args) throws IOException { Loan loan1 = new Loan("Bob Smith",6.5,30,318000,"Sep 1, 2015"); Loan loan2 = new Loan("Alicia Herman",4.2,15,248000,"Oct 15,2013"); Loan loan3 = new Loan("Julie Franciosca",8.5,10,30000,"Apr 14,2010"); PrintWriter printWriter = new PrintWriter(new FileWriter("loan.txt")); Loan loan4 = new Loan(); loan4.setCustomerName("Julio Quiros"); loan4.setNumberOfYears(3); loan4.setLoanAmount(50000); loan4.setAnnualInterest(15); loan4.setLoanDate("June 23, 2017"); //============================ Loan loan5 = new Loan(); loan5.setCustomerName("Frank Larsen"); loan5.setNumberOfYears(5); loan5.setLoanAmount(23000); loan5.setAnnualInterest(8.9); loan5.setLoanDate("Mar 8, 2016"); //write data to file printWriter.println(loan1); printWriter.println(loan2); printWriter.println(loan3); printWriter.println(loan4); printWriter.println(loan5); printWriter.close(); double totalLoanAmount =0,totalLoanPayment=0,totalMonthlyPayment=0; //header System.out.println(String.format("%16s %5s %6s %15s %15s %15s %15s","Customer Name","Percent","Years","Loan Amount","Payment","Loan Payments","Loan Date")); System.out.println(String.format("%16s %5s %4s %15s %15s %15s %15s","------------","---------","-----","-----------","-------","-------------","---------")); //Open stream to read Scanner scanFile = new Scanner(new File("loan.txt")); while (scanFile.hasNextLine()) { String line = scanFile.nextLine(); Scanner words = new Scanner(line); //Create loan Loan loan = new Loan(words.next()+" "+words.next(),words.nextDouble(),words.nextInt(),words.nextDouble(),words.nextLine()); totalLoanAmount+=loan.getLoanAmount(); totalLoanPayment+=loan.getTotalPayments(); totalMonthlyPayment+=loan.getMonthlyPayment(); loan.display(); } System.out.println(String.format("%16s %5s %6s %15s %17s %17s %15s","","","","============","=============","============","")); System.out.println(String.format("%16s %5s %6s %15s %17s %17s %15s","","","", NumberFormat.getCurrencyInstance().format(totalLoanAmount),NumberFormat.getCurrencyInstance().format(totalMonthlyPayment),NumberFormat.getCurrencyInstance().format(totalLoanPayment),"")); scanFile.close(); } }
//loan.txt
//Output
//If you need any help regarding this solution .......... please leave a comment ...... thanks