Guest User

Untitled

a guest
Dec 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4. 11. Deposit and Withdrawal Files
  5.  
  6. Use Notepad or another text editor to create a text file named Deposits.txt. The file should
  7. contain the following numbers, one per line:
  8.  
  9. 100.00
  10. 125.00
  11. 78.92
  12. 37.55
  13.  
  14. Next, create a text file named Withdrawals.txt. The file should contain the following numbers,
  15. one per line:
  16.  
  17. 29.88
  18. 110.00
  19. 27.52
  20. 50.00
  21. 12.90
  22.  
  23. The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings
  24. account during the month, and the numbers in the Withdrawals.txt file are the amounts
  25. of withdrawals that were made during the month. Write a program that creates an instance
  26. of the SavingsAccount class that you wrote in Programming Challenge 10. The starting balance
  27. for the object is 500.00. The program should read the values from the Deposits.txt file
  28. and use the object’s method to add them to the account balance. The program should read
  29. the values from the Withdrawals.txt file and use the object’s method to subtract them from
  30. the account balance. The program should call the class method to calculate the monthly
  31. interest, and then display the ending balance and the total interest earned.
  32. */
  33.  
  34. import java.io.*;
  35. import java.util.Scanner;
  36.  
  37. public class Main {
  38.  
  39. public static void main(String[] args) throws FileNotFoundException {
  40.  
  41. double startingBalance;
  42. double annualInterestRate;
  43. double deposit;
  44. double withdraw;
  45. double totalDeposits = 0;
  46. double totalWithdraws = 0;
  47. double totalInterest = 0;
  48. double months = 0;
  49.  
  50. Scanner scanner = new Scanner( System.in );
  51.  
  52. System.out.print( "Starting balance :" );
  53. startingBalance = scanner.nextDouble();
  54.  
  55. com.company.SavingsAccount account1 = new com.company.SavingsAccount( startingBalance );
  56. account1.setAnnualInterestRate( .1 ); // .1 is 10%
  57. double totalInterestEarned = 0;
  58.  
  59.  
  60. File depositFile = new File ( "Deposits.txt");
  61. File withdrawlsFile = new File ( "Withdrawls.txt");
  62.  
  63.  
  64. Scanner inputFile = new Scanner ( depositFile );
  65.  
  66. while( inputFile.hasNext() ) {
  67. account1.deposit( inputFile.nextDouble() );
  68. totalDeposits += account1.deposit( inputFile.nextDouble() );
  69. }
  70. inputFile.close();
  71.  
  72.  
  73. inputFile = new Scanner ( withdrawlsFile );
  74.  
  75. while( inputFile.hasNext() ) {
  76. account1.withdraw( inputFile.nextDouble() );
  77. //account1.withdraw( totalWithdraws );
  78. totalWithdraws += account1.withdraw( inputFile.nextDouble() );
  79. }
  80.  
  81. inputFile.close();
  82.  
  83. /*
  84. inputFile = new Scanner ( withdrawlsFile );
  85.  
  86. while( inputFile.hasNext() ) {
  87. account1.withdraw( inputFile.nextDouble() );
  88. account1.withdraw( totalWithdraws );
  89. }
  90.  
  91. inputFile.close();
  92. */
  93.  
  94. account1.setAnnualInterestRate( .10 );
  95. totalInterestEarned = account1.monthlyInterest();
  96.  
  97. System.out.printf( "Total deposits: $%,.2f\nTotal withdraws: $%,.2f\nEnding balance: $%,.2f \nTotal interest earned: $%,.2f\n",
  98. totalDeposits, totalWithdraws, account1.getBalance(), account1.monthlyInterest() );
  99.  
  100.  
  101. System.out.println( totalWithdraws );
  102.  
  103. /*
  104. for ( int month = 1; month <= months; month ++ ){
  105. System.out.print( "Deposit in month " + month + ":" );
  106. deposit = scanner.nextDouble();
  107. totalDeposits += deposit;
  108.  
  109. account1.deposit( deposit );
  110.  
  111. System.out.print( "Withdraw in month " + month + ":" );
  112. withdraw = scanner.nextDouble();
  113. totalWithdraws += withdraw;
  114.  
  115. account1.withdraw( withdraw );
  116.  
  117. account1.monthlyInterest();
  118. totalInterest += account1.monthlyInterest();
  119. }
  120. */
  121.  
  122. }
  123. }
Add Comment
Please, Sign In to add comment