1

Okay so now I got my code to compile and run, but the output is incorrect now. I need to be able to select an option and then for 2, and 3 have 3 additional options after selecting that option. How should I adapt my coding to do this?

Task:

  1. List of all information

  2. List of all surgeries for a specific doctor (prompt for the doctor)

  3. List of all surgeries of a specific type (prompt for the surgery type)

  4. Total amount of surgery fees paid to each Doctor

  5. Average Fees

Data File (patient.txt):

11111,Smith,Norris,Thyroid,1000.00    
11112,Obama,Norris,Lasek,500.00  
11113,Hoover,Norris,Dental,100.00  
11114,Cena,Norris,Lasek,500.00  
11115,Leto,Norris,Thyroid,1000.00  
22221,Clark,Bond,Thyroid,1000.00  
22222,Chang,Bond,Lasek,500.00  
22223,Dent,Bond,Dental,100.00  
22224,Nixon,Bond,Lasek,500.00  
22225,Washington,Bond,Dental,100.00  
33331,Jones,Lee,Dental,100.00  
33332,Ross,Lee,Lasek,500.00  
33333,Gates,Lee,Thyroid,1000.00  
33334,Johnson,Lee,Thyroid,1000.00  
33335,Carter,Lee,Dental,100.00  

Code so far for reference:

package Patient_Reports_Package;  
/**
* Created by bzink on 8/28/2015.
*/

import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;

/**
* The Patient_Reports_File class reads the data file into an array, and then has a menu for 5 reports.
*/
class Patient_Reports {

private final int[] id = new int[100];
private final String[] patient = new String[100];
private final String[] doctor = new String[100];
private final String[] surgery = new String[100];
private final double[] cost = new double[100];
private int count = -1;
private int i;

public static void main (String[] args) {
    int selection;
    String report_number;
    Patient_Reports patient = new Patient_Reports();
    patient.start_system();
    report_number = patient.menu();
    selection = Integer.parseInt(report_number);
    while (selection !=6) {
        if (selection == 1) {
            patient.allInformationReport();
        } else if (selection == 2) {
            patient.surgeryDoctorReport();
        } else if (selection == 3) {
            patient.surgeryTypeReport();
        } else if (selection == 4) {
            patient.doctorFeesReport();
        } else if (selection == 5) {
            patient.averageFeesReport();
        }
        report_number = patient.menu();
        selection = Integer.parseInt(report_number);
    }//while loop
    patient.writeReports();
    System.exit(0);
}//main

//Read Data File into Array
private void start_system() {

    String newLine;
    try {
        //define a file variable for Buffered read
        BufferedReader Patient_Reports = new BufferedReader(new java.io.FileReader("C:\\Users\\Brandon\\" +
                                                             "Downloads\\Patient_Reports_File\\patient.txt"));
        //read lines in file until there are no more lines in the file to read
        while ((newLine = Patient_Reports.readLine()) != null) {
            //there is a "," between each data item in each line
            StringTokenizer delimiter = new StringTokenizer(newLine, ",");
            count = count + 1;
            id[count] = Integer.parseInt(delimiter.nextToken());
            patient[count] = delimiter.nextToken();
            doctor[count] = delimiter.nextToken();
            surgery[count] = delimiter.nextToken();
            cost[count] = Double.parseDouble(delimiter.nextToken());
        }//while loop
        Patient_Reports.close();
    }//end try
    catch (IOException error) {
        //there was an error on the file writing
        System.out.println("Error on file read " + error);
    }//end catch
}//end start_system

//Report Menu
private String menu () {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Id ").append(" \n");
    stringBuilder.append("Patient ").append(" \n");
    stringBuilder.append("Doctor ").append(" \n");
    stringBuilder.append("Surgery ").append(" \n");
    stringBuilder.append("Cost ").append(" \n");
    for (int i = 0; i < 6; i++) {
        stringBuilder.append(i).append("  Name"+i).append('\n');
    }
    String startTag ="<font size='2' color='red'>";
    String endTag = "</font>";
    stringBuilder.append(startTag).append("Some content").append(endTag);
            JOptionPane.showMessageDialog(null, stringBuilder.toString());
    return stringBuilder.toString();
}//end menu\

/*
//Report Menu
private String menu() {
    String report;
    String Output = "Reports" + "\n" + "1. All_Information_Report" + "\n" +
            "2. Surgeries_Doctor_Report" + "\n" +
            "3. Surgeries_Type_Report" + "\n" +
            "4. Doctor_Fees_Report" + "\n" +
            "5. Average_Fees_Report" + "\n" +
            "6. Exit" + "\n" +
            " " + "\n" +
            "Select a Report >";
    report = JOptionPane.showInputDialog(null,
            Output, "", JOptionPane.QUESTION_MESSAGE);
    return report;
}//end menu\
*/

//Report containing all of the information
private void allInformationReport() {
    System.out.println("All Information Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

/*  void selectDoctor()
{
    //select doctor
    String doctorOutput;
    //int intNum=0,intNum1=0,i,x=-1;
    count=count+1;
    doctorOutput = "Enter the Doctor's Name";
    doctor[count] =JOptionPane.showInputDialog(null,doctorOutput,
            "",JOptionPane.QUESTION_MESSAGE);
}//end select doctor

//Start Doctor Menu
public static void doctorMenu (String[] args) {
    int selection;
    String doctorName;
    Patient_Reports doctor = new Patient_Reports();
    doctor.start_system();
    doctorName = doctorMenu();
    selection = Integer.parseInt(doctorName);
    while (selection !=4) {
        if (selection == 1) {
            doctor.norrisSurgeries();
        } else if (selection == 2) {
            doctor.bondSurgeries();
        } else if (selection == 3) {
            doctor.leeSurgeries();
        }
        doctorName = doctorMenu();
        selection = Integer.parseInt(doctorName);
    }//while loop
    doctor.writeReports();
    System.exit(0);
}//End Doctor Menu

//Report on all surgeries by Dr. Norris
private void norrisSurgeries() {
    System.out.println("Norris Surgeries Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(doctor[i] + " " + surgery[i] + " ");
    }//for loop
}//end report

//Report on all surgeries by Dr. Bond
private void bondSurgeries() {
    System.out.println("Bond Surgeries Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(doctor[i] + " " + surgery[i] + " ");
    }//for loop
}//end report

//Report on all surgeries by Dr. Lee
private void leeSurgeries() {
    System.out.println("Lee Surgeries Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(doctor[i] + " " + surgery[i] + " ");
    }//for loop
}//end report
*/

//Report on all surgeries of a specific doctor (prompt for the doctor)
private void surgeryDoctorReport() {
    System.out.println("Surgeries Doctor Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

/*
void selectSurgery()
{
    //select surgery
    String surgeryOutput;
    //int intNum=0,intNum1=0,i,x=-1;
    count=count+1;
    surgeryOutput = "Enter the Surgery Type";
    doctor[count] =JOptionPane.showInputDialog(null,surgeryOutput,
            "",JOptionPane.QUESTION_MESSAGE);
}//end select surgery
*/
//Report on all surgeries of a specific type(Prompt for the surgery type)
private void surgeryTypeReport() {
    System.out.println("Surgeries Type Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Report on the total amount of fees paid to each doctor
private void doctorFeesReport() {
    System.out.println("Doctor Fees Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Report on the Average Fee
private void averageFeesReport() {
    System.out.println("Average Fees Report");
    for (i = 0; i <= count; ++i) {
        System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
    }//for loop
}//end report

//Store Data File in Array
private void writeReports()
    {
        try {
            BufferedWriter Patient_Reports = new BufferedWriter(new  java.io.FileWriter("patient_out.txt"));
            for (i = 0; i <= count; ++i) {
                //put "," between each data item in the file
                Patient_Reports.write(id[i] + "," + patient[i] + "," + doctor[i] + "," +
                                        surgery[i] + "," + cost[i] + ",");
                //write a new line in the file
                Patient_Reports.newLine();
            }//for loop
            Patient_Reports.close();
        }//end try
        catch (IOException error) {
            //there was an error on the write to file
            System.out.println("Error on file write " + error);
        }//end error
}//end write_reports
}
'

1 Answers1

0

Use String or StringBuilder or StringBuffer to do this. But instead of String use either StringBuilder or StringBuffer. Since for String, you need some extra objects for String manipulation.

Ex:

StringBuilder sb = new StringBuilder();
sb.append("Id ").append(" Name\n");
for (int i = 0; i < 10; i++) {
    sb.append(i).append("  Name"+i).append('\n');
}
JOptionPane.showMessageDialog(null, sb.toString());

Use HTML tags to produce better formatted results like <font>, <table>, etc.

Ex:

String startTag ="<font size='2' color='red'>";
String endTag = "</font>";
sb.append(startTag+"Some content"+endTag);
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31