Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: This is for java only Hotel Expense Statement A salesperson enters client

ID: 3779924 • Letter: N

Question

Note: This is for java only

Hotel Expense Statement

A salesperson enters client hotel expenses in a text file. Each line contains the following, separated by semicolons: client name, service sold (i.e., Dinner, Conference, Lodging, etc.), the sales amount, and the date.

Your program should first query the user for the name of the input file and read it in. Display an error if the input file is not found (does not exist).

Then the program should read the file (line by line), keep a running tally of the total amount for each kind of service, and then display the total amount for each service category.

In addition to displaying totals on the screen, the totals should also be written to an output file.

Attached is an example input file that your program will be tested with, so you will need to make sure that you program will run correctly using this file. Since this may be your first experience reading from an input file, you will likely find it easiest if you store the input file in the same folder with your Java program file so that they can easily communicate with one another.

One feature of the input file, is that it uses a semicolon (;) to delimit the tokens on each line of input, rather than whitespace. Delimiters of this type are not covered in the text. So what you will need to do is to use a delimiter statement after you construct your line scanner object.  So for example, if you create an object called lineScan of type Scanner to process tokens on a given line of input, then you could call the useDelimiter method on your lineScan object, as follows:

lineScan.useDelimiter(";");

This will allow you to tokenize each input line based, not on white space delimiters, but using the semicolon as a delimiter instead.

The input text file looks like:-

Jason Inouye;Conference;250.00;11/10/2016
Jason Inouye;Lodging;78.95;11/10/2016
Mary Ryan;Dinner;16.95;11/10/2016
Mark Twain;Dinner;25.50;11/10/2016
Mark Twain;Spa;50.00;11/10/2016
Steven Hawking;Conference;250.00;11/10/2016
Steven Hawking;Room Service;45.00;11/11/2016
Steven Hawking;Lodging;78.95;11/11/2016
Ayrton Senna;Room Service;23.20;11/10/2016
Ayton Senna;Dinner;22.50;11/10/2016
Ayton Senna;Lodging;78.95;11/10/2016

Explanation / Answer

the code as per your requirement as sjhown below

import java.util.*;
import java.io.*;
public class Sales
{
public static void main(String[] args) throws FileNotFoundException
{
File inputFile = new File("input.txt");
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter("output.txt");
double dinnerTotal = 0;
double conferenceTotal = 0;
double lodgingTotal = 0;
while (in.hasNext())
{
String line = in.nextLine();
String[] parts = line.split(";");
if(parts[1].equals("Conference")) {
conferenceTotal += Double.parseDouble(parts[2]);
} else if(parts[1].equals("Dinner")) {
dinnerTotal += Double.parseDouble(parts[2]);
} else if(parts[1].equals("Lodging")) {
lodgingTotal += Double.parseDouble(parts[2]);
}
}
in.close();
out.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote