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

the data file provided contains similar to data below name \"purchases.txt\" Ama

ID: 3539526 • Letter: T

Question

the data file provided contains similar to data below name "purchases.txt"


Amazon 22.5

Bojangles 18.23

Lowes 56.50

Mcdonalds 6.73

FAB 19.25

Bojangles .99

Firestone 189.34

Lowes 67.82

Lowes 88.20

Amazon 200

Bojagles8.9

Firestone 340.77


write a java program that does the following:


I. read the data from the file


II. stores data in suitable data structure(s).


III. prompts the user as shown below:

   1. Enter 1 to search data

2.Enter 2 to exit program


IV. if user enters 2, your program print message saying "Exiting. Have a good day",

and terminates


V.if user enters 1

   1. the program prompts the user for the name of a business and shows all the purchases at the business.


2. the programs also prints the total amount of the money spent at that business.


3. the program repeat step 3 above.



VI your program works correctly, without any changes or recompilation, even the data in the file is altered. that is if lines are added, deleted or the values change.

Explanation / Answer

I have written the program, what you required. I have tested it with your input. Please test with your inputs and validate it. Please rate me. Thanks


import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

public class PurchaseDataProcessor {
    public static void main(String args[])throws Exception{
        Hashtable<String, ArrayList<Double>> purchaseData = new Hashtable<String, ArrayList<Double>>();
       
        //Create File Object.
        File file = new File("purchases.txt");
       
        //check file is valid or not.
        if(!file.exists() || file.isDirectory()){
            System.out.println("Invalid file input......");
            System.exit(0);
        }
       
        processFile(purchaseData, file);
       
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        while(true){
            System.out.println("Enter 1 to search data");
            System.out.println("Enter 2 to exit program ");
           
            while(true){
                try{
                    System.out.print("Enter Choice : ");
                    choice = scanner.nextInt();
                    scanner.skip(" ");
                    //valid input
                    if(choice >= 1 || choice <= 2 ){
                        break;
                    }else{
                        System.out.println("Invalid input.... Please try again....");
                    }
                }catch(Exception e){
                    System.out.println("Invalid input.... Please try again....");
                }
            }
           
            switch(choice){
            case 1:{
                System.out.println("==============================");
                System.out.print("Enter Business : ");
                String business = scanner.nextLine();
                if(purchaseData.containsKey(business)){
                    ArrayList<Double> prices = purchaseData.get(business);
                    double total = 0;
                    System.out.println("Purchases......");
                    System.out.print(business+" : ");
                    for(int i=0;i<prices.size();i++){
                        total +=prices.get(i);
                        System.out.print(prices.get(i));
                        if(i != prices.size()-1){
                            System.out.print(", ");
                        }
                    }
                    System.out.println(" Total purchases : "+total);
                }else{
                    System.out.println("Business '"+business+"' not found.... Please try again");
                }
                System.out.println("==============================");
                break;
            }
            case 2:{
                System.out.println("Exiting. Have a good day");
                System.exit(0);
            }
            }
        }
    }
   
    public static void processFile(Hashtable<String, ArrayList<Double>> purchaseData, File fileName)throws Exception{
        Scanner scanner = new Scanner(new FileInputStream(fileName));
        while(scanner.hasNext()){
            String line = scanner.nextLine();
            if(line == null || line.equals("")){
                continue;
            }
           
            String content[] = line.split(" ");
           
            if(content.length != 2){
                continue;
            }
            ArrayList<Double> prices = null;
            if(!purchaseData.containsKey(content[0])){
                prices = new ArrayList<Double>();
                purchaseData.put(content[0], prices);
            }else{
                prices = purchaseData.get(content[0]);
            }
            prices.add(Double.parseDouble(content[1].trim()));
        }
    }
}