黑马程序员技术交流社区
标题:
【源码】java学生管理系统--简单功能实现
[打印本页]
作者:
滔哥
时间:
2013-3-8 19:43
标题:
【源码】java学生管理系统--简单功能实现
主函数
package com.linguofeng.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeMap;
import com.linguofeng.dao.StudentDao;
import com.linguofeng.dao.impl.StudentDaoImpl;
import com.linguofeng.model.Student;
public class StudentMain {
static StudentDao dao;
static BufferedReader br;
public static void main(String[] args) {
/** 启动时判断数据文件是否存在,如不存在则新建. */
File file = new File("student.dat");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
dao = new StudentDaoImpl();
/** 获得控制台输入流 */
br = new BufferedReader(new InputStreamReader(System.in));
/** 进行死循环 */
while (true) {
System.out.println(getLine());
System.out.println("=[0]主菜单\t\t\t\t\t\t=");
System.out.println("=[1]查找学生\t\t\t\t\t\t=");
System.out.println("=[2]增加学生\t\t\t\t\t\t=");
System.out.println("=[3]删除一个学生\t\t\t\t\t=");
System.out.println("=[4]删除所有学生\t\t\t\t\t=");
System.out.println("=[5]学生列表\t\t\t\t\t\t=");
System.out.println("=[6]更新学生\t\t\t\t\t\t=");
System.out.println("=[x]退出\t\t\t\t\t\t=");
System.out.println(getLine());
System.out.print("请选择操作:");
/** 获得控制台输入的内容 */
String input = getInput();
if (input.equals("")) {
input = "x";
}
switch (input.charAt(0)) {
case '2' :
addStu();
break;
case 'x' :
System.out.println("退出成功,欢迎再次使用!");
System.exit(0);
break;
case '5' :
findAll();
break;
case '4' :
delAll();
break;
case '3' :
delByNum();
break;
case '1' :
findByNum();
break;
case '6' :
updateByNum();
break;
default :
System.out.println("不支持的系统指令,请重新输入!");
break;
}
}
}
/**
* 根据学生学号查询学生对象信息
*/
public static void findByNum() {
System.out.print("请输入要查询的学生学号:");
while (true) {
String input = getInput();
if (dao.findByNum(input) != null) {
System.out.println(dao.findByNum(input));
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("查询失败,不存在该学生,请重新输入要删除的学生学号:");
}
}
}
/**
* 根据学生学号删除学生对象
*/
public static void delByNum() {
System.out.println(getLine());
findAll();/** 显示所有学生信息方便进行删除操作 */
System.out.println(getLine());
System.out.println("如需取消留空直接按回车.");
System.out.print("请输入要删除的学生学号:");
while (true) {
String input = getInput();
if (dao.delByNum(input)) {
System.out.println("删除成功!");
findAll();
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("删除失败,不存在该学生,请重新输入要删除的学生学号:");
}
}
}
/**
* 学生列表
*/
public static void findAll() {
TreeMap<String, Student> stuMap = dao.findAll();
Iterator<Student> iterator = stuMap.values().iterator();
if (!iterator.hasNext()) {
System.out.println("数据库为空");
}
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
/**
* 删除所有学生对象
*/
public static void delAll() {
while (true) {
System.out.print("是否要删除所有学生信息,确定请输入YES,否请输入NO:");
String input = getInput();
if (input.equals("YES") || input.equals("yes")) {
dao.delAll();
System.out.println("清空成功");
return;
} else if (input.equals("NO") || input.equals("no")) {
return;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
/**
* 添加学生
*/
public static void addStu() {
while (true) {
System.out.print("请输入要添加学生的个数:");
String ch = null;
try {
ch = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (ch.equals("")) {
System.out.println("输入有误,请重新输入!");
} else {
int size = Integer.parseInt(ch);
TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
for (int i = 0; i < size; i++) {
Student student = new Student();
try {
System.out.print("请输入第 " + (i + 1) + " 个学生的姓名:");
student.setName(br.readLine());
System.out.print("请输入第 " + (i + 1) + " 个学生的学号:");
student.setNum(br.readLine());
System.out.print("请输入第 " + (i + 1)
+ " 个学生的性别(男输:1;女输:0):");
student.setSex(Integer.parseInt(br.readLine()));
} catch (IOException e) {
e.printStackTrace();
}
stuMap.put(student.getNum(), student);
}
dao.addStu(stuMap);
findAll();
return;
}
}
}
public static void updateByNum() {
findAll();
System.out.print("请输入要修改的学生学号:");
while (true) {
String input = getInput();
String num = input;
Student student = dao.findByNum(num);
if (student != null) {
System.out.println(student);
System.out.println("如不修改直接留空按回车!");
System.out.print("请输入学生的新姓名:");
input = getInput();
if (!input.equals(""))
student.setName(input);
System.out.print("请输入学生的新学号:");
input = getInput();
if (!input.equals(""))
student.setNum(input);
System.out.print("请输入学生的新版性别(男输:1;女输:0):");
input = getInput();
if (!input.equals(""))
student.setSex(Integer.parseInt(input));
dao.updateByNum(num, student);
System.out.println("更新成功,更新后的数据:");
System.out.println(dao.findByNum(student.getNum()));
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("查询失败,不存在该学生,请重新输入要修改的学生学号:");
}
}
}
public static void addTest() {
TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
for (int i = 0; i < 10000; i++) {
Student student = new Student(i, "A" + i, "NUM" + i, 0);
stuMap.put(student.getNum(), student);
}
dao.addStu(stuMap);
return;
}
/**
* 获得===========分割符
*
* @return
*/
public static String getLine() {
return "=========================================================";
}
/**
* 获得控制台输入
*
* @return
*/
public static String getInput() {
String ch = "";
try {
ch = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
复制代码
学生类
package com.linguofeng.model;
import java.io.Serializable;
/**
* 学生类
*
* @author Fst
* @since 2013-3 {@link <A href="http://www.itheima.com">http://www.itheima.com</A>}
*
*/
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private int id;// 学生编号
private String name;// 学生姓名
private String num;// 学生学号
private int sex;// 学生性别 1为男 0为女
public Student() {
}
public Student(int id, String name, String num, int sex) {
this.id = id;
this.name = name;
this.num = num;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student:@id:" + this.id + "\t@name:" + this.name + "\t@num:"
+ this.num + "\t@sex:" + ((this.sex == 0) ? "女" : "男");
}
}
复制代码
学生对象操作接口类
<PRE style="LINE-HEIGHT: normal; WIDOWS: 2; TEXT-TRANSFORM: none; FONT-VARIANT: normal; FONT-STYLE: normal; TEXT-INDENT: 0px; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); FONT-WEIGHT: normal; WORD-SPACING: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">package com.linguofeng.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeMap;
import com.linguofeng.dao.StudentDao;
import com.linguofeng.dao.impl.StudentDaoImpl;
import com.linguofeng.model.Student;
public class StudentMain {
static StudentDao dao;
static BufferedReader br;
public static void main(String[] args) {
/** 启动时判断数据文件是否存在,如不存在则新建. */
File file = new File("student.dat");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
dao = new StudentDaoImpl();
/** 获得控制台输入流 */
br = new BufferedReader(new InputStreamReader(System.in));
/** 进行死循环 */
while (true) {
System.out.println(getLine());
System.out.println("=[0]主菜单\t\t\t\t\t\t=");
System.out.println("=[1]查找学生\t\t\t\t\t\t=");
System.out.println("=[2]增加学生\t\t\t\t\t\t=");
System.out.println("=[3]删除一个学生\t\t\t\t\t=");
System.out.println("=[4]删除所有学生\t\t\t\t\t=");
System.out.println("=[5]学生列表\t\t\t\t\t\t=");
System.out.println("=[6]更新学生\t\t\t\t\t\t=");
System.out.println("=[x]退出\t\t\t\t\t\t=");
System.out.println(getLine());
System.out.print("请选择操作:");
/** 获得控制台输入的内容 */
String input = getInput();
if (input.equals("")) {
input = "x";
}
switch (input.charAt(0)) {
case '2' :
addStu();
break;
case 'x' :
System.out.println("退出成功,欢迎再次使用!");
System.exit(0);
break;
case '5' :
findAll();
break;
case '4' :
delAll();
break;
case '3' :
delByNum();
break;
case '1' :
findByNum();
break;
case '6' :
updateByNum();
break;
default :
System.out.println("不支持的系统指令,请重新输入!");
break;
}
}
}
/**
* 根据学生学号查询学生对象信息
*/
public static void findByNum() {
System.out.print("请输入要查询的学生学号:");
while (true) {
String input = getInput();
if (dao.findByNum(input) != null) {
System.out.println(dao.findByNum(input));
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("查询失败,不存在该学生,请重新输入要删除的学生学号:");
}
}
}
/**
* 根据学生学号删除学生对象
*/
public static void delByNum() {
System.out.println(getLine());
findAll();/** 显示所有学生信息方便进行删除操作 */
System.out.println(getLine());
System.out.println("如需取消留空直接按回车.");
System.out.print("请输入要删除的学生学号:");
while (true) {
String input = getInput();
if (dao.delByNum(input)) {
System.out.println("删除成功!");
findAll();
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("删除失败,不存在该学生,请重新输入要删除的学生学号:");
}
}
}
/**
* 学生列表
*/
public static void findAll() {
TreeMap<String, Student> stuMap = dao.findAll();
Iterator<Student> iterator = stuMap.values().iterator();
if (!iterator.hasNext()) {
System.out.println("数据库为空");
}
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
/**
* 删除所有学生对象
*/
public static void delAll() {
while (true) {
System.out.print("是否要删除所有学生信息,确定请输入YES,否请输入NO:");
String input = getInput();
if (input.equals("YES") || input.equals("yes")) {
dao.delAll();
System.out.println("清空成功");
return;
} else if (input.equals("NO") || input.equals("no")) {
return;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
/**
* 添加学生
*/
public static void addStu() {
while (true) {
System.out.print("请输入要添加学生的个数:");
String ch = null;
try {
ch = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (ch.equals("")) {
System.out.println("输入有误,请重新输入!");
} else {
int size = Integer.parseInt(ch);
TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
for (int i = 0; i < size; i++) {
Student student = new Student();
try {
System.out.print("请输入第 " + (i + 1) + " 个学生的姓名:");
student.setName(br.readLine());
System.out.print("请输入第 " + (i + 1) + " 个学生的学号:");
student.setNum(br.readLine());
System.out.print("请输入第 " + (i + 1)
+ " 个学生的性别(男输:1;女输:0):");
student.setSex(Integer.parseInt(br.readLine()));
} catch (IOException e) {
e.printStackTrace();
}
stuMap.put(student.getNum(), student);
}
dao.addStu(stuMap);
findAll();
return;
}
}
}
public static void updateByNum() {
findAll();
System.out.print("请输入要修改的学生学号:");
while (true) {
String input = getInput();
String num = input;
Student student = dao.findByNum(num);
if (student != null) {
System.out.println(student);
System.out.println("如不修改直接留空按回车!");
System.out.print("请输入学生的新姓名:");
input = getInput();
if (!input.equals(""))
student.setName(input);
System.out.print("请输入学生的新学号:");
input = getInput();
if (!input.equals(""))
student.setNum(input);
System.out.print("请输入学生的新版性别(男输:1;女输:0):");
input = getInput();
if (!input.equals(""))
student.setSex(Integer.parseInt(input));
dao.updateByNum(num, student);
System.out.println("更新成功,更新后的数据:");
System.out.println(dao.findByNum(student.getNum()));
return;
} else if (input.equals("")) {
return;
} else {
System.out.print("查询失败,不存在该学生,请重新输入要修改的学生学号:");
}
}
}
public static void addTest() {
TreeMap<String, Student> stuMap = new TreeMap<String, Student>();
for (int i = 0; i < 10000; i++) {
Student student = new Student(i, "A" + i, "NUM" + i, 0);
stuMap.put(student.getNum(), student);
}
dao.addStu(stuMap);
return;
}
/**
* 获得===========分割符
*
* @return
*/
public static String getLine() {
return "=========================================================";
}
/**
* 获得控制台输入
*
* @return
*/
public static String getInput() {
String ch = "";
try {
ch = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}</PRE>
复制代码
作者:
滔哥
时间:
2013-3-8 19:44
学生对象操作实现类
package com.linguofeng.dao.impl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.TreeMap;
import com.linguofeng.dao.StudentDao;
import com.linguofeng.model.Student;
/**
* 学生对象操作实现类
*
* @author 林国锋
* @since 2011-9-24 {@link http://www.linguofeng.com}
*
*/
public class StudentDaoImpl implements StudentDao {
private static final String file = "student.dat";
/**
* 添加学生
*/
@Override
public void addStu(TreeMap<String, Student> stuMap) {
/** 把原来的数据也加入去 */
Iterator<Student> iterator = findAll().values().iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
stuMap.put(student.getNum(), student);
}
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(file);
out = new ObjectOutputStream(fos);
Iterator<Student> it = stuMap.values().iterator();
while (it.hasNext()) {
out.writeObject(it.next());
}
out.writeObject(null);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 查找所有学生对象
*/
@Override
public TreeMap<String, Student> findAll() {
FileInputStream fis = null;
ObjectInputStream in = null;
TreeMap<String, Student> stuMap = null;
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
Student student = null;
stuMap = new TreeMap<String, Student>();
while ((student = (Student) (in.readObject())) != null) {
stuMap.put(student.getNum(), student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return stuMap;
}
/**
* 删除所有学生对象
*/
@Override
public void delAll() {
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(file);
out = new ObjectOutputStream(fos);
out.writeObject(null);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 更新学生对象
*/
@Override
public void updateStu(TreeMap<String, Student> stuMap) {
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(file);
out = new ObjectOutputStream(fos);
Iterator<Student> it = stuMap.values().iterator();
while (it.hasNext()) {
out.writeObject(it.next());
}
out.writeObject(null);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public boolean delByNum(String num) {
boolean flag = false;
TreeMap<String, Student> stuMap = findAll();
if (stuMap.remove(num) != null) {
updateStu(stuMap);
flag = true;
}
return flag;
}
@Override
public Student findByNum(String num) {
return findAll().get(num);
}
@Override
public void updateByNum(String num, Student student) {
TreeMap<String, Student> stuMap = findAll();
stuMap.get(num).setId(student.getId());
stuMap.get(num).setName(student.getName());
stuMap.get(num).setNum(student.getNum());
stuMap.get(num).setSex(student.getSex());
updateStu(stuMap);
}
}
复制代码
作者:
滔哥
时间:
2013-3-8 19:44
自己占楼,Mark
作者:
滔哥
时间:
2013-3-8 19:45
占楼
作者:
李培根
时间:
2013-3-8 19:47
好长的代码,占楼再看
作者:
余勇
时间:
2013-3-8 19:49
还可以这样
作者:
贾文泽
时间:
2013-3-8 19:55
刁炸了
作者:
猫腻
时间:
2013-3-8 19:56
余勇 发表于 2013-3-8 19:49
还可以这样
;P
作者:
Gaara
时间:
2013-3-8 20:52
叼炸了
作者:
小丑的媳妇2
时间:
2013-3-12 23:57
{:soso_e179:}
作者:
朱盛文
时间:
2013-3-13 01:37
{:soso_e179:}{:soso_e179:}{:soso_e179:}
作者:
aljdpoh
时间:
2013-6-20 04:00
好长代码。只是看了会发现DAO接口好像没发出来,请涛哥指点在哪个位置。
作者:
Candy
时间:
2013-7-18 14:40
好东西啊,学校正在要做这个,嘿嘿。
作者:
凌晨丶草未眠
时间:
2013-8-2 17:33
滔哥 发表于 2013-3-8 19:44
自己占楼,Mark
能不能把项目压缩啊?我复制的还有地方不对
作者:
凌晨丶草未眠
时间:
2013-8-2 17:34
滔哥 发表于 2013-3-8 19:44
自己占楼,Mark
能不能把项目压缩啊?我复制的还有地方不对
作者:
suiyuan_lin
时间:
2013-9-23 22:34
不错,赞一个
作者:
Alone_G4ykq
时间:
2015-11-11 19:49
不错,又一种想法,
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2