Question

In: Computer Science

​​​​​​​LANGUAGE IS JAVA Part One A hotel salesperson enters sales in a text file. Each line...

​​​​​​​LANGUAGE IS JAVA

  • Part One

  • 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.
  • Prompt the user for data to write the file.

    Part Two

  • Write a program that reads the text file as described above, and that writes a separate file for each service category, containing the entries for that category. Name the output files Dinner.txt, Conference.txt, and so on.
  • Enter the name of the output file from Part One as a command line argument.

    Both Parts

  • For all programs, catch and handle the Exceptions appropriately and validate the input data where needed.
  • Display an error if the sales file does not exist or the format is incorrect.
  • Also, create your own exception to handle "unknown transaction" exceptions.

    Samples:

    • Contents of sales.txt (file created in part one)
      John Public;Dinner;29.95;6/7/2014
      Jane Public;Conference;499.00;8/9/2014
      Abby Lawrence;Dinner;23.45;10/10/2014
      
    • Contents of Dinner.txt (file created in part two)
      John Public;Dinner;29.95;6/7/2014
      Abby Lawrence;Dinner;23.45;10/10/2014
      
    • Contents of Conference.txt (file created in part two)
      Jane Public;Conference;499.0;8/9/2014
      

Grading Criteria

  • You will be graded on the following components:
  • Does the program do what is required
  • Is it properly documented
  • Is it fully tested
  • As always, remember to create a default constructor and override the toString() method for all classes.
  • Is it properly designed

Solutions

Expert Solution

CODE:

- Please implement the validations as per the requirement. This program will enter data in a file and segregate the files based on the services.

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class SalesPerson {
    public static void main(String[] args) {
        HashMap < String, String > services = new HashMap < String, String > ();
        try {
            FileWriter input = new FileWriter("D:\\BackendWorkspace\\MyWork\\src\\search\\" + "personalData" + ".txt"); // give your path here and replace personalData with the filename u want to give
            Scanner sc = new Scanner(System.in);
            int canEnterMore = 1;
            String name, service, amount, date, finalVal, temp = "";
            while (canEnterMore == 1) {
                System.out.println("Enter name of client , service , amount , date");
                name = sc.nextLine();
                service = sc.nextLine();
                amount = sc.nextLine();
                date = sc.nextLine();
                finalVal = name + ";" + service + ";" + amount + ";" + date;
                if (services.containsKey(service)) {
                    temp = services.get(service) + "%" + finalVal; // stores multiple data values in hashmap seperated by % corresponding to a particular service
                    services.put(service, temp);
                } else {
                    services.put(service, finalVal); // stores service data
                }
                input.write(finalVal + "\n");
                System.out.println("Do you want to add more? Press 1 to add more and press enter");
                canEnterMore = Integer.parseInt(sc.nextLine());
            }
            input.close();
            sc.close();
        } catch (IOException e) {
            System.out.println("An error has occurred.");
            e.printStackTrace();
        }

        //part 2 begins
        FileWriter input = null;
        Iterator hmIterator = services.entrySet().iterator();

        String fileName;
        while (hmIterator.hasNext()) { // iterate hasmap and push the data in individual files.
            Map.Entry mapElement = (Map.Entry) hmIterator.next();
            String allValues = ((String) mapElement.getValue());
            fileName = (String) mapElement.getKey();
            String[] tempArr = allValues.split("%");
            try {
                input = new FileWriter("D:\\BackendWorkspace\\MyWork\\src\\search\\" + fileName + ".txt");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for (int z = 0; z < tempArr.length; z++) {
                try {
                    input.write(tempArr[z] + "\n");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}



Sample Input/Output (Output will be data in the files created in your system)
- Kindly press number only other than 1 to stop entering more. Do not enter string





Kindly upvote if this helped


Related Solutions

A hotel salesperson enters sales in a text file. Each line contains the following, separated by...
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. Use the following data for your text file:5 pts Bob;Dinner;10.00;January 1, 2013 Tom;Dinner;14.00;January 2, 2013 Anne;Lodging;125.00;January 3, 2013 Jerry;Lodging;125.00;January...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
A Java program that deciphers each message by building the string. Each line of the text...
A Java program that deciphers each message by building the string. Each line of the text file contains either a word or a command followed by a forward slash and the requirements of the command. A Stack Implementation is needed as. As you read the data from the text file, you need to know if it is a new phrase or command. The commands are: i – an insert followed by the letters, numbers, or symbols that need to be...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT