簡體   English   中英

使用JOptionPane打印報告

[英]Using JOptionPane to print reports

好的,現在我可以編譯並運行代碼,但是現在輸出不正確。 我需要能夠選擇一個選項,然后對於2和3,在選擇了該選項之后,還有3個其他選項。 我應該如何調整我的編碼來做到這一點?

任務:

  1. 全部信息一覽

  2. 特定醫生的所有手術清單(提示醫生)

  3. 特定類型的所有手術清單(提示手術類型)

  4. 支付給每位醫生的手術費總額

  5. 平均費用

數據文件(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  

到目前為止的代碼可供參考:

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
}
'

使用StringStringBuilderStringBuffer來執行此操作。 但是使用StringBuilderStringBuffer代替String 由於是String ,因此需要一些額外的對象來進行String操作。

例如:

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());

使用HTML標記產生更好的格式化結果,例如<font><table>等。

例如:

String startTag ="<font size='2' color='red'>";
String endTag = "</font>";
sb.append(startTag+"Some content"+endTag);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM