In: Computer Science
LANGUAGE IS JAVA
Part One
Part Two
Both Parts
Samples:
John Public;Dinner;29.95;6/7/2014 Jane Public;Conference;499.00;8/9/2014 Abby Lawrence;Dinner;23.45;10/10/2014
John Public;Dinner;29.95;6/7/2014 Abby Lawrence;Dinner;23.45;10/10/2014
Jane Public;Conference;499.0;8/9/2014
Grading Criteria
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