Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int writeBudgetData(String inputFileName, String outputFileName) throws IOException
- {
- int i;
- int budgetAmount;
- int totalAmount;
- int categoryAmount;
- int budgetDifference;
- String month;
- File inputFile = new File(inputFileName);
- Scanner budgetDataInput = new Scanner(inputFileName);
- if (!inputFile.exists())
- {
- budgetDataInput.close();
- return 1;
- }
- else
- {
- while(budgetDataInput.hasNext())
- {
- PrintWriter outputFile = new PrintWriter(outputFileName);
- totalAmount = 0;
- month = budgetDataInput.next();
- budgetAmount = budgetDataInput.nextInt();
- for ( i = 0; i < 6; i++)
- {
- categoryAmount = budgetDataInput.nextInt();
- totalAmount += categoryAmount;
- }
- outputFile.println("Total budgeted for " + month + ": $" + budgetAmount);
- outputFile.println("Total spent for " + month + ": $" + totalAmount);
- budgetDifference = budgetAmount-totalAmount;
- budgetDifference = Math.abs(budgetDifference);
- if (budgetAmount == totalAmount)
- {
- outputFile.println("You were precisely on budget for " + month + "\r\n");
- }
- else if (budgetAmount > totalAmount)
- {
- outputFile.println("You were under your budget for " + month + " by $" + budgetDifference + "\r\n");
- }
- else if (budgetAmount < totalAmount)
- {
- outputFile.println("You went over your budget for " + month + " by $" + budgetDifference + "\r\n");
- }
- budgetDataInput.close();
- outputFile.close();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement