使用到的知识Spring Builder,Array List,Scanner,Random
思路
Student.java
package Studentsystem;
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
StudentSystem.java
package Studentsystem;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
loop:while (true) {//给循环起个名字
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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
switch (choose) {
case 1:
addStudent(list);
break;
case 2:
deleteStudent(list);
break;
case 3:
updataStudent(list);
break;
case 4:
queryStudent(list);
break;
case 5:
System.out.println("退出系统");
break loop;//跳出while循环
// System.exit(0);//第二种写法
default:
System.out.println("无效选择,请重新输入。");
}
}
}
//添加学生
public static void addStudent(ArrayList<Student> list){
Student s = new Student();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入学生id");
String id = sc.next();
boolean flag = cheak(list, id);
if (flag){
System.out.println("id已存在,请重新输入");
break;
}else {
s.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
s.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
s.setAge(age);
System.out.println("请输入学生家庭住址");
String address = sc.next();
s.setAddress(address);
list.add(s);
//提醒用户
System.out.println("信息添加成功");
}
//删除学生
public static void deleteStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入想要删除的学生id");
String sid = sc.next();
int index = getIndex(list, sid);
if (index < 0 ){
System.out.println("id不存在,删除失败");
}else {
//找到索引直接删除
list.remove(index);
System.out.println("id为"+sid+"的学生删除成功");
}
}
//修改学生
public static void updataStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入想要修改的学生id");
String sid = sc.next();
int index = getIndex(list, sid);
if (index == -1){
System.out.println("学生id不存在");
return;
}
//取出需要修改的对象
Student stu = list.get(index);
//输入信息并进行修改
System.out.println("请输入修改的学生姓名");
String newname = sc.next();
stu.setName(newname);
System.out.println("请输入修改的学生年龄");
int newage = sc.nextInt();
stu.setAge(newage);
System.out.println("请输入修改的学生家庭住址");
String newaddress = sc.next();
stu.setAddress(newaddress);
}
//查询学生
public static void queryStudent(ArrayList<Student> list){
if(list.size() == 0){
//结束方法
System.out.println("当前没有学生信息,请添加后查询");
return;
}
//打印表头信息
System.out.println("id\t\t姓名\t年龄\t家庭住址");
//当代码到这里,证明集合中有东西
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
}
}
//判断id是否存在
public static boolean cheak(ArrayList<Student> list , String id){
// for (int i = 0; i < list.size(); i++) {
// Student stu = list.get(i);
// String sid = stu.getId();
// if (sid.equals(id)){
// return true;
// }
// }
// return false;
return getIndex(list , id) >= 0;
}
public static int getIndex(ArrayList<Student> list , String id){
for (int i = 0; i < list.size(); i++) {
//得到每一个学生对象
Student student = list.get(i);
//得到每一个id
String sid = student.getId();
//进行对比
if (sid.equals(id)){
//有的话返回索引
return i;
}
}
//没有返回-1
return -1;
}
}
后面还会写一个升级版的哦,增添了登录注册忘记密码等功能,里面有一些基本的条件用来判断是否正确。Java学生管理系统(升级版)