2015-09-06 144 views
1

好吧,现在我得到我的代码进行编译和运行,但现在输出不正确。我需要能够选择一个选项,然后选择2,3选择3个附加选项。我应该如何调整我的编码才能做到这一点?使用JOptionPane打印报告

任务:

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

为什么你不能使用其他组件,如'JTextArea'和'JScrollPane' d'JOptionpane'。 – Satya

回答

0

使用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); 
+0

那么,我要添加到我目前的字符串方法或替换它?编辑:我有点生疏,一般来说是新的。在我从大学毕业之前试图提高我的技能。 – whitehawk23x

+0

我的建议,使用'StringBuilder'而不是'String'。 http://stackoverflow.com/questions/2439243/what-is-the-difference-between-string-and-stringbuffer-in-java – Satya

+0

私人字符串菜单(){ 把它放在这里? – whitehawk23x