* 函數功能:讀取txt文件到jTable2中

  * 函數功能:讀取txt文件到jTable2中

  *

  */

  public static void readCourseTxtFile(String filePath) {

  try {

  String encoding = "GBK";

  File file = new File(filePath);

  if (file.isFile() && file.exists()) {// 判斷文件是否存在

  InputStreamReader read = new InputStreamReader(

  new FileInputStream(file), encoding);//考慮到編碼格式

  BufferedReader bufferedReader = new BufferedReader(read);

  String lineTxt = null;

  Vector data = new Vector();

  int j = 0;

  String[] temp;

  while ((lineTxt = bufferedReader.readLine()) != null) {

  temp = lineTxt.split(" ");

  course[j] = new Course(temp[0], temp[1],

  Double.parseDouble(temp[2]));

  j += 1;

  Vector row = new Vector();

  for (int i = 0; i < temp.length; i++) {

  row.add(temp[i]);

  }

  data.add(row);

  }

  for (int k = j; k < SIZE; k++) {

  course[k] = null;

  }

  //String[] title=new String[]{"學號","姓名","性別","年齡","專業"};

  Vector title = new Vector();

  title.add("課程號");

  title.add("課程名");

  title.add("學分");

  DefaultTableModel dtm = new DefaultTableModel(data, title);

  jTable2.setModel(dtm);

  read.close();

  } else {

  System.out.println("找不到指定的文件");

  }

  } catch (Exception e) {

  System.out.println("讀取文件內容出錯");

  e.printStackTrace();

  }

  }

  /**

  * 修改課程表信息函數

  * 創建時間:2014-02-20 13:17

  * @param filePath

  * @param Sno

  */

  public static void UpdateCourseTxt(String filePath, Course st) {

  String str = new String(); //原有txt內容

  String s1 = new String();//內容更新

  try {

  File f = new File(filePath);

  if (!f.exists()) {

  System.out.print("文件不存在");

  f.createNewFile();// 不存在則創建

  }

  BufferedReader input = new BufferedReader(new FileReader(f));

  while ((str = input.readLine()) != null) {

  String temp[] = str.split(" ");

  if (temp[0].equals(st.GetCno())) {

  str = st.GetCno() + " " + st.GetCname() + " "

  + st.GetCcredit();

  }

  s1 += str + "\n";

  }

  // System.out.println(s1);

  input.close();

  BufferedWriter output = new BufferedWriter(new FileWriter(f));

  output.write(s1);

  output.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  //課程表添加按鈕

  private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  String Cno, Cname, Ccredit;

  Cno = jTextField3.getText();

  Cname = jTextField6.getText();

  Ccredit = jTextField7.getText();

  boolean b1 = hp.onlyCno(course, Cno);

  boolean b2 = hp.onlyCname(course, Cname);

  boolean b3 = hp.checkCcredit(Ccredit);

  if (b1 && b2 && b3) {

  String filePath = "D:\\數據結構課程設計\\Course.txt";

  contentToTxt(filePath, Cno + " " + Cname + " " + Ccredit + "\n");

  readCourseTxtFile(filePath);

  jTextField3.setText("");

  jTextField6.setText("");

  jTextField7.setText("");

  }

  if (!b1) {

  JOptionPane.showMessageDialog(this, "已經存在該課程號");

  jTextField3.setText("");

  }

  if (!b2) {

  JOptionPane.showMessageDialog(this, "已經存在該課程名");

  jTextField6.setText("");

  }

  if (!b3) {

  JOptionPane.showMessageDialog(this, "學分不符合規定,請重新輸入");

  jTextField7.setText("");

  }

  }

  //課程表查找按鈕

  private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  int k = 0;

  String inputValue = JOptionPane.showInputDialog("請輸入要查詢課程的課程名");

  if (inputValue != null) {

  for (; course[k] != null; k++) {

  if (course[k].GetCname()。equals(inputValue)) {

  jTextField3.setText(course[k].GetCno());

  jTextField6.setText(course[k].GetCname());

  //注意,得把double值轉化爲String

  jTextField7

  .setText(Double.toString(course[k].GetCcredit()));

  break;

  }

  }

  if (course[k] == null) {

  JOptionPane.showMessageDialog(this, "不存在該課程,請檢查輸入的課程名是否有誤。");

  }

  }

  }

  //課程表刪除按鈕

  private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  CourseTableSelected();

  String filePath = "D:\\數據結構課程設計\\Course.txt";

  String SCFilePath = "D:\\數據結構課程設計\\SC.txt";

  //返回int值0&1,0表示確定,1表示否

  int response = JOptionPane.showConfirmDialog(null, "是否真的刪除?", "標題",

  JOptionPane.YES_NO_OPTION);

  if (response == 0) {

  DeleteTxt(filePath, jTextField3.getText());

  readCourseTxtFile(filePath);

  DeleteSCTxtByNo(SCFilePath, jTextField3.getText(), 1);

  readSCTxtFile(SCFilePath);

  jTextField3.setText("");

  jTextField6.setText("");

  jTextField7.setText("");

  jTextField3.setEditable(true);

  jTextField6.setEditable(true);

  JOptionPane.showMessageDialog(this, "刪除成功!");

  } else {

  jTextField3.setText("");

  jTextField6.setText("");

  jTextField7.setText("");

  jTextField3.setEditable(true);

  }

  }

  //課程表修改按鈕

  private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  if (jButton6.getText()。equals("修改")) {

  CourseTableSelected();

  jButton6.setText("保存");

  } else {

  //返回int值0&1,0表示確定,1表示否

  int response = JOptionPane.showConfirmDialog(null, "是否進行修改?", "標題",

  JOptionPane.YES_NO_OPTION);

  //如果確定的話,執行對新數據的保存

  if (response == 0) {

  String Cno, Cname, Ccredit;

  Cno = jTextField3.getText();

  Cname = jTextField6.getText();

  Ccredit = jTextField7.getText();

  boolean b = hp.checkCcredit(Ccredit);

  if (b) {

  Course cs = new Course(Cno, Cname,

  Double.parseDouble(Ccredit));

  String filePath = "D:\\數據結構課程設計\\Course.txt";

  UpdateCourseTxt(filePath, cs);

  readCourseTxtFile(filePath);

  JOptionPane.showMessageDialog(this, "保存成功!");

  }

  if (!b) {

  JOptionPane.showMessageDialog(this, "學分輸入有誤,請重新輸入");

  jTextField7.setText("");

  }

  }

  jTextField3.setText("");

  jTextField6.setText("");

  jTextField7.setText("");

  jTextField3.setEditable(true);

  jTextField6.setEditable(true);

  jButton6.setText("修改");

  }

  }

  (六)、對成績表的操作

  /**

  * 對成績表的操作

  * @param args

  */

  static SC sc[] = new SC[SIZE];

  //成績表把表格的選中行填到文本框中

  private void SCTableSelected() {

  // TODO add your handling code here:

  int rowIndex = jTable3.getSelectedRow();

  jTextField8.setText(jTable3.getValueAt(rowIndex, 0)。toString());

  jTextField9.setText(jTable3.getValueAt(rowIndex, 1)。toString());

  jTextField10.setText(jTable3.getValueAt(rowIndex, 2)。toString());

  jTextField8.setEditable(false);

  jTextField9.setEditable(false);

  }

  /**

  * 修改成績表信息函數

  * 創建時間:2014-02-22 21:33

  * @param filePath

  * @param st

  */

  public static void UpdateSCTxt(String filePath, SC st) {

  String str = new String(); //原有txt內容

  String s1 = new String();//內容更新

  try {

  File f = new File(filePath);

  if (!f.exists()) {

  System.out.print("文件不存在");

  f.createNewFile();// 不存在則創建

  }

  BufferedReader input = new BufferedReader(new FileReader(f));

  while ((str = input.readLine()) != null) {

  String temp[] = str.split(" ");

  if (temp[0].equals(st.GetSno()) && temp[1].equals(st.GetCno())) {

  str = st.GetSno() + " " + st.GetCno() + " " + st.GetGrade();

  }

  s1 += str + "\n";

  }

  input.close();

  BufferedWriter output = new BufferedWriter(new FileWriter(f));

  output.write(s1);

  output.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  /*

  * 函數功能:讀取txt文件到jTable3中

  * 創建時間:2014-02-22 12:13

  *

  */

  public static void readSCTxtFile(String filePath) {

  try {

  String encoding = "GBK";

  File file = new File(filePath);

  if (file.isFile() && file.exists()) {// 判斷文件是否存在

  InputStreamReader read = new InputStreamReader(

  new FileInputStream(file), encoding);//考慮到編碼格式

  BufferedReader bufferedReader = new BufferedReader(read);

  int k = 0;

  String lineTxt = null;

  Vector data = new Vector();

  String[] temp;

  while ((lineTxt = bufferedReader.readLine()) != null) {

  temp = lineTxt.split(" ");

  sc[k] = new SC(temp[0], temp[1],

  Double.parseDouble(temp[2]));

  k += 1;

  Vector row = new Vector();

  for (int i = 0; i < temp.length; i++) {

  row.add(temp[i]);

  }

  data.add(row);

  }

  for(int m=k;k<size;k++)< p="">

  {

  sc[k]=null;

  }

  //String[] title=new String[]{"學號","姓名","性別","年齡","專業"};

  Vector title = new Vector();

  title.add("學號");

  title.add("課程號");

  title.add("成績");

  DefaultTableModel dtm = new DefaultTableModel(data, title);

  jTable3.setModel(dtm);

  read.close();

  } else {

  System.out.println("找不到指定的文件");

  }

  } catch (Exception e) {

  System.out.println("讀取文件內容出錯");

  e.printStackTrace();

  }

  }

  /**

  * 成績表的刪除函數

  * 創建時間:2014-02-22 21:55

  * @param filePath

  * @param num

  */

  public static void DeleteSCTxt(String filePath, String Sno, String Cno) {

  String str = new String(); //原有txt內容

  String s1 = new String();//內容更新

  try {

  File f = new File(filePath);

  if (!f.exists()) {

  System.out.print("文件不存在");

  f.createNewFile();// 不存在則創建

  }

  BufferedReader input = new BufferedReader(new FileReader(f));

  while ((str = input.readLine()) != null) {

  String temp[] = str.split(" ");

  if (temp[0].equals(Sno)&& temp[1].equals(Cno)) {

  } else {

  s1 += str + "\n";

  }

  }

  // System.out.println(s1);

  input.close();

  BufferedWriter output = new BufferedWriter(new FileWriter(f));

  output.write(s1);

  output.close();

  } catch (Exception e) {

  1

  }

  }

  /**

  * 根據學號或課程號完成成績表的刪除函數

  * 創建時間:2014-02-23 22:21

  * @param filePath

  * @param Sno

  */

  public static void DeleteSCTxtByNo(String filePath, String no, int k) {

  String str = new String(); //原有txt內容

  String s1 = new String();//內容更新

  try {

  File f = new File(filePath);

  if (!f.exists()) {

  System.out.print("文件不存在");

  f.createNewFile();// 不存在則創建

  }

  BufferedReader input = new BufferedReader(new FileReader(f));

  while ((str = input.readLine()) != null) {

  String temp[] = str.split(" ");

  if (!temp[k].equals(no)) {

  s1 += str + "\n";

  }

  }

  // System.out.println(s1);

  input.close();

  BufferedWriter output = new BufferedWriter(new FileWriter(f));

  output.write(s1);

  output.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  //刪除成績按鈕

  //成績表的刪除按鈕

  private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  SCTableSelected();

  String filePath = "D:\\數據結構課程設計\\SC.txt";

  //返回int值0&1,0表示確定,1表示否

  int response = JOptionPane.showConfirmDialog(null, "是否真的刪除?", "標題",

  JOptionPane.YES_NO_OPTION);

  if (response == 0) {

  DeleteSCTxt(filePath, jTextField8.getText(), jTextField9.getText());

  readSCTxtFile(filePath);

  jTextField8.setText("");

  jTextField9.setText("");

  jTextField10.setText("");

  jTextField8.setEditable(true);

  jTextField9.setEditable(true);

  JOptionPane.showMessageDialog(this, "刪除成功!");

  } else {

  jTextField8.setText("");

  jTextField9.setText("");

  jTextField10.setText("");

  jTextField8.setEditable(true);

  jTextField9.setEditable(true);

  }

  }

  //成績表的修改按鈕

  //修改成績按鈕

  private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  if (jButton10.getText()。equals("修改")) {

  SCTableSelected();

  jButton10.setText("保存");

  } else {

  //返回int值0&1,0表示確定,1表示否

  int response = JOptionPane.showConfirmDialog(null, "是否進行修改?", "標題",

  JOptionPane.YES_NO_OPTION);

  //如果確定的話,執行對新數據的保存

  if (response == 0) {

  String Sno, Cno, Grade;

  Sno = jTextField8.getText();

  Cno = jTextField9.getText();

  Grade = jTextField10.getText();

  boolean b = hp.checkGrade(Grade);

  if (b) {

  SC sc = new SC(Sno, Cno, Double.parseDouble(Grade));

  String filePath = "D:\\數據結構課程設計\\SC.txt";

  UpdateSCTxt(filePath, sc);

  readSCTxtFile(filePath);

  JOptionPane.showMessageDialog(this, "保存成功!");

  } else {

  JOptionPane.showMessageDialog(this, "成績值輸入有誤(0~100)");

  jTextField10.setText("");

  return;

  }

  }

  jTextField8.setText("");

  jTextField9.setText("");

  jTextField10.setText("");

  jTextField8.setEditable(true);

  jTextField9.setEditable(true);

  jButton10.setText("修改");

  }

  }

  //成績表的添加按鈕

  //添加成績的按鈕

  private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {

  // TODO add your handling code here:

  String Sno, Cno, Grade;

  Sno = jTextField8.getText();

  Cno = jTextField9.getText();

  Grade = jTextField10.getText();

  boolean b1 = hp.existSno(student, Sno);

  boolean b2 = hp.existCno(course, Cno);

  boolean b3 = hp.checkGrade(Grade);

  boolean b4=hp.checkOnlyOneSC(sc, Sno, Cno);

  if (b1&&b2&&b3&&b4) {

  String filePath = "D:\\數據結構課程設計\\SC.txt";

  contentToTxt(filePath, Sno + " " + Cno + " " + Grade + "\n");

  readSCTxtFile(filePath);

  jTextField8.setText("");

  jTextField9.setText("");

  jTextField10.setText("");

  }

  if (!b1) {

  JOptionPane.showMessageDialog(this, "不存在該同學,請檢查輸入是否有誤");

  jTextField8.setText("");

  }

  if (!b2) {

  JOptionPane.showMessageDialog(this, "不存在該課程,請檢查輸入是否有誤");

  jTextField9.setText("");

  }

  if (!b3) {

  JOptionPane.showMessageDialog(this, "成績值輸入有誤(0~100)");

  jTextField10.setText("");

  }

  if(!b4){

  JOptionPane.showMessageDialog(this, "該生的該課程已有成績,不能再次添加");

  jTextField8.setText("");

  jTextField9.setText("");

  }

  }

  /**

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章