0.2版更新内容:
1.使用文件存储数据;
2.使用HashMap查找学生信息;
3.使用DAO模式;
4.使用Factory模式;
5.使用BufferedReader、PrintWriter读写文件。
Student.java:学生类
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
public class Student {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StudentAction.java:学生信息处理类
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentAction {
Scanner sc = new Scanner(System.in);
public void add() {
Student stu = new Student();
System.out.println("请输入学生学号:");
String id = sc.next();
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
try {
DAOFactory.getStudentDAOInstance().add(stu);
} catch (Exception e) {
System.out.println("添加学生信息失败!");
}
}
public void showAll() {
List<Student> stus = null;
try {
stus = DAOFactory.getStudentDAOInstance().queryAll();
} catch (Exception e) {
System.out.println("信息查找失败!");
}
Iterator<Student> iter = stus.iterator();
this.printHeader();
while (iter.hasNext()) {
Student stu = iter.next();
this.print(stu);
}
this.printFooter();
}
public void printHeader() {
System.out.println("------------------------");
System.out.println("学号\t姓名\t年龄");
System.out.println("------------------------");
}
public void printFooter() {
System.out.println("------------------------");
}
public void print(Student stu) {
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge());
}
public Student queryById() {
System.out.println("请输入学号:");
String id = sc.next();
HashMap<String, Student> stus = null;
try {
stus = DAOFactory.getStudentDAOInstance().queryById();
} catch (Exception e) {
System.out.println("信息查找失败!");
}
Student stu = null;
if (stus.containsKey(id)) {
stu = stus.get(id);
}
if (stu != null) {
this.printHeader();
this.print(stu);
this.printFooter();
} else {
System.out.println("对不起,没找到您要查找的学生!");
}
return stu;
}
}
DAOFactory.java:数据操作工厂类
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
public class DAOFactory {
public static StudentDAO getStudentDAOInstance() {
return new StudentDAOImpl();
}
}
StudentDAO.java:学生信息数据操作接口
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
import java.util.HashMap;
import java.util.List;
public interface StudentDAO {
//查询所有学生信息
public List<Student> queryAll() throws Exception;
//按ID查询学生信息
public HashMap<String, Student> queryById() throws Exception;
//添加学生信息
public void add(Student stu) throws Exception;
}
StudentDAOImpl.java:学生信息数据操作实现类
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class StudentDAOImpl implements StudentDAO {
public static final String FILE_PATH = "DB/student.txt";
public List<Student> queryAll() throws Exception {
List<Student> stus = new ArrayList<Student>();
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(FILE_PATH);
br = new BufferedReader(fr);
String stuStr = null;
while ((stuStr = br.readLine()) != null) {
String[] stuMsg = stuStr.split(":");
if (stuMsg.length == 3) {
Student stu = new Student();
stu.setId(stuMsg[0]);
stu.setName(stuMsg[1]);
int age = 20;
try {
age = Integer.parseInt(stuMsg[2]);
} catch (NumberFormatException e) {
age = 20;
}
stu.setAge(age);
stus.add(stu);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new Exception(e);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
br = null;
}
if (fr != null) {
try {
fr.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
fr = null;
}
}
return stus;
}
public HashMap<String, Student> queryById() throws Exception {
HashMap<String, Student> stus = new HashMap<String, Student>();
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(FILE_PATH);
br = new BufferedReader(fr);
String stuStr = null;
while ((stuStr = br.readLine()) != null) {
String[] stuMsg = stuStr.split(":");
if (stuMsg.length == 3) {
Student stu = new Student();
stu.setId(stuMsg[0]);
stu.setName(stuMsg[1]);
int age = 20;
try {
age = Integer.parseInt(stuMsg[2]);
} catch (NumberFormatException e) {
age = 20;
}
stu.setAge(age);
stus.put(stuMsg[0], stu);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new Exception(e);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
br = null;
}
if (fr != null) {
try {
fr.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
fr = null;
}
}
return stus;
}
public void add(Student stu) throws Exception {
FileWriter fw = null;
PrintWriter pw = null;
try {
fw = new FileWriter(FILE_PATH, true);
pw = new PrintWriter(fw, true);
pw.println(stu.getId() + ":" + stu.getName() + ":" + stu.getAge());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
pw = null;
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
}
fw = null;
}
}
}
}
StudentMgr.java:学生信息管理类
程序代码 程序代码
package cn.edu.ahau.mgc.stu;
import java.util.Scanner;
public class StudentMgr {
public static void main(String[] args) {
StudentAction sa = new StudentAction();
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
showMenu();
int selected = sc.nextInt();
switch(selected) {
case 1 :
sa.add();
break;
case 2 :
sa.showAll();
break;
case 3 :
sa.queryById();
break;
case 4 :
flag = false;
System.out.println("已退出!");
break;
}
}
}
public static void showMenu() {
System.out.println("学生信息管理菜单:");
System.out.println("(1)增加学生信息");
System.out.println("(2)显示所有学生信息");
System.out.println("(3)根据学号查询学生信息");
System.out.println("(4)退出管理");
}
}
student.txt:学生数据文件
程序代码 程序代码
1001:Magci:20
1002:MGC:20
1003:haha:24
1005:lala:19
1004:test:23