import java.io.File; import java.io.IOException; import java.io.PrintStream; imp
ID: 3701600 • Letter: I
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 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
Explanation / Answer
Please find the answers to the above questions and Full java implementation below :
Question #2 :Describe the possible filenames generated by GenerateMonthlySpending. What is the meaning of the Filename?
Answer : Following are the file names generated. file names are generated for each month per year. for example a file with name "1-2016.txt" represents the spending data for January 2016.
1-2016.txt
2-2013.txt
2-2014.txt
2-2017.txt
3-2013.txt
3-2014.txt
3-2015.txt
3-2016.txt
4-2013.txt
4-2016.txt
4-2017.txt
5-2016.txt
6-2017.txt
7-2016.txt
8-2013.txt
8-2014.txt
8-2016.txt
9-2013.txt
9-2015.txt
9-2016.txt
9-2017.txt
10-2014.txt
10-2015.txt
10-2016.txt
10-2017.txt
11-2013.txt
11-2017.txt
12-2014.txt
12-2015.txt
12-2016.txt
Question #3: Describe the format of the files generated by GenerateMonthlySpending. Explain what each row must look like. What is the meaning of the + and - symbols?
Answer : File Format - Files are .txt file and it has comma separated values. Each row represent the spending/earning for a day of month. It has three columns (for e.g., 1, Rent, -$550.00) -
Symbols :
+ means money In
- means money out
JAVA Program :
===========
OUTPUT :
========
Following Files would be generated after running the above program. Make sure you run the
2013summary.txt
2014summary.txt
2015summary.txt
2016summary.txt
2017summary.txt
File content would be different for each and every run.
SAMPLE Content of the above summary files :
-==================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.