Question

in Java and is for intro level class A hotel salesperson enters sales in a text...

in Java and is for intro level class

A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Display an error if the file does not exist or the format is incorrect.

Now that you have completed the above, modify as follows:

Write a program that reads a text file as described above, and that writes a separate file for each service category, containing the entries for that category (this is not the total, but each entry). Name the output files Dinner.txt, Conference.txt, and so on.

input Text(hotelSale.txt)

Gallman;Dinner;1900;10/23/2019
Tony;Lodging;200;02/02/2012
Steve;Conference;100;12/25/2016
Tony;Conference;500;01/01/2017
Banner;Dinner;1900;07/04/2006

output Text(Dining.txt, Lodging.txt, Conference.txt)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program:

/* Java Program that reads data from hotel sales and writes outputs to respective files */

import java.io.*;
import java.util.*;

class HotelSalesPerson
{
   //Main method
public static void main(String[] args) throws FileNotFoundException
   {
       //Scanner class object for reading from file
       Scanner reader = new Scanner(new File("hotelSale.txt"));
  
       //Writer class objects for writing data
       PrintWriter diningOut = new PrintWriter("Dining.txt");
       PrintWriter lodgingOut = new PrintWriter("Lodging.txt");
       PrintWriter conferenceOut = new PrintWriter("Conference.txt");
      
       double dinnerTotal = 0, conferenceTotal = 0, lodgingTotal = 0;
      
       //Iterate entire file is processed
       while (reader.hasNext())
       {
           //Reading a line
           String line = reader.nextLine();
          
           //Splitting line on semicolon
           String[] cols = line.split(";");
          
           //Conference Type
           if(cols[1].equals("Conference"))
           {
               conferenceTotal += Double.parseDouble(cols[2]);
               conferenceOut.printf("%.2f ", Double.parseDouble(cols[2]));
           }
          
           //Lodging Type
           else if(cols[1].equals("Lodging"))
           {
               lodgingTotal += Double.parseDouble(cols[2]);
               lodgingOut.printf("%.2f ", Double.parseDouble(cols[2]));
           }
          
           //Dinner Type
           else if(cols[1].equals("Dinner"))
           {
               dinnerTotal += Double.parseDouble(cols[2]);
               diningOut.printf("%.2f ", Double.parseDouble(cols[2]));
           }
       }
      
       //Closing reader
       reader.close();
      
       //Writing output to respective files
       System.out.printf("\n Dinner Total Sale: $%.2f \n", dinnerTotal);
       System.out.printf(" Conference Total Sale: $%.2f \n", conferenceTotal);
       System.out.printf(" Lodging Total Sale: $%.2f \n", lodgingTotal);
      
       //Closing files
       diningOut.close();
       conferenceOut.close();
       lodgingOut.close();
      
       System.out.println("\n Data has been processed and output has been written to respective files.... \n");
}
}

____________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
in Java and is for intro level class A hotel salesperson enters sales in a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A hotel keeps records of its sales in a text file (called "hotel.txt"). Each line contains...

    A hotel keeps records of its sales in a text file (called "hotel.txt"). Each line contains the following information separated by semicolons (":"): The client's name, the service sold (such as Dining, Conference, ...), and the amount of the sale. The figure below shows a few lines of input file. Write a Java program that reads such a file to display some information on the Hotel. You are requested to use lambda expression and streams to display the following information...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

  • Java Programming: Make a Java program with two processes, a producer and a consumer. If you...

    Java Programming: Make a Java program with two processes, a producer and a consumer. If you want to use another language, clear it with me first. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random...

  • C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...

    C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales for some number of weeks. It will create an output file with a file name you have gotten from the user that will hold a summary of sales data. Specifications: 1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc. 2. The program...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...

    Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Overview These exercises will allow you to have some practice with basic Java file Input/Output. In...

    Overview These exercises will allow you to have some practice with basic Java file Input/Output. In addition, you will also have an opportunity to have more practice with methods and arrays.   Objectives Practice with programming fundamentals Variables - Declaration and Assignment Primitive types Arithmetic Expressions Simple keyboard input and text display output Branching - if-elseif-else syntax Loops - simple while loops, nested while loops Methods - functions and procedures ArrayLists - collections of variables File I/O Works towards the following...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT