数据结构课程设计

  数据结构课程设计

  数据结构课程设计详细说明书

  一、问题描述

  (一)、建立三个文本文件(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);

  }

  /*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值