java - 使用 JOptionPane 打印报告

标签 java arrays report joptionpane dialog

好的,现在我可以编译并运行代码了,但是现在输出不正确。我需要能够选择一个选项,然后对于 2、3,在选择该选项后还有 3 个附加选项。我应该如何调整我的编码来做到这一点?

任务:

  1. 所有信息列表

  2. 特定医生的所有手术列表(提示医生)

  3. 特定类型的所有手术列表(提示手术类型)

  4. 支付给每位医生的手术费用总额

  5. 平均费用

数据文件(患者.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去做这个。但不是 String使用 StringBuilderStringBuffer 。由于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);

关于java - 使用 JOptionPane 打印报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424008/

相关文章:

java - 如何比较Arraylist中不同数组中的两个元素?

数组作为哈希值 : get the 2 arrays with most elements

c# - C#中的卡片打印

report - 如何使用nodejs加密和解密亚马逊卖家api报告文件

crystal-reports - Crystal 报表子报表重叠

java - .NET BCL 中是否有等同于 java 的 File.deleteOnExit() 的方法?

java - 如何删除 ExpandableListView 中父子之间的特定空间

java - 组合框单元格编辑器上的 getSelectedRow

java 试图分配较弱的访问权限错误

c++ - 检查二维数组中的相邻索引