由于现阶段学习的知识有限,还没有学习GUI界面设置和数据库,故只有用数组来存储.......只设置了教师和学生两个类的知识.....
只实现了教师注册、登录管理学生姓名,学号,性别,年龄的添加、删除、查询功能
运行主程序:
public class Manage {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.teacherDemo();
}
}
学生类
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student {
private String name;// 学生姓名
private String number;// 学生学号
public static List<Student> students = new ArrayList<Student>();
public Student() {
}
public Student(String name, String number) {
this.name = name;
this.number = number;
}
// 添加学生功能的操作
public void addStuInfo() {
System.out.println("请输入要添加的学生的学号和姓名,学号和姓名以回车键分开!");
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入学号:");
number = sc.next();
System.out.println("请输入姓名:");
name = sc1.next();
Student stu = new Student(name, number);
students.add(stu);
System.out.println("姓名为:" + name + "\t学号为:" + number + "\t的学生信息已成功录入!");
}
// 删除学生功能的操作
public void delete() {
System.out.println("请输入要删除的学生的学号:");
Scanner sc = new Scanner(System.in);
String stuno = sc.next();
for (int m = 0; m < students.size(); m++) {
Student stu = (Student) students.get(m);
if (stu.number.equals(stuno)) {
students.remove(m);
System.out.println("该学生信息已成功删除!");
}
}
}
// 查找学生功能的操作
public void search() {
System.out.println("请输入要查询的学生学号:");
Scanner sc = new Scanner(System.in);
String stuno = sc.next();
int m = 0;
Student stu = null;
if (students.size() > 1) {
for (int k = 0; k < students.size(); k++) {
stu = students.get(k);
if (stu.number.equals(stuno)) {
m++;
break;
}
}
}
if (m < students.size()) {
System.out.println("姓名为:" + stu.name + "的学生已经找到!");
} else if (m == students.size()) {
System.out.println("对不起,没有找到姓名为:" + stu.name + "的学生信息!");
}
}
// 列出学生列表功能的操作
public void list() {
System.out.println("学号 姓名");
for (int m = 0; m < students.size(); m++) {
Student stu = (Student) students.get(m);
System.out.println(stu.number + " " + stu.name);
}
}
// 结束退出功能的操作
public void exit() {
System.out.println("程序结束");
System.exit(0);
}
// 进入学生对象操作界面
public void studentDemo() {
Student s = new Student();
int x;
System.out.println("==欢迎进入学生管理操作==");
System.out.println("==请输入对应的数字进行操作==");
System.out.println("1、新增学生,2、删除学生,3、按学号查询学生,4、查看学生列表,5、退出系统");
for (int i = 1;; i++) {
Scanner sc1 = new Scanner(System.in);
x = sc1.nextInt();
switch (x) {
case 1:
s.addStuInfo();
break;
case 2:
s.delete();
break;
case 3:
s.search();
break;
case 4:
s.list();
break;
case 5:
s.exit();
break;
default:
break;
}
}
}
}
教师类
package com.lx.java.shuju;
import java.util.ArrayList;
import java.util.Scanner;
public class Teacher {
public String name;// 教师姓名
public String id;// 登陆账号
public int passWord;// 登陆密码
ArrayList<Teacher> mlist = new ArrayList<Teacher>();
public Teacher() {
}
public Teacher(String name, String id, int passWord) {
this.name = name;
this.id = id;
this.passWord = passWord;
}
// 教师注册的操作
public void register() {
Scanner in = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = in.next();
System.out.println("请输入登陆账号:");
String id = in.next();
System.out.println("请输入登陆密码:");
int passWord = in.nextInt();
Teacher teacher = new Teacher(name, id, passWord);
mlist.add(teacher);
}
// 教师登陆的操作
public void login() {
Scanner scaner = new Scanner(System.in);
System.out.println("请输入登录账号");
String id = scaner.next();
System.out.println("请输入登录密码:");
int passWord = scaner.nextInt();
boolean flag = check(id, passWord);
if (flag) {
System.out.println("登录成功!");
Student s=new Student();
s.studentDemo;
} else {
System.out.println("您输入的账号错误,请重新输入!");
login();
}
}
// 检查登陆账号密码是否正确
public boolean check(String id, int passWord) {
boolean flag = false;// 如果为true表示没有找到,false表示找到</p><p> int
int count = 0;
// count = mlist.size(); //
// 获取ArrayList集合对象mList中元素个数
for (int i = 0; i < count; i++) {
Teacher teacher = mlist.get(i);
if (id.equals(teacher.id) && passWord == teacher.passWord) {
flag = true;
break;
}
}
return flag;
}
// 教师注册,登陆界面
public void teacherDemo() {
Teacher teacher = new Teacher();
System.out.println("==欢迎进入注册、登陆操作==");
System.out.println("==请输入对应的数字进行操作==");
System.out.println("1、注册账号,2、登陆系统");
int y;
for (int j = 1;; j++) {
Scanner sc = new Scanner(System.in);
y = sc.nextInt();
switch (y) {
case 1:
teacher.register();
System.out.println("1、注册账号,2、登陆系统");
break;
case 2:
teacher.login();
break;
default:
break;
}
}
}
}
有不足的地方请见谅