这篇文章主要为大家详细介绍了Java实现学生选课管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Java实现学生选课管理系统的具体代码,供大家参考,具体内容如下
需求分析
本数据库的用户主要是学生,通过对用户需求的收集和分析,获得用户对数据库的如下要求。
1.信息需求
1.学生信息:学号,姓名,性别,专业
2.登陆信息:账号,密码
3.课程信息:课程号,课程名,选课人数,选课容量,任课老师
4.选课信息:课程号,学生学号
5.登录信息:账号、密码
2.功能需求
1.系统为学生建立登陆信息,学生进入系统前需要身份验证,用户名、密码输入正确后方可进入系统。
2.在系统中,用户可以在界面中看到本人的基本信息,也可以对课程信息表和个人选课信息表进行查看、以及选课。
3.使用数据库存储读取数据内容
3.系统需求
学生信息管理系统采用的编译环境是IntelliJ IDEA,编程语言是Java,使用用MySQL数据库
定义数据库
CREATE DATABASE Couse ON PRIMARY(NAME = Couse,
FILENAME = 'D:\JAVA\courseDesign_2020JAVA' ,
SIZE = 2MB, FILEGROWTH = 10%,FILERROWHT=4MB)
CREATE TABLE user (
id char(25) IDENTITY NOT NULL PRIMARY KEY,
name char(25) NOT NULL ,
sex char(2) CHECK (性别 IN('男','女')) ,
profess char(30) NULL ,
)
CREATE TABLE subject (
subjectId varchar(20) NOT NULL PRIMARY KEY ,
name varchar(25) NOT NULL ,
Noss int NOT NULL ,
capacity int NULL ,
teacher varchar(25) NULL ,
)
CREATE TABLE PickCouse (
stuId char(25),
couseId char(25) NOT NULL ,
)
CREATE TABLE login (
Id char(25) NOT NULL PRIMARY KEY,
pwd char(25) NOT NULL ,
)
设计项目结构
1.项目包结构
2.项目UML类图
运行效果展示
1.登录
2.查看全部课程信息
3.查看个人选课信息
4.学生选课
开放源代码
1、学生用户信息类:
package StuPickCouse_Dos.PickCouse;
public class User {
String id;
String pwd;
String name;
String sex;
String pross;//专业
public User(String id,String pwd){
this.id=id;
this.pwd=pwd;
}
public User(String id, String name, String sex, String pross) {
this.id = id;
this.name = name;
this.sex = sex;
this.pross = pross;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(){}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPross() {
return pross;
}
public void setPross(String pross) {
this.pross = pross;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", pross='" + pross + '\'' +
'}';
}
}
2、课程信息类:
package StuPickCouse_Dos.PickCouse;
public class Couse {
String id;
String name;
String num;//选课人数
String capacity;//容量
String teacher;
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public Couse(String id, String name, String num, String capacity, String teacher) {
this.id = id;
this.name = name;
this.num = num;
this.capacity = capacity;
this.teacher = teacher;
}
public Couse(String id){this.id=id;}
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 String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getCapacity() {
return capacity;
}
public void setCapacity(String capacity) {
this.capacity = capacity;
}
@Override
public String toString() {
return id +" "+ name +" "+ num+ "/" + capacity+" " +teacher;
}
}
3、学生数据库操作类
package StuPickCouse_Dos.PickMysql;
import StuPickCouse_Dos.PickCouse.User;
import javax.swing.*;
import java.sql.*;
public class UserSql {
Connection con=null;
Statement sql;
ResultSet rs;
public UserSql(){}
void star(){
String url="jdbc:mysql://localhost:3306/couse?user=root&password=&useSSL=true&useUnicode=true&characterEncoding=utf-8";
String user="root";
String passKey="";
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection(url,user,passKey);
}
catch(SQLException e){
System.out.println(e);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public User getUser(String id){
User user=new User();
star();
try {
sql=con.createStatement();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
rs=sql.executeQuery("select * from User where id="+id);
while(rs.next()){
user.setId(id);
user.setName(rs.getString(2));
user.setSex(rs.getString(3));
user.setPross(rs.getString(4));
}
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user;
}
public String findUser(String id){
String user="";
star();
try {
sql=con.createStatement();
} catch (SQLException throwables) {
// throwables.printStackTrace();
JOptionPane.showMessageDialog(null,
"数据库无法连接!",
"登录失败",
JOptionPane.ERROR_MESSAGE);
}
try {
rs=sql.executeQuery("select pwd from Login where id="+id);
while(rs.next()){
user=rs.getString(1);
}
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user.equals("")?null:user;
}
}
4、课程数据库信息操作类:
package StuPickCouse_Dos.PickMysql;
import StuPickCouse_Dos.PickCouse.Couse;
import StuPickCouse_Dos.PickCouse.User;
import java.sql.*;
import java.util.ArrayList;
public class CouseSql {
User user;
public CouseSql(User user){
this.user=user;
}
Connection con=null;
Statement sql;
ResultSet rs;
void star(){
String url="jdbc:mysql://localhost:3306/couse?user=root&password=&useSSL=true&useUnicode=true&characterEncoding=utf-8";
String user="root";
String passKey="";
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection(url,user,passKey);
}
catch(SQLException e){
System.out.println(e);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public ArrayList<Couse> findCouse(String sqlLine){
ArrayList<Couse> couses=new ArrayList<>();
star();
try {
sql=con.createStatement();//
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
rs = sql.executeQuery(sqlLine);
while (rs.next()) {
couses.add(new Couse(rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5)
));
}
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return couses;
}
public boolean isMyCouse(String sqlLine){
boolean couses=false;
star();
try {
sql=con.createStatement();//
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
rs = sql.executeQuery(sqlLine);
while (rs.next()) {
couses=true;
}
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return couses;
}
public void chioseCouse(Couse couse) {
star();
try {
sql=con.createStatement();//
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
sql.executeUpdate("update Subject set Noss=Noss+1 where subjectId="+couse.getId());
sql.executeUpdate("INSERT INTO PickCouse VALUES ('"+this.user.getId()+"','"+couse.getId()+"')");
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
System.out.println("====>添加成功!");
}
public void addCouse(String id,String name,int x1,int x2,String tec) throws Exception{
star();
sql=con.createStatement();
sql.executeUpdate("INSERT INTO Subject VALUES (\""+id+"\",\""+name+"\","+x1+","+x2+",\""+tec+"\")");
con.close();
}
}
5、功能实现类:
package StuPickCouse_Dos;
import StuPickCouse_Dos.PickCouse.Couse;
import StuPickCouse_Dos.PickCouse.User;
import StuPickCouse_Dos.PickMysql.CouseSql;
import StuPickCouse_Dos.PickMysql.UserSql;
import java.util.ArrayList;
import java.util.Scanner;
public class Operator {
User user;
CouseSql couseSql;
UserSql userSql;
ArrayList<Couse> couses = new ArrayList<>();
public void setUser(User user) {
this.user = user;
}
public Operator(){}
public Operator(User user) {
this.user = user;
couseSql=new CouseSql(user);
userSql=new UserSql();
}
public void findAllCouse(){
couses = couseSql.findCouse("select * from Subject");
for (int i = 0; i < couses.size(); i++)
System.out.println((i + 1) + " " + couses.get(i).toString());
}
public void selectCouse(){
System.out.println("所有课程如下:");
ArrayList<Couse> couseICanChose=new ArrayList<>();
ArrayList<String> couseICanChoseId=new ArrayList<>();
for (int i = 0; i < couses.size(); i++){
System.out.print((i + 1) + " " + couses.get(i).toString());
boolean tag=false,flage=false;
System.out.println( ( tag= ( ( !(flage=couseSql.isMyCouse("select * from PickCouse where stuId="+user.getId()+" and couseId="+couses.get(i).getId() )
)&& Integer.parseInt( couses.get(i).getCapacity() )
>= Integer.parseInt( couses.get(i).getNum() ) ) ) )? " 可选": flage?" 已选":" 人员已满");
if(tag) {
couseICanChose.add(couses.get(i));
couseICanChoseId.add(couses.get(i).getId());
}
}
if(couseICanChose.size() < 1)
System.out.print("暂时没有可选的课程!");
else {
System.out.println("====》可选课程有:");
for (int i = 0; i < couseICanChose.size(); i++)
System.out.println((i + 1) + " " + couseICanChose.get(i).toString());
System.out.print("====>请输入选择的课程编号:");
String couse = "";
while(!couseICanChoseId.contains( couse = (new Scanner(System.in)).nextLine()))
System.out.print("输入不在选择范围,请重新输入:");
couseSql.chioseCouse(new Couse(couse));
}
}
public void findMyCouse(){
shoeArrayList(couseSql.findCouse("select * from Subject where subjectId in (select couseId from PickCouse where stuId="+user.getId()+")"));
}
public static void shoeArrayList(ArrayList<Couse> item){
for (int i = 0; i < item.size(); i++)
System.out.println((i + 1) + " " + item.get(i).toString());
System.out.print("Tip: 按任意键继续:");
(new Scanner(System.in)).next();
}
public boolean intoPwd(String pwd,String id){
int inputTimes=2;
while( !( userSql.findUser(id).equals((new Scanner(System.in)).nextLine()) )&&inputTimes>0)
System.out.print("![第"+(3-inputTimes)+"次输入]-密码输入错误:剩余输入机会"+(inputTimes--)+"\n请重新输入:");
return inputTimes>=0;
}
public int show(){
System.out.println("*****************************************");
System.out.println("*****************************************");
System.out.println("******** 1)查看课程信息 **************");
System.out.println("******** 2)选课 *************");
System.out.println("******** 3)查看个人选课 *************");
System.out.println("*****************************************");
System.out.print("请输入选择:");
return (new Scanner(System.in)).nextInt();
}
}
6、程序入口:
package StuPickCouse_Dos;
import StuPickCouse_Dos.PickCouse.User;
import StuPickCouse_Dos.PickMysql.UserSql;
import java.util.Scanner;
public class Demo_1 {
public static void main(String[] args) throws Exception {
System.out.println("*****************************************");
System.out.println("******** 学生选课管理-登录 ***********");
System.out.println("*****************************************");
User user= new User();
System.out.print("===》请输入账号:");
String id="";
int inputTimes=1;
while(!(id=(new Scanner(System.in)).nextLine()).matches("[0-9]{9}"))
System.out.print("![第"+(inputTimes++)+"次输入]输入错误:用户账号为你的学号\n请重新输入:");
System.out.print("====>请输入密码:");
if((new Operator()).intoPwd("",(user=(new UserSql()).getUser(id)).getId())){
System.out.println("用户:"+user.getName()+",欢迎登录!");
while((inputTimes=(new Operator()).show())> 0 ) {
switch (inputTimes) {
case 1:(new Operator(user)).findAllCouse();break;
case 2:(new Operator(user)).selectCouse();break;
case 3:(new Operator(user)).findMyCouse();break;
// case 99:addCouse(new CouseSql(user));break;
default:return;
}
System.out.print("按任意键返回主菜单:");
(new Scanner(System.in)).next();
}
}
else{
System.out.println("登录失败!请重新登录!");
main(args);
}
}
// public static void addCouse(CouseSql sql) throws Exception{
// System.out.print("1)请输入课程编号:");
// String id=(new Scanner(System.in)).nextLine();
// System.out.print("2)请输入课程名称:");
// String name=(new Scanner(System.in)).nextLine();
// System.out.print("3)请输入已选课人数:");
// int noss=(new Scanner(System.in)).nextInt();
// System.out.print("4)请输入课程选课容量:");
// int cap=(new Scanner(System.in)).nextInt();
// System.out.print("5)请输入课程教师姓名:");
// String tec=(new Scanner(System.in)).nextLine();
// sql.addCouse(id,name,noss,cap,tec);
// System.out.println("======》添加成功!");
// System.out.print("继续输入请按1,退出按其他键:");
// if((new Scanner(System.in)).nextLine()=="1"){
// addCouse(sql);
// }
// }
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
本文标题为:Java实现学生选课管理系统
基础教程推荐
- java基础知识之FileInputStream流的使用 2023-08-11
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java并发编程进阶之线程控制篇 2023-03-07
- Java文件管理操作的知识点整理 2023-05-19
- Java数据结构之对象比较详解 2023-03-07
- java实现多人聊天系统 2023-05-19
- springboot自定义starter方法及注解实例 2023-03-31
- Java实现查找文件和替换文件内容 2023-04-06
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java实现线程插队的示例代码 2022-09-03