关于数据表

Angel_Dumpling 2017-09-22 05:06:59
实现一个顺序表,并基于该顺序表实现一个学生通讯录管理系统。学生信息包括学号、姓名、性别、电话和地址。程序的功能要求如下:
1. 建立:定义一个顺序表类型,并以此顺序表的对象来存储通讯录中所有学生的信息。
2. 插入:将指定的新学生信息插入到通讯录顺序表的指定位置。
3. 删除:从通讯录顺序表中删除指定位置或学号的学生信息。
4. 查找:按学号查找相关学生,若找到则输出该生的所有信息,否则提示未找到。
5. 修改:可以修改学号之外的其它所有学生信息。
6. 显示所有元素:按顺序显示通讯录中所有学生的信息。
7. 以上功能应以数字菜单的方式列出。
本次实验的代码框架已经给出,请补全如下方法的实现:
private void init(int size)
public void insert(int i, Object obj) throws Exception
public Object delete(int i) throws Exception
static int searchByNo(SeqList SL, String stuNo)
...全文
165 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
长安223 2017-09-23
  • 打赏
  • 举报
回复
类和方法还没化理解为实例,抱歉帮不了你。
Angel_Dumpling 2017-09-23
  • 打赏
  • 举报
回复
package seqlist; import java.io.Serializable; public class SeqList implements IList, Serializable { final int defaultSize = 10; // 默认为10个 int maxsize; int curLen; Object[] listArray; public SeqList() { init(defaultSize); } public SeqList(int size) { init(size); } // 初始化 // 补全该方法 private void init(int size) { } // 在i位置插入obj对象 // 补全该方法 @Override public void insert(int i, Object obj) throws Exception { } // 删除i位置的元素 // 补全该方法 @Override public Object delete(int i) throws Exception { return null; } // 获取i位置的元素并返回 @Override public Object getData(int i) throws Exception { if (i < 0 || i > curLen) { throw new Exception("参数错误!"); } return listArray[i]; } // 获得表长 @Override public int getLen() { return curLen; } // 判断是否为空 @Override public boolean isEmpty() { return curLen == 0; } // 多个值为x对象的删除(表中不存在x返回0,删除成功返回1) public int MoreDataDelete(SeqList SL, Object x) throws Exception { int i; int tag = 0; for (i = 0; i < SL.curLen; i++) { if (x.equals(SL.getData(i))) { SL.delete(i); i--; tag = 1; } } return tag; } // 查找顺序表中值的x元素,若查找成功则返回元素在表中的位序(0~curLen-1),否则返回-1。 public int indexOf(Object x) { int j = 0; // j指示顺序表中待比较的数据元素,其初始值指示顺序表中第0个数据元素 while (j < curLen && !listArray[j].equals(x)) // 依次比较 { j++; } if (j < curLen) // 判断j的位置是否位于顺序表中 { return j; // 返回值为x的数据元素在顺序表中的位置 } else { return -1; // 值为x的数据元素在顺序表中不存在 } } // 输出顺序表中的数据元素 public void display() { for (int j = 0; j < curLen; j++) { System.out.println(j + " : " + listArray[j].toString()); } System.out.println(); } }
Angel_Dumpling 2017-09-23
  • 打赏
  • 举报
回复
package app; import java.io.Serializable; public class Student implements Serializable { private String stuNo; private String name; private char gender; private String tel; private String address; public Student(String stuNo, String name) { this.stuNo = stuNo; this.name = name; } public Student(String stuNo, String name, char gender, String tel, String address) { this(stuNo, name); this.gender = gender; this.tel = tel; this.address = address; } public String getStuNo() { return stuNo; } public void setStuNo(String stuNo) { this.stuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [stuNo=" + stuNo + ", name=" + name + ", gender=" + gender + ", tel=" + tel + ", address=" + address + "]"; } }
Angel_Dumpling 2017-09-23
  • 打赏
  • 举报
回复
package app; import java.io.*; import java.util.Scanner; import seqlist.SeqList; public class StuAddressBook { // 读数据文件,用读到的数据初始化顺序表。 SeqList readData() { // 判断路径是否存在 File filePath = new File("MyData"); File fileName = new File(filePath, "stu.ser"); if (!fileName.exists()) { return null; } FileInputStream fis = null; ObjectInputStream ois = null; SeqList sl = null; try { fis = new FileInputStream(fileName); ois = new ObjectInputStream(fis); sl = (SeqList) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { ois.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } return sl; // 返回初始化好的顺序表 } // 将顺序表中的数据保存到文件。 void saveToFile(SeqList SL) { // 判断路径是否存在并创建 File filePath = new File("MyData"); if (!filePath.exists()) { filePath.mkdir(); } File fileName = new File(filePath, "stu.ser"); FileOutputStream fs = null; ObjectOutputStream oos = null; try { fs = new FileOutputStream(fileName); oos = new ObjectOutputStream(fs); oos.writeObject(SL); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); fs.close(); } catch (IOException e) { e.printStackTrace(); } } } static void menu() { System.out.println("**************************"); System.out.println("* 1 - 插入一个学生 *"); System.out.println("* 2 - 刪除一个学生 *"); System.out.println("* 3 - 查找指定学生 *"); System.out.println("* 4 - 修改学生信息 *"); System.out.println("* 5 - 显示所有学生 *"); System.out.println("* 0 - 结束程序 *"); System.out.println("**************************"); System.out.println("请输入菜单项:"); } Student input() { Scanner sc = new Scanner(System.in); System.out.println("请输入一个学号:"); String no = sc.nextLine(); System.out.println("请输入一个姓名:"); String name = sc.nextLine(); Student s = new Student(no, name); return s; } // 按学号查找 // 补全该方法 static int searchByNo(SeqList SL, String stuNo) { return -1; } static void modifyMenu() { System.out.println("**************************"); System.out.println("* 1 - 修改姓名 *"); System.out.println("* 2 - 修改性别 *"); System.out.println("* 3 - 修改电话 *"); System.out.println("* 4 - 修改地址 *"); System.out.println("* 0 - 修改结束返回 *"); System.out.println("**************************"); System.out.println("请选择菜单项:"); } // 修改指定学生的信息 static boolean modifyByNo(SeqList SL, String stuNo) { boolean success = false; for (int i = 0; i < SL.getLen(); i++) { Student data = null; try { data = (Student) (SL.getData(i)); // 获取第i个元素 } catch (Exception e) { e.printStackTrace(); } if (data.getStuNo().equals(stuNo)) { success = true; boolean flag = true; while (flag) { modifyMenu(); Scanner sc = new Scanner(System.in); int modifySelect = sc.nextInt(); System.out.println("当前学生信息:" + data.toString()); sc = new Scanner(System.in); // ?? switch (modifySelect) { case 1: // 修改姓名 System.out.println("请输入新的姓名:"); String name = sc.nextLine().trim(); data.setName(name); break; case 2: // 修改性别 System.out.println("请输入新的性别:"); char gender = sc.nextLine().trim().charAt(0); data.setGender(gender); break; case 3: // 修改电话 System.out.println("请输入新的电话:"); String tel = sc.nextLine().trim(); data.setTel(tel); break; case 4: // 修改地址 System.out.println("请输入新的地址:"); String address = sc.nextLine().trim(); data.setAddress(address); break; case 0: // 修改结束返回 flag = false; }// end switch }// end while } // end if }// end for return success; } public static void main(String[] args) { StuAddressBook sab = new StuAddressBook(); SeqList SL = sab.readData(); if (SL == null) { // 数据文件不存在,则创建空的顺序表 SL = new SeqList(); } while (true) { int poi; int ret; String stuNo; Object data = null; menu(); Scanner sc = new Scanner(System.in); int select = sc.nextInt(); // sc.close(); switch (select) { case 1: // 插入一个学生到顺序表的指定位置 Student x = sab.input(); System.out.printf("请输入插入学生的位置[0,%d]:", SL.getLen()); poi = sc.nextInt(); try { SL.insert(poi, x); } catch (Exception e) { e.printStackTrace(); } break; case 2: // 删除顺序表中指定位置的学生 System.out.printf("请输入删除学生的位置[0,%d]:", SL.getLen() - 1); poi = sc.nextInt(); try { Student s = (Student) SL.delete(poi); System.out.println(s + "已经成功删除!"); } catch (Exception e) { e.printStackTrace(); } break; case 3: // 查找指定学生 System.out.println("请输入待查找学生的学号:"); sc = new Scanner(System.in); // ??? stuNo = sc.nextLine(); ret = searchByNo(SL, stuNo); if (ret != -1) { System.out.printf("您查找的学生在%d单元,其信息如下:\n", ret); try { data = SL.getData(ret); // 获取第i个元素 } catch (Exception e) { e.printStackTrace(); } System.out.println(((Student) data).toString()); } else { System.out.println("您查找的学生不存在!"); } break; case 4: // 修改指定学生的信息 System.out.printf("请输入待修改学生的学号:"); sc = new Scanner(System.in); // ??? stuNo = sc.nextLine(); boolean suc = modifyByNo(SL, stuNo); if (suc) { System.out.println("修改完成!"); } else { System.out.println("顺序表中未找到您要修改的学生!"); } break; case 5: // 显示所有学生 if (SL.isEmpty()) { System.out.println("顺序表为空!"); } else { System.out.println("顺序表中的内容如下:"); SL.display(); } break; case 0: // 保存顺序表到文件,退出 sab.saveToFile(SL); System.exit(0); } } } }
李德胜1995 2017-09-22
  • 打赏
  • 举报
回复
Angel_Dumpling 2017-09-22
  • 打赏
  • 举报
回复
....................

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧