好久没有写过这种简单的程序了,这次写这个也主要是给一个刚学习java的兄弟看,让他了解怎么写这种简单的小项目。这个学生管理系统主要涉及的java基础知识有变量、数据类型、流程控制、数组操作、面向对象、数据封装(构造、重载)、多态、接口、集合、异常、线程(用了一个线程延时)等。语法都是用的相对简单的语法,便于初学者学习和观看。
程序结构 -----Main.java 程序入口
-----Student.java 学生实体
------StudentService.java 学生信息操作相关接口
-------StudentServiceImpl.java 学生信息操作相关接口实现类
操作:项目中创建这四个文件即可,运行Main.java 的 main 就可以了.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author pengyangyan
*/
public class Main {
/**
* 学生列表
*/
private static List students = new ArrayList();
/**
* 输入
*/
private static Scanner scanner = new Scanner(System.in);
/**
* 是否登录的标志
*/
private static boolean isLogin = false;
/**
* 当前登录的学生信息
*/
private static Student current = new Student();
private static StudentServiceImpl studentService = new StudentServiceImpl();
public static void main(String[] args) throws InterruptedException {
System.out.println("正在初始化学生信息....");
Thread.sleep(3000);
studentService.initStudentList(students);
int choose = Integer.parseInt(Menu());
while (choose<11){
switch (choose) {
case 1:
login();
break;
case 2:
regester();
break;
case 3:
studentService.printStudentList(students);
break;
case 4:
if (isLogin){
System.out.println("====>个人资料");
studentService.printMine(current);
}else {
System.out.println("您尚未登录!");
}
break;
case 5: {
if (isLogin){
System.out.println("输入需要删除的学生姓名");
String name = scanner.next();
studentService.deleteStudentInfo(students, name);
}else {
System.out.println("请先登录!");
}
}
break;
case 6:
update();
break;
case 7:
sortByScore();
break;
case 8:
sortByAge();
break;
case 9:
getGrade();
break;
case 10:
getSex();
break;
default:
System.out.println("输入错误");
break;
}
choose = Integer.parseInt(Menu());
}
}
private static String Menu() {
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("6.更新学生信息");
System.out.println("7.根据学生成绩排名");
System.out.println("8.根据学生年龄排名");
System.out.println("9.获取某个年级学生");
System.out.println("10.获取某个性别的学生");
System.out.println("11.退出");
System.out.println("==================================");
System.out.print("输入你需要选择的选项编号:");
return scanner.next();
}
/**
* 登录系统
*/
private static void login(){
System.out.print("输入学号:");
String username = scanner.next();
System.out.print("输入密码:");
String password = scanner.next();
for (Student student : students) {
if (student.getStuNo().equals(username) && student.getPassword().equals(password)){
isLogin = true;
current.setStuNo(student.getStuNo());
current.setName(student.getName());
current.setAge(student.getAge());
current.setSex(student.getSex());
current.setScore(student.getScore());
current.setGrade(student.getGrade());
current.setPassword(student.getPassword());
break;
}
}
if (isLogin){
System.out.println("登录成功");
}else {
System.out.println("学号或者密码错误");
}
}
/**
* 注册/添加学生
*/
private static void regester(){
System.out.println("输入学生基本信息=====>");
System.out.print("学号:");
String stuNo = scanner.next();
for (Student student : students) {
if (student.getStuNo().equals(stuNo)){
System.out.println("已存在改学号学生,请重新输入");
System.out.print("学号:");
stuNo = scanner.next();
}
}
System.out.print("姓名:");
String name = scanner.next();
System.out.print("性别:");
String sex = scanner.next();
System.out.print("年龄:");
Integer age = scanner.nextInt();
System.out.print("成绩:");
Integer score = scanner.nextInt();
System.out.print("年级:");
String grade = scanner.next();
System.out.print("密码:");
String password = scanner.next();
Student student = new Student(stuNo,name,age,sex,score,grade,password);
studentService.addStudentInfo(students,student);
}
/**
* 更新学生信息
*/
private static void update(){
if (isLogin){
studentService.printStudentList(students);
System.out.print("输入需要更新的学号:");
String stuNo = scanner.next();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("性别:");
String sex = scanner.next();
System.out.print("年龄:");
Integer age = scanner.nextInt();
System.out.print("成绩:");
Integer score = scanner.nextInt();
System.out.print("年级:");
String grade = scanner.next();
System.out.print("密码:");
String password = scanner.next();
Student student = new Student(stuNo,name,age,sex,score,grade,password);
studentService.updateStudentInfo(students,student);
}else {
System.out.println("请先登录!");
}
}
/**
* 按成绩排名
*/
private static void sortByScore(){
List list = students;
for (int i = 0; i < list.size()-1; i++) {
for (int j = i+1; jlist.get(j).getScore()){
swap(list.get(i),list.get(j));
}
}
}
studentService.printStudentList(list);
}
/**
* 按年龄排名
*/
private static void sortByAge(){
List list = students;
for (int i = 0; i < list.size()-1; i++) {
for (int j = i+1; jlist.get(j).getAge()){
swap(list.get(i),list.get(j));
}
}
}
studentService.printStudentList(list);
}
private static void getSex(){
System.out.println("1.男");
System.out.println("2.女");
System.out.print("输入需要获取的性别序号:");
int choose = scanner.nextInt();
List list = new ArrayList();
switch (choose){
case 1:{
for (Student student : students) {
if (("男").equals(student.getSex())){
list.add(student);
}
}
}break;
case 2:{
for (Student student : students) {
if (("女").equals(student.getSex())){
list.add(student);
}
}
}break;
default:
System.out.println("输入错误");break;
}
if (list.size()>0){
studentService.printStudentList(list);
}
}
private static void getGrade(){
System.out.println("1.高级");
System.out.println("2.中级");
System.out.println("3.初级");
System.out.print("输入需要获取的年级序号:");
int choose = scanner.nextInt();
List list = new ArrayList();
switch (choose){
case 1:{
for (Student student : students) {
if (("高级").equals(student.getGrade())){
list.add(student);
}
}
}break;
case 2:{
for (Student student : students) {
if (("中级").equals(student.getGrade())){
list.add(student);
}
}
}break;
case 3:{
for (Student student : students) {
if (("初级").equals(student.getGrade())){
list.add(student);
}
}
}break;
default:
System.out.println("输入错误");break;
}
if (list.size()>0){
studentService.printStudentList(list);
}
}
private static void swap(Student m,Student n){
Student student = new Student(m.getStuNo(),m.getName(),m.getAge(),m.getSex(),m.getScore(),m.getGrade(),m.getPassword());
m.setStuNo(n.getStuNo());
m.setName(n.getName());
m.setAge(n.getAge());
m.setSex(n.getSex());
m.setScore(n.getScore());
m.setGrade(n.getGrade());
m.setPassword(n.getPassword());
n.setStuNo(student.getStuNo());
n.setName(student.getName());
n.setAge(student.getAge());
n.setSex(student.getSex());
n.setScore(student.getScore());
n.setGrade(student.getGrade());
n.setPassword(student.getPassword());
}
}
/**
* @author pengyangyan
*/
public class Student {
private String stuNo;
private String name;
private Integer age;
private String sex;
private Integer score;
private String grade;
private String password;
public Student() {
}
public Student(String stuNo, String name, Integer age, String sex, Integer score, String grade, String password) {
this.stuNo = stuNo;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
this.grade = grade;
this.password = password;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return stuNo+"| "+name+"| "+sex+"| "+age+"| "+score+"| "+grade+"| "+password+"|";
}
}
import java.util.List;
/**
* @author pengyangyan
*/
public interface StudentService {
/**
* 添加学生信息
* @param all 学生列表
*/
void addStudentInfo(List all,Student student);
/**
* 修改学生信息
* @param all 所有学生的列表
* @param student 需要修改的学生
* @return 当前的学生列表
*/
List updateStudentInfo(List all , Student student);
/**
*
* 删除学生信息
* @param all 所有学生的列表
* @param name 需要删除的学生姓名
* @return 当前的学生列表
*/
List deleteStudentInfo(List all , String name);
/**
* 打印学生列表
* @param all 当前学生列表
* @return 返回打印结果
*/
void printStudentList(List all);
/**
* 打印个人信息
* @param student 当前学生
*/
void printMine(Student student);
/**
* 初始化学生信息
* @param students 学生列表
*/
void initStudentList(List students);
}
import java.util.List;
import java.util.Random;
/**
* @author pengyangyan
*/
public class StudentServiceImpl implements StudentService {
@Override
public void addStudentInfo(List all,Student student) {
all.add(student);
System.out.println("学生信息已添加");
}
@Override
public List updateStudentInfo(List all, Student student) {
for (Student student1 : all) {
if (student1.getStuNo().equals(student.getStuNo())){
student1.setName(student.getName());
student1.setAge(student.getAge());
student1.setGrade(student.getGrade());
student1.setScore(student.getScore());
student1.setPassword(student.getPassword());
System.out.println("==============个人信息已更新=============");
printMine(student1);
break;
}
}
return all;
}
@Override
public List deleteStudentInfo(List all, String name) {
int size = all.size();
for (Student student1 : all) {
if (student1.getName().equals(name)){
System.out.println("-------消息通知----------");
System.out.println("| 成功删除"+name+" |");
System.out.println("------------------------");
all.remove(student1);
break;
}
}
if (all.size()==size){
System.out.println("-------消息通知----------");
System.out.println("| 不存在该学生 |");
System.out.println("------------------------");
}
return all;
}
@Override
public void printStudentList(List all) {
System.out.println("================学生信息================");
System.out.println("学号| 姓名| 性别| 年龄| 分数| 年级| 密码|");
for (Student student : all) {
System.out.println(student.toString());
}
System.out.println("--------------------------------------");
}
@Override
public void printMine(Student student) {
System.out.println("============我的个人信息=============");
System.out.println("学号--->"+student.getStuNo());
System.out.println("姓名--->"+student.getName());
System.out.println("性别--->"+student.getSex());
System.out.println("年龄--->"+student.getAge());
System.out.println("分数--->"+student.getScore());
System.out.println("年级--->"+student.getGrade());
System.out.println("密码--->"+student.getPassword());
System.out.println("-----------------------------------");
}
@Override
public void initStudentList(List students) {
Random random = new Random();
for (int i = 0; i < 10; i++) {
String sex = i%2==0?"男":"女";
String grade = i%3==0?"高级":(i%2==0?"初级":"中级");
Student student = new Student("No."+i,"学生"+i,+random.nextInt(30),sex,random.nextInt(100),grade,"123");
students.add(student);
}
}
}