您的位置:首页 > 编程语言 > Java开发

java学生管理系统代码

2018-05-02 21:24 169 查看

//导入的包名

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

 

//学生管理系统类

public class StudentManager {

public static void main(String[] args) throws IOException {

ArrayList<Student> array = new ArrayList<Student>();//定义一个集合,用于接收学生对象的信息。

Scanner sc = new Scanner(System.in);//定义Scanner对象用于接收指令信息。

System.out.println("----------欢迎来到学生管理系统----------");

//建立循环使程序能够循环进行。

while(true){

显示();//包含指令信息已经对应的操作。

System.out.println("请输入一个数字进行操作:");

int num = sc.nextInt();//根据提示,录入指令。

switch(num){//根据指令,选择需要进行的操作。

case 1:

查看学生(array);//具体如方法名

break;

case 2:

添加学生(array);//具体如方法名

break;

case 3:

修改学生(array);//具体如方法名

break;

case 4:

删除学生(array);//具体如方法名

break;

case 5:

是否保存信息(array);//具体如方法名

System.out.println("系统已退出!");

System.exit(0);//退出系统的代码。

break;

default:

System.out.println("你输入有误请重新输入!");

break;

}

}

}

public static void 是否保存信息(ArrayList<Student> array) throws IOException {//退出系统提示是否保存信息。

Scanner sc = new Scanner(System.in);//定义Scanner对象用于接收指令信息。

System.out.println("是否保留本次操作的结果!(输入1保存,输入2不保存):");

//建立循环使程序在输入有问题的时候能够循环进行。

while(true){

int num =sc.nextInt();//获取指令信息。

if(num==1){//当指令为1时,保存此次操作的结果。

System.out.println("请输入保存数据的文件路径:");//选择一个保存文件的路径。

String f = sc.next();

BufferedWriter bw = new BufferedWriter(new FileWriter(f));//建立对象用于接收数据。

bw.write("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"户籍");//文档抬头。

bw.newLine();//换行

for (int i = 0; i < array.size(); i++) {//建立for循环,将集合中的数据循环录入到文档中。

Student s = array.get(i);//得到集合中i索引的对象。

bw.write(s.get学号()+"\t"+s.get姓名()+"\t"+s.get年龄()+"\t"+s.get户籍());//将集合中i索引的对象的信息数据录入到文档中。

bw.newLine();//换行

}

bw.close();//结束输入。

break;//跳出while循环,从而结束方法。

}else if(num==2){

break;//当不保存操作结果的时候,不用进行任何操作,跳出循环即可。

}else{

System.out.println("你输入有误!请重新输入(输入1保存,输入2不保存):");//输入错误的提示。

}

}

}

public static void 显示() {//包含指令信息已经对应的操作。

System.out.println("1.输入1查看(查看系统中所有的学生信息);");

System.out.println("2.输入2添加(添加学生信息到系统中);");

System.out.println("3.输入3修改(根据学号修改学生信息);");

System.out.println("4.输入4删除(根据学号删除学生信息);");

System.out.println("5.输入5退出。");

}

public static void 删除学生(ArrayList<Student> array) throws IOException {//按照录入与导入的方法修改集合当中学生的信息。

Scanner sc = new Scanner(System.in);//定义Scanner对象用于接收指令信息。

System.out.println("请输入删除的方法(1、导入  2、手动输入):");

while(true){

int num = sc.nextInt();

if(num==2){//手动输入。

System.out.println("请输入学生学号:");

String code = sc.nextLine();//学号。

int index = 判断(array, code);

if(index!=-1){//当索引值为true的时候,即集合中存在输入的学号的时候,根据该对象的索引删除该对象。

array.remove(index);

System.out.println("删除成功!");

}else{

System.out.println("系统中不存在该学号,请重新输入!");//不存在就循环录入。

}

}else if(num==1){//导入。

System.out.println("请输入需要删除数据的文件路径:");//选择一个文件的路径。

String filename = sc.next();

BufferedReader br = new BufferedReader(new FileReader (filename));//建立导入数据的对象

String line="";//定义字符串用于接收整行数据。

int sum = 0 ;//用于接收删除的学生总数。

while((line = br.readLine())!=null){//判断读取的数据是否为空。

String[] s = line.split(" ");//利用空格分割字符串得到Student对象的信息。

//下面的方法同上面手动录入的过程。

int index = 判断(array, s[0]);

if(index!=-1){

array.remove(index);

sum++ ;

}else{

System.out.println("学号'"+s[0]+"不存在");

}

}

br.close();

System.out.println("成功删除了"+sum+"位同学的信息!");

break;

}else{

System.out.println("你输入有误,请重新选择删除的方法(1、导入  2、手动输入):");

}

}

}

public static void 修改学生(ArrayList<Student> array) throws IOException {//按照录入与导入的方法修改集合当中学生的信息。

Scanner sc = new Scanner(System.in);

System.out.println("请输入修改的方法(1、导入  2、手动输入):");

while(true){

int num = sc.nextInt();

if(num==1){

System.out.println("请输入需要修改数据的文件路径:");

String filename = sc.next();

BufferedReader br = new BufferedReader(new FileReader (filename));

String line="";

int sum = 0 ;

//while循环判断是否录入完毕。

while((line = br.readLine())!=null){

String[] s = line.split(" ");

//for循环判断是否存在录入的学号

int index = 判断(array, s[0]);

if(index==-1){

System.out.println("学号'"+s[0]+"不存在");

}else{

Student st = new Student(s);

array.set(index,st);

sum++ ;

}

}

br.close();

System.out.println("成功修改了"+sum+"位同学的信息!");

break;

}else if(num==2){

System.out.println("请输入学生学号:");

String code = sc.nextLine();

//for循环判断是否存在录入的学号

int index = 判断(array, code);

if(index==-1){

System.out.println("系统中不存在学号'"+code+"',请重新输入!");

}else{

System.out.println("请输入学生新姓名:");

String name = sc.next();

System.out.println("请输入学生新年龄:");

String age = sc.next();

System.out.println("请输入学生新户籍:");

String place=sc.next();

Student s = new Student(code,name,age,place);

array.set(index,s);

System.out.println("修改成功!");

break;

}

}else{

System.out.println("你输入有误,请重新选择修改的方法(1、导入  2、手动输入):");

}

}

}

public static void 查看学生(ArrayList<Student> array) {//将集合中的数据以特定格式遍历出来。

if(array.isEmpty()==true){

System.out.println("系统中还未存储学生信息,请你添加之后再查看!");

}else{

System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"户籍");

for (int i = 0; i < array.size(); i++) {

Student s = array.get(i);

System.out.println(s.get学号()+"\t"+s.get姓名()+"\t"+s.get年龄()+"\t"+s.get户籍());

}

}

}

public static void 添加学生(ArrayList<Student> array) throws IOException {//按照录入与导入的方法添加学生的信息到集合中。

Scanner sc = new Scanner(System.in);

System.out.println("请输入添加的方法(1、导入  2、手动输入):");

while(true){

int num = sc.nextInt();

if(num==1){

System.out.println("请输入需要导入数据的文件路径:");

String filename = sc.next();

BufferedReader br = new BufferedReader(new FileReader (filename));

String line="";

int sum = 0;

//while循环判断是否录入完毕。

while((line = br.readLine())!=null){

String[] s = line.split(" ");

int index = 判断(array, s[0]);

if(index!=-1){

System.out.println("学号'"+s[0]+"'在系统中已存在!");

}else{

Student st = new Student(s);

array.add(st);//将对象存入集合。

sum++;

}

}

br.close();

System.out.println("成功添加了"+sum+"位同学的信息!");

return;

}else if(num==2){

System.out.println("请输入学生学号:");

String code = sc.next();

int index = 判断(array, code);

if(index!=-1){

System.out.println("学号'"+code+"'已经存在,请重新输入!");

}else{

System.out.println("请输入学生姓名:");

String name = sc.next();

System.out.println("请输入学生年龄:");

String age = sc.next();

System.out.println("请输入学生户籍:");

String place=sc.next();

Student s = new Student(code,name,age,place);

array.add(s);//将对象存入集合。

System.out.println("添加成功!");

return;

}

}else{

System.out.println("你输入有误,请重新选择添加的方法:");

}

}

}

public static int 判断(ArrayList<Student> array, String code) {//判断集合中是否存在与code相同的学号,如果存在,返回值为索引值,不存在返回值为-1。

for (int i = 0; i < array.size(); i++) {//遍历集合。

if (array.get(i).get学号().equals(code)){//判断集合中是否存在与code相同的学号,如果存在,同时得到索引值。

return i ;

}

}

return -1;//不存在返回值为-1。

}

}

 

//学生类

public class Student {

private String 学号 ;

private String 姓名 ;

private String 年龄 ;

private String 户籍 ;

public Student() {

}

public Student(String[] s) {

this.学号 = s[0];

this.姓名 = s[1];

this.年龄 = s[2];

this.户籍 = s[3];

}

public Student(String 学号, String 姓名, String 年龄, String 户籍) {

this.学号 = 学号;

this.姓名 = 姓名;

this.年龄 = 年龄;

this.户籍 = 户籍;

}

public String get学号() {

return 学号;

}

public void set学号(String 学号) {

this.学号 = 学号;

}

public String get姓名() {

return 姓名;

}

public void set姓名(String 姓名) {

this.姓名 = 姓名;

}

public String get年龄() {

return 年龄;

}

public void set年龄(String 年龄) {

this.年龄 = 年龄;

}

public String get户籍() {

return 户籍;

}

public void set户籍(String 户籍) {

this.户籍 = 户籍;

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: