亲宝软件园·资讯

展开

用java实现学生信息管理系统

人气:0

用java写的学生信息管理系统,供大家参考,具体内容如下

使用到了集合类ArrayLisat 来对Student类的对象进行存储。

StudentManagerTest为主类,Student类里面进行了相应数据的封装。里面用了很多循环来写,并且运用了很多标记来进行判断是否退出循环,如flag和index。

创建集合类对象格式:

ArrayList array= new ArrayList();在传递方法的时候需要在形参列表中用<>对传递的类型进行明确

主类

package zjh;
 import java.util.ArrayList;
 import java.util.Scanner;
 
 public class StudentManageTest {
 public static void main(String[] args) {
 ArrayList<Student> array = new ArrayList<Student>();
 
 while(true) {
 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:退出");
 
 Scanner scanner = new Scanner(System.in);
 
 String choice = scanner.nextLine();
 


 switch (choice) {
 case "1":
  findAllStudents(array);
  break;
 case "2":
  addStudent(array);
  break;
 case "3":
  deleteStudent(array);
  break;
 case "4":
  updateStudent(array);
  break;
 case "5":
 default:
  System.out.println("正在退出系统,欢迎下次继续使用");
  break;
 }
 
 }
 }
 //修改学生信息
 public static void updateStudent(ArrayList<Student> array) {
 Scanner scanner = new Scanner(System.in);
 while(true) {
 System.out.println("请输入你要修改的学号:");
 String id = scanner.nextLine();
 int index = -1;
 for(int x=0; x<array.size(); x++) {
  Student student = array.get(x);
  if(student.getId().equals(id)) {
  index = x;
  break;
  }
 }
 if(index ==-1) {
  System.out.println("您输入的学号有误请重新输入");
  continue;
 }else {
  System.out.println("请输入新的姓名:");
  String name = scanner.nextLine();
  System.out.println("请输入新的年龄:");
  String age = scanner.nextLine();
  System.out.println("请输入新的地址");
  String address = scanner.nextLine();
  
  Student student = new Student();
  student.setId(id);
  student.setName(name);
  student.setAge(age);
  student.setAddress(address);
  
  array.set(index, student);
  break;
  }
 }
 System.out.println("修改学生成功");
 
 }
 
 //删除学生信息
 public static void deleteStudent(ArrayList<Student> array) {
 Scanner scanner = new Scanner(System.in);
 while(true) {
 System.out.println("请输入你要删除的学号");
 
 String id = scanner.nextLine();
 int index = -1;
 
   for(int x=0; x<array.size(); x++) {
   Student student = array.get(x);
   if (student.getId().equals(id)) {
    index = x;
    break;
   }
   }
   if(index == -1) {
   System.out.println("您输入的学号有误 请重新输入");
   continue;
   }else {
   array.remove(index);
   break;
   }
 }
 System.out.println("删除学生信息成功!");
 }
 
 //添加学生信息
 public static void addStudent(ArrayList<Student> array) {
 Scanner scanner = new Scanner(System.in);
 String id;
 while(true) {
 System.out.println("请输入你要添加的学号:");
 int flag =0;
 id = scanner.nextLine();
 for(int x=0; x<array.size(); x++) {
  Student student =array.get(x);
  if(student.getId().equals(id)) {
  System.out.println("你输入的学号已被占用,请重新输入");
  break;
  }else {
  flag++;
  }
  }
 if(flag==array.size()) {
  break;
 }
 }
 System.out.println("请输入你要添加的姓名:");
 String name = scanner.nextLine();
 System.out.println("请输入你要添加的年龄:");
 String age = scanner.nextLine();
   System.out.println("请输入你要添加的地址:");
   String address = scanner.nextLine();
 Student student = new Student();
 student.setId(id);
 student.setName(name);
 student.setAge(age);
 student.setAddress(address);
 
 array.add(student);
 
 
 System.out.println("添加信息成功");
 
 }

 
 //查看所有学生信息
 public static void findAllStudents(ArrayList<Student> array) {
 if(array.size()==0) {
  System.out.println("当前没有任何学生信息,请添加后再使用");
 }
 System.out.println("学号\t\t姓名\t年龄\t居住地");
 for(int x=0; x<array.size(); x++) {
  Student s = array.get(x);
  System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
 }
 }
}

Student类

package zjh;
 public class Student {
 private String id;
 private String name;
 private String age;
 private String address;
 public Student() {
 
 }
 public Student(String id, String name, String age, String address) {
 this.id = id;
 this.name = name;
 this.age = age;
 this.address = address;
 }
 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 String getAge() {
 return age;
 }
 public void setAge(String age) {
 this.age = age;
 }
 public String getAddress() {
 return address;
 }
 public void setAddress(String address) {
 this.address = address;
 }
 }

接下来的是进行实际操作的一些样例展示

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论