概述
运用集合与IO流编写简单的学生信息管理系统,实现增删改查操作。
有错误的地方请指出!!
本文链接:http://m.blog.csdn.net/article/details?id=52076179#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1
import java.text.NumberFormat;
public class Info {
private int stu_id;
private String stu_name;
private String stu_sex;
private int stu_age;
private int chinese;
private int math;
private int english;
private String stu_address;
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
public int getChilese() {
return chinese;
}
public void setChilese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public String getStu_address() {
return stu_address;
}
public void setStu_addrese(String stu_address) {
this.stu_addrese = stu_addrese;
}
public double getAvgScose(){
double avg=(math+chinese+english)/3;
NumberFormat nf=NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
avg=Double.parseDouble(nf.format(avg));
return avg;
}
public int getSunScose(){
return math+chinese+english;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;
public class ChooseDemo {
/*
*得到int类型的数据
*/
BufferedReader s=null;
public int getInputIntInfo(String info){
int input_int=0;
try{
System.out.println(info);
s=new BufferedReader(new InputStreamReader(System.in));
input_int=Integer.parseInt(s.readLine());
}catch (Exception e) {
System.out.println("输入错误!!请重新输入。");
input_int=getInputIntInfo(info);
}
return input_int;
}
/*
*得到String类型的数据
*/
public String getInputStringInfo(String string){
String input_String="";
try{
System.out.println(string);//显示输入的字符串
s=new BufferedReader(new InputStreamReader(System.in));
input_String=s.readLine();
}catch (Exception e) {
System.out.println("输入错误!!请重新输入");
input_String=getInputStringInfo(string);
}
return input_String;
}
public void menu(){
System.out.println("************************");
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("6.查询所有学生信息");
System.out.println("7.查询学生信息(根据学号)");
System.out.println("8.查询学生信息(根据姓名)");
System.out.println("9.退出系统");
System.out.println("************************");
}
public void Choose(int coad) throws Exception{
switch (coad) {
//添加学生信息
case 1:
System.out.println("<---你已进入添加学生信息界面--->");
Info add_info=getStudentInfo();
if(!(Handle.selectStuId(add_info.getStu_id()))){
if(Handle.AddInfo(add_info)){
System.out.println("n添加成功!!n");
}else{
System.out.println("n添加失败!!n");
}
}else{
System.out.println("该学号已存在!!请重新选择!");
}
break;
//修改学生信息(根据学号修改)
case 2:
System.out.println("<---你已进入修改学生信息(根据学号修改)界面--->");
int update_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(update_id)){
System.out.println("<---请输入新的学生信息--->");
Info update_infoById=getStudentInfo();
if(!(Handle.selectStuId(update_infoById.getStu_id()))||update_infoById.getStu_id()==update_id){
if(Handle.updateInfoById(update_id,update_infoById)){
System.out.println("n修改成功!!!n");
}else{
System.out.println("n修改失败!!!n");
}
}else{
System.out.println("该学号的已存在!!请重新选择!");
}
}else{
System.out.println("n没有该学号的学生信息!!请重新选择!n");
}
break;
//修改学生信息(根据姓名修改)
case 3:
System.out.println("<---你已进入修改学生信息(根据姓名修改)界面--->");
String update_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(update_name)){
System.out.println("<---请输入新的学生信息--->");
Info update_infoByName=getStudentInfo();
if(!(Handle.selectStuId(update_infoByName.getStu_id()))||update_name.equals(update_infoByName.getStu_name())){
if(Handle.updateInfoByName(update_name,update_infoByName)){//调用Handle类的updateInfoByName方法进行根据姓名修改学生信息操作
System.out.println("n修改成功!!n");
}else{
System.out.println("n修改失败!!n");
}
}else{
System.out.println("该学号的已存在!!请重新选择!");
}
}else{
System.out.println("n没有该姓名的学生信息!!请重新选择!n");
}
break;
//删除学生信息(根据学号删除)
case 4:
System.out.println("<---你已进入删除学生信息(根据学号删除)界面--->");
int delete_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(delete_id)){
if(Handle.deleteInfoById(delete_id)){
System.out.println("n删除成功!!!n");
}else{
System.out.println("n删除失败!!!n");
}
}else{
System.out.println("n没有该学号的学生信息!!请重新选择!n");
}
break;
//删除学生信息(根据姓名删除)
case 5:
System.out.println("<---你已进入删除学生信息(根绝姓名删除)界面--->");
String delete_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(delete_name)){
if(Handle.deleteInfoByName(delete_name)){
System.out.println("n删除成功!!!n");
}else{
System.out.println("n删除失败!!!n");
}
}else{
System.out.println("n没有该姓名的学生的信息!!请重新选择!n");
}
break;
//查询所有学生信息
case 6:
System.out.println("<---你已进入所有学生信息界面--->");
Handle.selectAllInfo();
break;
//查询学生信息界面(根据学号查询)
case 7:
System.out.println("<---你已进入查询学生信息界面(根据学号查询)--->");
int select_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(select_id)){
String id=Handle.selectInfoById(select_id);
if(id!=null){
}
}else{
System.out.println("n没有该学号的学生信息!!请重新选择!n");
}
break;
//查询学生信息界面(根据姓名查询)
case 8:
System.out.println("<---你已进入查询学生信息界面(根据姓名查询)--->");
String select_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(select_name)){
String name=Handle.selectInfoByName(select_name);
if(name!=null){
}
}else{
System.out.println("n没有该姓名的学生信息!!请重新选择!n");
}
break;
//退出系统
case 9:System.out.println("退出成功!");
s.close();
System.exit(0);
break;
default:
System.out.println("没有该选项!!请重新输入!");
break;
}
}
//给 Info类的学生属性进行赋值
private Info getStudentInfo(){
Info info=new Info();
info.setStu_id(getInputIntInfo("请输入学号"));
info.setStu_name(getInputStringInfo("请输入姓名"));
info.setStu_sex(getInputStringInfo("请输入性别"));
info.setStu_age(getInputIntInfo("请输入年龄"));
info.setChilese(getInputIntInfo("请输入语文成绩"));
info.setMath(getInputIntInfo("请输入数学成绩"));
info.setEnglish(getInputIntInfo("请输入英语成绩"));
info.setStu_addrese(getInputStringInfo("请输入地址"));
info.getAvgScose();//得到平均成绩
info.getSunScose();//得到总成绩
return info;
}
}
import java.util.ArrayList;
public class Handle {
private static ArrayList list=new ArrayList();//创建一个集合,用于保存数据
//添加学生信息
public static boolean AddInfo(Info add_info) throws Exception {
BufferedWriterDemo buffered=new BufferedWriterDemo();
buffered.save(add_info);
boolean add=false;
add=list.add(add_info);
return add;
}
//根据学号修改学生信息
public static boolean updateInfoById(int id, Info update_infoById) throws Exception {
boolean update=false;
BufferedUpdataDemo up=new BufferedUpdataDemo();
update=up.updataInfoById(id,update_infoById);
return update;
}
//根据姓名修改学生信息
public static boolean updateInfoByName(String name, Info update_infoByName) throws Exception {
boolean update=false;
BufferedUpdataDemo up=new BufferedUpdataDemo();
update=up.updataInfoByName(name,update_infoByName);
return update;
}
//根据学号删除学生信息
public static boolean deleteInfoById(int delete_id) throws Exception {
boolean delete=false;
BufferedDeleteDemo d=new BufferedDeleteDemo();
delete=d.deleteInfoById(delete_id);
return delete;
}
//根据姓名删除信息
public static boolean deleteInfoByName(String delete_name) throws Exception {
boolean delete=false;
BufferedDeleteDemo d=new BufferedDeleteDemo();
delete=d.deleteInfoByName(delete_name);
return delete;
}
//查询所有学生信息
public static void selectAllInfo() throws Exception {
BufferedSelectDemo read=new BufferedSelectDemo();
read.selectAllInfo();
}
//根据学号查询信息
public static String selectInfoById(int select_id) throws Exception {
String in=null;
BufferedSelectDemo read=new BufferedSelectDemo();
in=read.selectInfoById(select_id);
return in;
}
//根据姓名查询信息
public static String selectInfoByName(String select_name) throws Exception {
String in=null;
BufferedSelectDemo read=new BufferedSelectDemo();
in=read.selectInfoByName(select_name);
return in;
}
//得到学号
public static boolean selectStuId(int id) throws Exception {
boolean fag=false;
BufferedSelectDemo read=new BufferedSelectDemo();
fag=read.getStuId(id);
return fag;
}
//得到姓名
public static boolean selectStuName(String name) throws Exception{
boolean fag=false;
BufferedSelectDemo read=new BufferedSelectDemo();
fag=read.getStuName(name);
return fag;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
public class BufferedWriterDemo {
private static File file=new File("d:"+File.separator+"test1"+File.separator+"studendInfo.txt");
public void save(Info info ) throws Exception{
ArrayList list=new ArrayList();
list.add(info);
BufferedWriter save=new BufferedWriter(new FileWriter(file,true));
save.write("学号:"+String.valueOf(info.getStu_id()));
save.write("; 姓名:"+String.valueOf(info.getStu_name()));
save.write("; 年龄:"+String.valueOf(info.getStu_age()));
save.write("; 性别:"+info.getStu_sex());
save.write("; 语文成绩:"+String.valueOf(info.getChilese()));
save.write("; 数学成绩:"+String.valueOf(info.getMath()));
save.write("; 英语成绩:"+String.valueOf(info.getEnglish()));
save.write("; 平均成绩:"+String.valueOf(info.getAvgScose()));
save.write("; 总成绩:"+String.valueOf(info.getSunScose()));
save.write("; 地址:"+info.getStu_address());
save.newLine();
save.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
public class BufferedDeleteDemo {
private static File file=new File("d:"+File.separator+"test1"+File.separator+"studendInfo.txt");
private static ArrayList array=new ArrayList();
//根据学号删除信息
public boolean deleteInfoById(int delete_id) throws Exception{
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
boolean fag=false;
array.clear();
while((info=read.readLine())!=null){
array.add(info);
}
read.close();
for(Object obj:array){
info=(String)obj;
if(info.substring(info.indexOf(":")+1,info.indexOf(";")).equals(String.valueOf(delete_id))){//截取集合中的学号与输入的学号进行比较
array.remove(info);
fag=true;
break;
}
}
file.delete();
BufferedWriter write=new BufferedWriter(new FileWriter(file));
for(Object obj:array){
info=(String)obj;
write.write(info);
write.newLine();
}
write.close();
return fag;
}
//根据姓名删除学生信息
public boolean deleteInfoByName(String name) throws Exception{
boolean fag=false;
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
array.clear();
while((info=read.readLine())!=null){
array.add(info);
}
read.close();
for(Object obj:array){
info=(String)obj;
if(info.substring(info.indexOf("名")+2,info.indexOf("年")-2).equals(name)){//截取集合中的学号与输入的学号进行比较
array.remove(info);
fag=true;
break;
}
}
file.delete();
BufferedWriter write=new BufferedWriter(new FileWriter(file));
for(Object obj:array){
info=(String)obj;
write.write(info);
write.newLine();
}
write.close();
return fag;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
public class BufferedUpdataDemo {
private static ArrayList array=new ArrayList();
private static File file=new File("d:"+File.separator+"test1"+File.separator+"studendInfo.txt");
//根据学号修改信息
public boolean updataInfoById(int updata_id,Info in) throws Exception{
array.clear();
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
boolean fag=false;
while((info=read.readLine())!=null){
array.add(info);
}
read.close();
for(Object obj:array){
info=(String)obj;
String input="学号:"+in.getStu_id()+"; 姓名:"+in.getStu_name()+"; 年龄:"+in.getStu_age()+"; 性别:"+in.getStu_sex()+"; 语文成绩:"+in.getChilese()+"; 数学成绩:"+in.getMath()+"; 英语成绩:"+in.getEnglish()+"; 平均成绩:"+in.getAvgScose()+"; 总成绩:"+in.getSunScose()+"; 地址:"+in.getStu_address();
if(info.substring(info.indexOf(":")+1,info.indexOf(";")).equals(String.valueOf(updata_id))){//截取集合中的学号与输入的学号进行比较
array.set(array.indexOf(info), input);
fag=true;
break;
}
}
file.delete();
BufferedWriter write=new BufferedWriter(new FileWriter(file));
for(Object obj:array){
info=(String)obj;
write.write(info);
write.newLine();
}
write.close();
return fag;
}
//根据姓名修改学生信息
public boolean updataInfoByName(String name,Info in) throws Exception{
array.clear();
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
boolean fag=false;
while((info=read.readLine())!=null){
array.add(info);
}
read.close();
for(Object obj:array){
info=(String)obj;
String input="学号:"+in.getStu_id()+"; 姓名:"+in.getStu_name()+"; 年龄:"+in.getStu_age()+"; 性别:"+in.getStu_sex()+"; 语文成绩:"+in.getChilese()+"; 数学成绩:"+in.getMath()+"; 英语成绩:"+in.getEnglish()+"; 平均成绩:"+in.getAvgScose()+"; 总成绩:"+in.getSunScose()+"; 地址:"+in.getStu_address();
if(info.substring(info.indexOf("名")+2,info.indexOf("年")-2).equals(name)){//截取集合中的姓名与输入的学号进行比较
array.set(array.indexOf(info), input);
fag=true;
break;
}
}
file.delete();
BufferedWriter write=new BufferedWriter(new FileWriter(file));
for(Object obj:array){
info=(String)obj;
write.write(info);
write.newLine();
}
write.close();
return fag;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
public class BufferedSelectDemo {
private static File file=new File("d:"+File.separator+"test1"+File.separator+"studendInfo.txt");
private static ArrayList list=new ArrayList();
//查询所有学生信息
public void selectAllInfo() throws Exception{
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
list.clear();
while((info=read.readLine())!=null){
list.add(info);
}
read.close();
for(Object obj:list){
System.out.println(obj);
}
}
//根据学号查询学生信息
public String selectInfoById(int select_id) throws Exception{
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
//String info1=null;
ArrayList list=new ArrayList();
while((info=read.readLine())!=null){
list.add(info);
}
read.close();
for(Object obj:list){
info=(String)obj;
if(info.substring(info.indexOf(":")+1,info.indexOf(";")).equals(String.valueOf(select_id))){//截取集合中的学号与输入的学号进行比较
System.out.print("该学生信息如下n");
System.out.println(info);
break;
}
}
return info;
}
//根据姓名查询学生信息
public String selectInfoByName(String name ) throws Exception{
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
String info1=null;
ArrayList list=new ArrayList();
while((info=read.readLine())!=null){
list.add(info);
}
read.close();
for(Object obj:list){
info1=(String)obj;
if(info1.substring(info1.indexOf("名")+2,info1.indexOf("年")-2).equals(name)){//截取集合中的学号与输入的学号进行比较
System.out.print("该学生信息如下n");
System.out.println(info1);
break;
}
}
return info1;
}
//得到学号
public boolean getStuId(int id) throws Exception{
boolean fag=false;
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
ArrayList list=new ArrayList();
while((info=read.readLine())!=null){
list.add(info);
}
read.close();
for(Object obj:list){
String stu_id=(String)obj;
if(stu_id.substring(stu_id.indexOf(":")+1,stu_id.indexOf(";")).equals(String.valueOf(id))){
fag=true;
break;
}
}
return fag;
}
//得到姓名
public boolean getStuName(String name) throws Exception{
boolean fag=false;
BufferedReader read=new BufferedReader(new FileReader(file));
String info=null;
ArrayList list=new ArrayList();
while((info=read.readLine())!=null){
list.add(info);
}
read.close();
for(Object obj:list){
String stu_name=(String)obj;
if(stu_name.substring(stu_name.indexOf("名")+2,stu_name.indexOf("年")-2).equals(name)){
fag=true;
break;
}
}
return fag;
}
}
public class TestMemo {
public static void main(String[] args) throws Exception {
ChooseDemo ch=new ChooseDemo();
while(true){
ch.menu();
int choose=ch.getInputIntInfo("
<请输入数字选择操作>
");
ch.Choose(choose);
}
}
}
最后
以上就是仁爱荷花为你收集整理的集合与IO流结合编写的简单的学生信息管理系统的全部内容,希望文章能够帮你解决集合与IO流结合编写的简单的学生信息管理系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复