Question: import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; public class GenerateMonthlySpending { public static void main(String[] paramArrayOfString) throws IOException { String[] arrayOfString1 = { Roots

import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter;

public class GenerateMonthlySpending { public static void main(String[] paramArrayOfString) throws IOException { String[] arrayOfString1 = { "Roots Cafe", "Tim Hortons", "Safeway", "Skytrain", "Triple Os", "Ten Ren Bubble Tea", "H&M", "Muji", "Chevron", "7-11", "All India Sweets", "Rexall", "Fresh Slice" }; String[] arrayOfString2 = { "Lottery", "Craigslist", "Grandma", "Grandpa", "Transfer from roommates" }; String str1 = "Work Monthly Pay"; String str2 = "Rent"; String str3 = "Utilities"; String str4 = "Finances"; File localFile1 = new File(str4); if (!localFile1.mkdir()) { System.out.println("Sorry, the directory could not be created! Maybe you already have a directory called " + str4 + "."); System.exit(1); } for (int m = 0; m < 30; m++) { int k = getRandomInt(2013, 2017); int j = getRandomInt(1, 12); String str5 = j + "-" + k + ".txt"; File localFile2 = new File(str4 + File.separator + str5); if (!localFile2.exists()) { PrintWriter localPrintWriter = new PrintWriter(localFile2); localPrintWriter.println("Day, Item, Cost"); int i = 1; printRow(localPrintWriter, i, true, str2, 550.0D); printRow(localPrintWriter, i, true, str3, 30.0D); printRow(localPrintWriter, i, false, str1, getRandomCost(1000.0D, 2000.0D)); while (i < 29) { printRandomRow(localPrintWriter, i, arrayOfString1, arrayOfString2); i += getRandomInt(0, 4); } localPrintWriter.close(); } else { m--; } } System.out.println("Spending files generated in the " + str4 + " directory."); } public static void printRandomRow(PrintWriter paramPrintWriter, int paramInt, String[] paramArrayOfString1, String[] paramArrayOfString2) { if (Math.random() < 0.9D) { printRow(paramPrintWriter, paramInt, true, getRandomElement(paramArrayOfString1), getRandomCost(2.0D, 40.0D)); } else { printRow(paramPrintWriter, paramInt, false, getRandomElement(paramArrayOfString2), getRandomCost(10.0D, 100.0D)); } } public static String getRandomElement(String[] paramArrayOfString) { return paramArrayOfString[getRandomInt(0, paramArrayOfString.length - 1)]; } public static void printRow(PrintWriter paramPrintWriter, int paramInt, boolean paramBoolean, String paramString, double paramDouble) { char c = '+'; if (paramBoolean) { c = '-'; } paramPrintWriter.printf("%d, %s, %c$%.2f ", new Object[] { Integer.valueOf(paramInt), paramString, Character.valueOf(c), Double.valueOf(paramDouble) }); } public static double getRandomCost(double paramDouble1, double paramDouble2) { return paramDouble1 + Math.random() * (paramDouble2 - paramDouble1); } public static int getRandomInt(int paramInt1, int paramInt2) { return (int)(paramInt1 + Math.random() * (paramInt2 - paramInt1 + 1)); } }

1. Run the GenerateMonthlySpending program given above from the command line. Look in your current directory and check for a new directory called Finances. Open the directory and look at the ?les inside. Pay attention to their ?lenames. Open some of the ?les and look at the contents. 2. Describe the possible ?lenames generated by GenerateMonthlySpending. What is the meaning of the ?lename? 3. Describe the format of the ?les generated by GenerateMonthlySpending. Explain what each row must look like. What is the meaning of the + and - symbols? 4. Read through the assignment. Speci?cations Write a program called SummarizeFinances. Your program should go through all the ?les in the Finances directory, and generate a summary of the activity for each year. You need to generate a separate text ?le for each year. The name of each ?le generated should be yyyy summary.txt, where yyyy is replaced by the actual year. Each generated ?le must have two columns. The columns should be aligned nicely, as a table. The ?rst column will contain the names of all the months that had associated data. The second column will contain the net earnings for that month (or the net spending, if the amount is negative). write a java program.

Sample Output One of the generated ?les could be 2013 summary.txt, and may contain the following. (Your actual values will be di?erent, as the Finances directory is randomly generated.) Month Net Earning/Spending

===================================

January +$145.34

March +$203.41

June -$79.12

September -$12.45

October +$84.58

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!