數據結構課程設計
數據結構課程設計詳細說明書
一、問題描述
(一)、建立三個文本文件(Student.txt,Course.txt和SC.txt)
1.學生表(文件Student.txt)(學號(唯一Sno),姓名(Sname),性別(Ssex),年齡(Sage),專業(Sdept))
2.課程表(文件Course.txt)(課程號(唯一Cno),課程名(Cname),學分(Ccredit))
3.學生成績表(文件SC.txt)(學號(Sno) 課程號(Cno) 成績(Grade))
4.表間關係如圖所示:
(二)、對三個文件的操作
1、爲存儲學生、課程和選課信息設計數據結構,將以上文件內容導入其中;
2、對學生表進行插入、刪除、修改和查詢操作;
3、對選修表進行插入、刪除、修改操作;
4、綜合查詢(查詢一門課程選課學生的信息和查詢某位同學所有課程的成績信息);
5、將系統中的數據保存迴文件中;
二、解題思路
1、建立三張表的數據存儲結構(Java的對象數組),完成Student,Course和SC三個類,並且設計各屬性爲private類型,獲取其屬性值的函數Get~~(),還有待參數的構造函數,便於直接賦值。
2、實現對學生表數據的增刪改查,通過讀寫文件操作做到同步更新txt文件,抽取部分代碼達到最簡潔。
1>、通過文件的讀取和插入同步對數據進行存儲,每次運行時對數據進行讀取到jTable中,同時對對象數組初始化。插入後又執行一次讀取,所以又存儲一次。繼而完成對數據表的增加
2>、修改時先點擊表格中的數據,使數據先顯示到文本框中,接着修改變爲保存,清空文本框。接着執行讀取函數,重新存儲。
3>、刪除時先選定要刪除的數據,和修改一樣。接着執行一次讀取。完成刪除
4>、查找函數,通過學號查找學生信息。點擊一下查找按鈕,彈出一個文本框,輸入學號,通過學號查詢,若查詢到,即把學生信息顯示到文本框中;若查詢不到,則提示沒有該學生信息;當然也得對輸入學號進行驗證。
3、實現對課程表的增刪改查功能,和學生表類似。
4、關鍵的成績表,它關聯着學生表和課程表。從它上面可以看出學生選的課程以及某門課程被那些人選了。故通過成績表來進行綜合查詢。
1>、首先是把SC.txt文件中的數據讀取到jTable中,同時對SC數據進行存儲到SC對象數組中去。
2>、對成績表進行添加,添加現有學生的學號和現有課程的課程號及成績。但是學號+課程號一起不能重複,就是一個人的某一門課程只能有一個成績。添加仿照課程表的添加。
3>、對成績表進行修改,修改只能修改成績。
4>、對成績表進行刪除,直接刪除就行。
5、建立綜合查詢:
1>、建立查詢個人的成績單,從成績表中查找一個學號對應的多個課程號,再通過課程號從課程表中獲得對應的課程名,從成績表中取得成績。
2>、建立查詢某門課程的遜克學生情況,輸入課程名,從課程表中取得對應的課程號,再通過成績表得到該課程號對應的所有學號,通過學號從學生表中取得所有選該科目的學生信息。
6、完成學生表的添加時對學號的正確性和唯一性驗證;完成對學生表中學生年齡的正確性驗證;完成學生表中數據刪除時同時刪除成績表中該學生學號對應的所有課程成績。
7、完成選課表的添加時對學分的正確性驗證,對課程號和課程名的唯一性驗證;完成對課程名和學分的修改時繼續驗證;完成課程表刪除時同時刪除成績表中的該課程號對應的所有成績。
8、完成成績表的添加時對學號的課程號的檢索,檢索是否存在該學號和課程號;同時對成績分數進行限制(0~100),且僅允許有一位小數。當然修改時也得對成績進行驗證。
三、 主要源程序及其分析
(一)、定義存儲結構
public class Student {
/**
* 學生信息類
* 功能:存儲學生信息(學號,姓名,性別,年齡,專業)
*/
private String Sno;
private String Sname;
private String Ssex;
private int Sage;
private String Sdept;
Student(){}
Student(String Sno,String Sname,String Ssex,int Sage,String Sdept){
this.Sno=Sno;
this.Sname=Sname;
this.Ssex=Ssex;
this.Sage=Sage;
this.Sdept=Sdept;
}
public String GetSno(){return Sno;}
public String GetSname(){return Sname;}
public String GetSsex(){return Ssex;}
public int GetSage(){return Sage;}
public String GetSdept(){return Sdept;}
public static void main(String[] args) {
}
}
/**
* 課程信息類
*/
class Course{
private String Cno;
private String Cname;
private double Ccredit;
Course(){}
Course(String Cno,String Cname,double Ccredit){
this.Cno=Cno;
this.Cname=Cname;
this.Ccredit=Ccredit;
}
public String GetCno(){return Cno;}
public String GetCname(){return Cname;}
public double GetCcredit(){return Ccredit;}
}
/**
* 學生課程成績類
*/
class SC{
private String Sno;
private String Cno;
private double Grade;
SC(){}
SC(String Sno,String Cno,double Grade){
this.Sno=Sno;
this.Cno=Cno;
this.Grade=Grade;
}
public String GetSno(){return Sno;}
public String GetCno(){return Cno;}
public double GetGrade(){return Grade;}
}
(二)、對文件的操作代碼
/**
* 創建文件
* @param fileName
* @return
*/
public static boolean createFile(File fileName) throws Exception {
boolean flag = false;
try {
if (!fileName.exists()) {
fileName.createNewFile();
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 函數功能:讀取txt文件到jTable1中
*
*/
public static void readTxtFile(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 i = 0;
String lineTxt = null;
Vector data = new Vector();
String[] temp;
while ((lineTxt = bufferedReader.readLine()) != null) {
temp = lineTxt.split(" ");
student[i] = new Student(temp[0], temp[1], temp[2],
Integer.parseInt(temp[3]), temp[4]);
i += 1;
Vector row = new Vector();
for (int m = 0; m < temp.length; m++) {
row.add(temp[m]);
}
data.add(row);
}
for (int k = i; k < SIZE; k++) {
student[k] = null;
}
Vector title = new Vector();
title.add("學號");
title.add("姓名");
title.add("性別");
title.add("年齡");
title.add("專業");
DefaultTableModel dtm = new DefaultTableModel(data, title);
jTable1.setModel(dtm);
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
/**
* 函數功能:公共可用的插入新數據到指定txt文件中
*
*/
public static void contentToTxt(String filePath, String content) {
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) {
s1 += str + "\n";
}
input.close();
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
System.out.println("寫入文件成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改學生表信息函數
*/
public static void UpdateTxt(String filePath, Student 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())) {
str = st.GetSno() + " " + st.GetSname() + " "
+ st.GetSsex() + " " + st.GetSage() + " "
+ st.GetSdept();
}
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();
}
}
/**
* 刪除txt 數據文件中的某一行內容(按學號或課程號刪除)
* @param filePath
* @param num
*/
public static void DeleteTxt(String filePath, String num) {
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(num)) {
s1 += str + "\n";
}
}
// System.out.println(s1);
input.close();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
System.out.println("刪除文件成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
(四)、對學生表的增刪改查操作
/**
* 以下是對學生表的操作
*/
//定義全局變量
final static int SIZE = 100;
final static int MAX = 10;
static Student student[] = new Student[SIZE];
static Helper hp = new Helper();
//學生表把表格的選中行填到文本框中
private void jTableSelected() {
// TODO add your handling code here:
int rowIndex = jTable1.getSelectedRow();
jTextField1.setText(jTable1.getValueAt(rowIndex, 0)。toString());
jTextField2.setText(jTable1.getValueAt(rowIndex, 1)。toString());
if (jTable1.getValueAt(rowIndex, 2)。toString()。equals("男")) {
jRadioButton1.setSelected(true);
} else {
jRadioButton2.setSelected(true);
}
jTextField4.setText(jTable1.getValueAt(rowIndex, 3)。toString());
jTextField5.setText(jTable1.getValueAt(rowIndex, 4)。toString());
jTextField1.setEditable(false);
}
//學生表插入按鈕的代碼段
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sno, sname, ssex, sage, sdept;
sno = jTextField1.getText();
sname = jTextField2.getText();
if (jRadioButton1.isSelected()) {
ssex = jRadioButton1.getText();
} else {
ssex = jRadioButton2.getText();
}
sage = jTextField4.getText();
sdept = jTextField5.getText();
boolean b1 = hp.checkSno(sno);
boolean b2 = hp.onlySno(student, sno);
boolean b3 = hp.checkSage(sage);
if (b1 && b2 && b3) {
String filePath = "D:\\數據結構課程設計\\Student.txt";
contentToTxt(filePath, sno + " " + sname + " " + ssex + " " + sage
+ " " + sdept + "\n");
readTxtFile(filePath);
jTextField1.setText("");
jTextField2.setText("");
jRadioButton1.setSelected(true);
jTextField4.setText("");
jTextField5.setText("");
}
if (!b1) {
JOptionPane.showMessageDialog(this, "學號輸入錯誤,請重新輸入");
jTextField1.setText("");
}
if (!b2) {
JOptionPane.showMessageDialog(this, "該學號已存在,請修改學號");
jTextField1.setText("");
}
if (!b3) {
JOptionPane.showMessageDialog(this, "該學生年齡輸入有誤,請重新輸入");
jTextField4.setText("");
}
}
//學生表查找按鈕的事件
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int k = 0;
String inputValue = JOptionPane.showInputDialog("請輸入學號,確認按學號查詢!");
if (inputValue != null) {
for (; student[k] != null; k++) {
if (student[k].GetSno()。equals(inputValue)) {
jTextField1.setText(student[k].GetSno());
jTextField2.setText(student[k].GetSname());
if (student[k].GetSsex()。equals("男")) {
jRadioButton1.setSelected(true);
} else {
jRadioButton2.setSelected(true);
}
//注意,得把int值轉化爲String
jTextField4.setText(Integer.toString(student[k].GetSage()));
jTextField5.setText(student[k].GetSdept());
break;
}
}
if (student[k] == null) {
JOptionPane.showMessageDialog(this, "不存在該學生,請檢查學號是否有誤。");
}
}
}
//學生表修改按鈕的事件
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jButton2.getText()。equals("修改")) {
jTableSelected();
jButton2.setText("保存");
} else {
//返回int值0&1,0表示確定,1表示否
int response = JOptionPane.showConfirmDialog(null, "是否進行修改?", "標題",
JOptionPane.YES_NO_OPTION);
//如果確定的話,執行對新數據的保存
if (response == 0) {
String sno, sname, ssex, sage, sdept;
sno = jTextField1.getText();
sname = jTextField2.getText();
if (jRadioButton1.isSelected()) {
ssex = jRadioButton1.getText();
} else {
ssex = jRadioButton2.getText();
}
sage = jTextField4.getText();
sdept = jTextField5.getText();
Student st = new Student(sno, sname, ssex,
Integer.parseInt(sage), sdept);
String filePath = "D:\\數據結構課程設計\\Student.txt";
UpdateTxt(filePath, st);
readTxtFile(filePath);
JOptionPane.showMessageDialog(this, "保存成功!");
}
jTextField1.setText("");
jTextField2.setText("");
jRadioButton1.setSelected(true);
jTextField4.setText("");
jTextField5.setText("");
jTextField1.setEditable(true);
jButton2.setText("修改");
}
}
//學生表刪除按鈕的事件
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTableSelected();
String filePath = "D:\\數據結構課程設計\\Student.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, jTextField1.getText());
readTxtFile(filePath);
DeleteSCTxtByNo(SCFilePath, jTextField1.getText(), 0);
readSCTxtFile(SCFilePath);
jTextField1.setText("");
jTextField2.setText("");
jRadioButton1.setSelected(true);
jTextField4.setText("");
jTextField5.setText("");
jTextField1.setEditable(true);
JOptionPane.showMessageDialog(this, "刪除成功!");
} else {
jTextField1.setText("");
jTextField2.setText("");
jRadioButton1.setSelected(true);
jTextField4.setText("");
jTextField5.setText("");
jTextField1.setEditable(true);
}
}
(五)、對成績表的操作代碼
/**
* 對課程表的操作
*/
static Course course[] = new Course[SIZE];
//課程表把表格的選中行填到文本框中
private void CourseTableSelected() {
// TODO add your handling code here:
int rowIndex = jTable2.getSelectedRow();
jTextField3.setText(jTable2.getValueAt(rowIndex, 0)。toString());
jTextField6.setText(jTable2.getValueAt(rowIndex, 1)。toString());
jTextField7.setText(jTable2.getValueAt(rowIndex, 2)。toString());
jTextField3.setEditable(false);
jTextField6.setEditable(false);
}
/*