Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. public static int writeBudgetData(String inputFileName, String outputFileName) throws IOException
  2. {
  3. int i;
  4. int budgetAmount;
  5. int totalAmount;
  6. int categoryAmount;
  7. int budgetDifference;
  8.  
  9. String month;
  10.  
  11. File inputFile = new File(inputFileName);
  12. Scanner budgetDataInput = new Scanner(inputFileName);
  13.  
  14. if (!inputFile.exists())
  15. {
  16. budgetDataInput.close();
  17. return 1;
  18. }
  19. else
  20. {
  21. while(budgetDataInput.hasNext())
  22. {
  23. PrintWriter outputFile = new PrintWriter(outputFileName);
  24. totalAmount = 0;
  25.  
  26. month = budgetDataInput.next();
  27. budgetAmount = budgetDataInput.nextInt();
  28.  
  29. for ( i = 0; i < 6; i++)
  30. {
  31. categoryAmount = budgetDataInput.nextInt();
  32. totalAmount += categoryAmount;
  33. }
  34.  
  35. outputFile.println("Total budgeted for " + month + ": $" + budgetAmount);
  36. outputFile.println("Total spent for " + month + ": $" + totalAmount);
  37.  
  38. budgetDifference = budgetAmount-totalAmount;
  39. budgetDifference = Math.abs(budgetDifference);
  40.  
  41. if (budgetAmount == totalAmount)
  42. {
  43. outputFile.println("You were precisely on budget for " + month + "\r\n");
  44. }
  45. else if (budgetAmount > totalAmount)
  46. {
  47. outputFile.println("You were under your budget for " + month + " by $" + budgetDifference + "\r\n");
  48. }
  49. else if (budgetAmount < totalAmount)
  50. {
  51. outputFile.println("You went over your budget for " + month + " by $" + budgetDifference + "\r\n");
  52. }
  53.  
  54. budgetDataInput.close();
  55. outputFile.close();
  56. }
  57. }
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement