Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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);

bw.write("Expenses for " + fileName + " recorded " + new Date());

bw.newLine(); // need to do this after EVERY line written to log file!

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 ");

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 = 0;

if ((expenseAmountAsString.length() - decimalPointOffset) != 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();

// 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();

}

}

}

JAVA LANGUAGE PROGRAM ABOVE IS TO CREATE AN EXPENSEACCOUNT:

I'm having issues adding the following code to verify that the user input amount has a decimal point in it:

int decimalPointOffset = stringNumber.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 }

stringNumber is not declared and even when done and initialized, it still gives an error. I've tried several ways to fix it and continues to give an error on the program.

Can you please provide a solution?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions