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

BIG JAVA P7.16 Use JFileChooser to select the input and output files. A hotel sa

ID: 3667797 • Letter: B

Question

BIG JAVA P7.16

Use JFileChooser to select the input and output files.

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 in the file does not exists or the format is incorrect. Use JFileChooser to select the input and output files.

Explanation / Answer

Java Program


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;

public class hotelDataCalculator {
    public static void main(String[] args) throws IOException{
        String[] clientName = new String[10];
        String[] serviceSold = new String[10];
        int[] amountSale = new int[10];
        String[] dateEvent = new String[10];
        int i = 0;
      
        JFileChooser chooserFile = new JFileChooser();
        int result = chooserFile.showOpenDialog(null);

        File file = chooserFile.getSelectedFile(); //get file here
      
        if(!file.getName().endsWith(".txt")) // check if the file extension is .txt only
        {
            System.out.println("File format is not correct, Its not .txt");
            return;
        }
      
        // Open file here
      
        java.io.BufferedReader readerFile = new java.io.BufferedReader(new java.io.FileReader(file));
      
        String line = readerFile.readLine();
      
        String[] hotelData_list = new String[4];
        while (i < 10) // here we have suppose that we have ten records
        {
            hotelData_list = line.split(";"); // split the data into array
            clientName[i] =hotelData_list[0]; // client array having all the client details
            serviceSold[i] =hotelData_list[1]; // serviceSold array
            amountSale[i] = Integer.parseInt(hotelData_list[2]); // amountSale array
            dateEvent[i] = hotelData_list[3]; //array for data
            i++;
        }     
       // using these above calculated array, you get get the desired output
      
        readerFile.close();
    }
}