这个系统只是一些简单的操作,包含一个学生类,和一个测试类
学生类包含成员变量和成员方法,成员变量用private私有
测试类里包含最基本的增删改查的操作,
学生类
public class SoutdenDemo {
private String sid;
private String name;
private String age;
private String address;
public SoutdenDemo() {
}
public SoutdenDemo(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;
}
}
测试类
import java.util.ArrayList;
import java.util.Scanner;
public class Tset {
//主界面
public static void main(String[] args) {
//为了防止循环重复执行,集合会被重新创建,导致数据丢失,所以必须把集合定义在循环的外面!
ArrayList list = new ArrayList<>();
String nserName = "账户";
String mi = "123456";
String yanZhenMa = "abc";
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的账户");
String s = sc.nextLine();
System.out.println("请输入你的密码");
String s1 = sc.nextLine();
System.out.println("请输入你的验证码");
String s2 = sc.nextLine();
if (s.equalsIgnoreCase(nserName)&&s1.equalsIgnoreCase(mi)&&s2.equalsIgnoreCase(yanZhenMa)){
System.out.println("登录成功");
break;
}else if (2-i==0){
System.out.println("你的账户已被封");
System.exit(0);//退出
}else {
System.out.println("你还有"+(2-i)+"次机会");
}
}
//编写主界面
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 退出");
//通过键盘录入我的选择
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的选择");
String line = sc.nextLine();
switch (line){
case "1":
//System.out.println("添加学生");
addStudent(list);
break;
case "2":
//System.out.println("删除学生");
deleteStudent(list);
break;
case "3":
//System.out.println("修改学生");
updateStudent(list);
break;
case "4":
//System.out.println("查看所有学生");
findStudent(list);
break;
case "5":
System.out.println("欢迎再次使用");
//break;
return;
default:
System.out.println("你输入的有误");
break;
}
}
}
//添加学生
public static void addStudent(ArrayList list){
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的学号");
String sid = sc.nextLine();
//遍历集合
for (int i = 0; i < list.size(); i++) {
SoutdenDemo soutdenDemo = list.get(i);
if (soutdenDemo.getSid().equals(sid)){
System.out.println("你输入的学号已经存在,请确认后再输");
return;
}
}
System.out.println("请输入学生的姓名:");
String name = sc.nextLine();
System.out.println("请输入学生的年龄:");
String age = sc.nextLine();
System.out.println("请输入学生的居住地址:");
String address = sc.nextLine();
//把学生的信息封装到学生对象中
SoutdenDemo stu = new SoutdenDemo(sid, name, age, address);
//把学生对象添加到集合中
list.add(stu);
System.out.println("添加学生信息成功!");
}
///查询学生
public static void findStudent(ArrayList list){
if (list.size()==0){
System.out.println("当前没有学生信息,请回到主界面添加学生!");
}else {
System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");
//遍历集合
for (int i = 0; i < list.size(); i++) {
SoutdenDemo soutdenDemo = list.get(i);
//把学生对象的信息打印在控制台
String sid = soutdenDemo.getSid();
String name = soutdenDemo.getName();
String age = soutdenDemo.getAge();
String address = soutdenDemo.getAddress();
System.out.println(sid+"\t"+name+"\t"+age+"岁\t"+address);
}
}
}
//删除学生
public static void deleteStudent(ArrayList list){
if (list.size()==0){
System.out.println("当前没有学生信息,请回到主界面添加学生!");
}else {
//键盘录入确认要删除的学生学号
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您要删除的学生学号");
String sid = sc.nextLine();
//获取集合中每一个学生对象
for (int i = 0; i < list.size(); i++) {
SoutdenDemo soutdenDemo = list.get(i);
if (soutdenDemo.getSid().equalsIgnoreCase(sid)){
//从集合中删除
list.remove(i);
System.out.println("学号为"+sid+"姓名是"+soutdenDemo.getName()+"删除完毕");
return;
}
}
System.out.println("您输入的学号有误,请确认学号后再次尝试!");
}
}
//修改学生
public static void updateStudent(ArrayList list){
if (list.size()==0){
System.out.println("当前没有学生信息,请回到主界面添加学生;");
}else {
//键盘录入要修改的学生学号
Scanner sc = new Scanner(System.in);
System.out.println("请输入您要修改的学生学号:");
String sid = sc.nextLine();
//遍历集合元素.去匹配有没有此sid的学生
for (int i = 0; i < list.size(); i++) {
SoutdenDemo soutdenDemo = list.get(i);
if (soutdenDemo.getSid().equalsIgnoreCase(sid)){
//若有此学生,开始录入新的学生信息
System.out.println("请输入新的学生姓名:");
String name = sc.nextLine();
System.out.println("请输入新的学生年龄");
String age = sc.nextLine();
System.out.println("请输入新的学生居住地址");
String address = sc.nextLine();
//我需要把新的学生信息封装到学生对象里
SoutdenDemo stu = new SoutdenDemo(sid, name, age, address);
//把新的学生对象替换集合中的老学生对象 集合的设置方法
list.set(i,stu);
System.out.println("学生信息修改成功!");
return;
}
}
//若没有此学号,提示请确认后再来
System.out.println("您要修改的学生不存在,请您确认学生学号后再来!");
}
}
}