Question
import java.io.BufferedWriter; import java.io.FileWriter; import java.text.DecimalFormat; import java.util.Date; import java.util.Scanner; public class ExpenseAccount { public static void main(String[] args) throws Exception { // TODO Auto-generated
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Scanner;
public class ExpenseAccount {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("NAME 2023");
System.out.println("Welcome to the ExpenseAccount program! Run this program to accumulate the expenses for a given activity, like");
System.out.println("the business trip to L.A. Then run the program again to accumulate the expenses for the business trip to New York.");
System.out.println("The expenses for each trip/activity are kept in a separate text disk file that you will name.");
System.out.println("Each expense item is entered on a separate line. e.g. $12.47 for Tuesday lunch");
System.out.println("A leading $ sign is optional, then expense amount, then ' for ' (no quotes) and then a brief description.");
System.out.println("An expense amount must contain a decimal point followed by two decimal digits.");
System.out.println("Each entered expense is added to the accumulating total expenses amount.");
System.out.println("Enter EXIT to end the program and to show the total expenses and to write the log file on the disk.");
System.out.println("");
Scanner scanner = new Scanner(System.in);
DecimalFormat df = new DecimalFormat(("0.00"));
double totalExpenses = 0;
double expenseAmount = 0;
String expenseAmountAsString = null;
String expenseDescription = null;
String expenseEntryLine = null;
String fileName;
while(true) // let user specify a unique file name for this activity's expenses
{
System.out.println("To begin, enter the name of the disk text file (no blanks, periods or slashes) to hold the expenses for this activity. (e.g. TripToLA2023)");
fileName = scanner.nextLine().trim(); // wait for user entry
if (fileName.equalsIgnoreCase("EXIT")) return;
if ((fileName.length() != 0) && !fileName.contains(".") && !fileName.contains(" ") && !fileName.contains("\\") && !fileName.contains(" /"))
break;
}
FileWriter fw = new FileWriter(fileName + ".txt");
BufferedWriter bw = new BufferedWriter(fw);
while(true) // "do forever" // main program loop
{
System.out.println("");
System.out.println("Enter an expense line or EXIT.");
expenseEntryLine = scanner.nextLine().trim(); // remove blanks
if (expenseEntryLine.equalsIgnoreCase("EXIT")) break; // exit loop
int forOffset = expenseEntryLine.indexOf(" for ");
bw.write("Expenses for " + fileName + " recorded " + new Date());
bw.newLine(); // need to do this after EVERY line written to log file!
bw.flush();
if (forOffset < 0) // no find the word for with a space on each side.
{
System.out.println("The lower-case word ' for ' (surrounded by blanks) must be in the expense entry line "
+ "following the expense amount and prior to the expense description.");
continue; // back to the top of the loop to re-enter the expense line.
}
expenseAmountAsString = expenseEntryLine.substring(0,forOffset).trim(); // from,to (non-inclusive). trim() drops leading/trailing blanks
expenseDescription = expenseEntryLine.substring(forOffset+4).trim(); // from here to end of String. trim() drops leading/trailing blanks
if ((expenseAmountAsString.length() == 0) || (expenseDescription.length() == 0))
{
System.out.println("The entered expense line must contain both an expense amount prior to ' for '"
+ "and an expense description following ' for '.");
continue;
}
if (expenseAmountAsString.startsWith("$"))
expenseAmountAsString = expenseAmountAsString.substring(1).trim(); // also remove any blanks after $
try {
expenseAmount = Double.parseDouble(expenseAmountAsString);
// If we're still executing here, the amount is a valid double number.
if (expenseAmount < 0) // good a time as any to check for this.
{
System.out.println("Expense anount cannot be negative.");
continue;
}
// but we WON'T add this amount to totalExpenses until we have done more checking.
// Execution continues FOLLOWING the catch block if we are still running here.
}
catch(NumberFormatException nfe) // String didn't convert to a double.
{
System.out.println("Expense amount is not numeric.");
continue; // back to top of loop
}
int decimalPointOffset = expenseAmountAsString.indexOf("."); // get offset (base 0) of decimal point in this String?
if (decimalPointOffset < 0) // (no decimal point anywhere)
{
System.out.println("Entered number must contain a decimal point.");
continue; // go to top of loop to start over
}
int decimalPointOffset1 = 0;
if ((expenseAmountAsString.length() - decimalPointOffset1) != 3)
{
System.out.println("Entered number must contain 2 decimal digits following the decimal point.");
continue;
}
totalExpenses += expenseAmount; // finally! add to total
System.out.println("total expenses so far = $" + df.format(totalExpenses)); // force 2 decimal digits
// Add this line entry to the log file
bw.write(expenseEntryLine);
bw.newLine();
bw.flush();
}
// We got here (out of the 2nd while(true) loop) by the user entering EXIT instead of an expense
// line at the top of the 2nd while(true) loop. It is absolutely required that we close() that output
// log file to get it written on the disk.
scanner.close();
// close output log file
bw.write("Total Expenses for " + fileName + " were $" + df.format(totalExpenses));
bw.newLine();
bw.close();
fw.close();
}
}
JAVA Program: ExpenseAccount
I' m having the following issues with the program:
1. When the user enters EXIT the correct total of entered expense amounts is not printed on the console.
2. Log file does not contain any of the expenses
3. Log file does not contain the correct total of the expenses on last line
How can I fix these errors?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started