学生管理系统(ArraList)
需求
实现对学生信息的增删改查
实现步骤
1.定义学生类
2.搭建主界面
3.添加学生功能实现
4.查看学生功能实现
5.删除学生功能实现
6.修改功能实现
知识点
ArraList
Scanner
代码
Student
package com.wxit;
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student() {
}
public Student(String sid, String name, String age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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;
}
}
StudentManager
package com.wxit;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args){
ArrayList<Student> array = new ArrayList<>();
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.print("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
// System.out.println("添加学生");
addStudent(array);
break;
case "2":
// System.out.println("删除学生");
deleteStudent(array);
break;
case "3":
// System.out.println("修改学生");
updateStudent(array);
break;
case "4":
// System.out.println("查看所有学生");
findAllStudent(array);
break;
case "5":
System.out.println("谢谢使用!");
System.exit(0);
}
}
}
public static void addStudent(ArrayList<Student> array){
Scanner sc =new Scanner(System.in);
String sid;
while (true){
System.out.print("请输入学生学号:");
sid =sc.nextLine();
boolean flag = isUsed(array,sid);
if(flag){
System.out.println("你的学号已经使用,请重新输入");
}else {
break;
}
}
System.out.print("请输入学生姓名:");
String name =sc.nextLine();
System.out.print("请输入学生年龄:");
String age =sc.nextLine();
System.out.print("请输入学生居住地:");
String address =sc.nextLine();
Student s =new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
public static boolean isUsed(ArrayList<Student> array,String sid){
boolean flag = false;
for(int i=0;i<array.size();i++){
Student s =array.get(i);
if(s.getSid().equals(sid)){
flag=true;
break;
}
}
return flag;
}
public static void findAllStudent(ArrayList<Student> array){
if(array.size() ==0){
System.out.println("无信息,请先添加信息再查询");
// 让程序停止
return;
}
System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
for(int i=0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getSid()+"\t"+ s.getName() +"\t\t"+s.getAge()+"岁\t"+s.getAddress());
}
}
public static void deleteStudent(ArrayList<Student> array){
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要删除的学号:");
String sid = sc.nextLine();
int index = -1;
for(int i=0;i<array.size();i++){
Student s=array.get(i);
if(s.getSid().equals(sid)){
// array.remove(i);
index = i;
break;
}
}
if(index ==-1){
System.out.println("该信息不存在,请重新输入");
}else {
array.remove(index);
System.out.println("删除学生成功!");
}
}
public static void updateStudent(ArrayList<Student> array){
Scanner sc =new Scanner(System.in);
System.out.print("请输入你要修改的学生学号:");
String sid =sc.nextLine();
System.out.print("请输入学生新姓名:");
String name =sc.nextLine();
System.out.print("请输入学生新年龄:");
String age =sc.nextLine();
System.out.print("请输入学生新住距地:");
String address =sc.nextLine();
Student s =new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
for(int i=0;i<array.size();i++){
Student student=array.get(i);
if(student.getSid().equals(sid)){
array.set(i,s);
break;
}
}
System.out.println("修改学生成功!");
}
}