简介
制作一个简单的学生管理系统,通过键盘选择操作进行添加学生(学 号,姓名,性别,年龄)的信息,这些信息存储在一个文件里面,同时还可以进行 查询全体学生,修改学生信息,删除学生的操作, 要求按照学 生年龄从大到小排序。
代码
package com.softeem.project4;
public class Student {
private int no;
private String name;
private String sex;
private int age;
public Student() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(int no, String name, String sex, int age) {
super();
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "no=" + no + "\t" + "name=" + name + "\t" + " sex=" + sex + "\t" + " age=" + age;
}
}
package com.softeem.project4;
import java.util.ArrayList;
public class StudentManage {
static ArrayList<Student> list = new ArrayList<>();
public void addS(Student s) {
list.add(s);
}
public ArrayList<Student> findAll() {
list.sort((a, b) -> a.getAge() - b.getAge());
return list;
}
public Student find(int no) {
Student stu = null;
for (Student s : list) {
if (s.getNo() == no) {
stu = s;
break;
}
}
return stu;
}
public boolean modify(int no, Student s) {
Student stu = find(no);
if (stu != null) {
stu.setNo(s.getNo());
stu.setName(s.getName());
stu.setSex(s.getSex());
stu.setAge(s.getAge());
return true;
}
return false;
}
public boolean delete(int no) {
Student stu = find(no);
if (stu != null) {
return list.remove(stu);
}
return false;
}
}
package com.softeem.project4;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
private StudentManage sm = new StudentManage();
private Scanner sc;
public void menu() throws IOException {
msg("*****1.添加学生*****2.修改学生*****3.查询全体学生*****4.删除学生*****5.退出*****");
msg("请选择操作:");
start();
}
private void start() throws IOException {
sc = new Scanner(System.in);
String i = sc.next();
switch (i) {
case "1":
add();
break;
case "2":
update();
break;
case "3":
list();
break;
case "4":
delete();
break;
case "5":
exit();
break;
default:
msg("请输入正确的操作指令!!!");
break;
}
menu();
}
private void exit() {
sc = new Scanner(System.in);
msg("是否确定退出?(Y/N)");
String op = sc.next();
if (op.equalsIgnoreCase("y")) {
msg("再你妈的见");
System.exit(1);
}
}
private void delete() throws IOException {
sc = new Scanner(System.in);
msg("请输入学生学号:");
int no = sc.nextInt();
if (sm.delete(no)) {
msg("删除成功!");
addFile(sm.findAll());
} else {
msg("删除失败!");
}
}
private void list() {
msg("学号\t姓名\t性别\t年龄");
for (Student s : sm.findAll()) {
msg(s);
}
}
private void update() throws IOException {
sc = new Scanner(System.in);
msg("请输入学生学号:");
int s1 = sc.nextInt();
if (sm.find(s1) == null) {
msg("该学生不存在,请重新输入");
update();
return;
}
msg("请输入学生姓名:");
String s2 = sc.next();
msg("请输入学生性别:");
String s3 = sc.next();
msg("请输入学生年龄:");
int s4 = sc.nextInt();
Student s = new Student(s1, s2, s3, s4);
if (sm.modify(s1, s)) {
msg("修改成功!");
addFile(sm.findAll());
} else {
msg("修改失败!");
}
}
private void add() throws IOException {
msg("请输入学生学号:");
sc = new Scanner(System.in);
int s1 = sc.nextInt();
if (sm.find(s1) != null) {
msg("该学生已存在,请重新输入");
add();
return;
} else {
msg("请输入学生姓名:");
String s2 = sc.next();
msg("请输入学生性别:");
String s3 = sc.next();
msg("请输入学生年龄:");
int s4 = sc.nextInt();
Student s = new Student(s1, s2, s3, s4);
sm.addS(s);
addFile(sm.findAll());
msg("添加成功");
}
}
public void addFile(ArrayList<Student> list) throws IOException {
list.sort((a, b) -> a.getAge() - b.getAge());
FileOutputStream fos = new FileOutputStream("src/student");
for (Student stu : list) {
fos.write((stu.toString() + "\n").getBytes());
}
fos.close();
}
public void msg(Object obj) {
System.out.println(obj);
}
public static void main(String[] args) throws IOException {
new Test().menu();
}
}
运行结果
*****1.添加学生*****2.修改学生*****3.查询全体学生*****4.删除学生*****5.退出*****
请选择操作:
总结
上面代码还没有实现从文件里读取数据并显示到控制台的功能,只是在控制台中展示了数据,并且将展示的数据写入到文件中了