java选课系统_java实现选课系统
本文实例为大家分享了java实现选课系统的具体代码,供大家参考,具体内容如下
这个程序主要是练习IO(文件读写,序列化),集合框架的使用
学生端可以实现,查找课程,增加选课,删除选课的功能
管理端可以实现对备选课程,学生信息的增删查改
缺点:登陆操作没有实现密码验证和多态。
另外map对象明明put了,可是get的时候竟然会取到null,而且尝试多次,有时候成功,有时候取到null,并不确定。据说这是由多线程引起的map取值为null,因为多线程部分还没开始学习,所以也没做修改。
//课程信息
package selectCourse;
import java.io.Serializable;
public class Course implements Serializable{
private String id;
private String name;
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
public Course() {
super();
}
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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Course other = (Course) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "课程号:" + id + " " + "课程名:" + name;
}
}
//学生信息
package selectCourse;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Student implements Serializable,Comparable{
private int id;
private String name;
private Set courses;
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
this.courses = new HashSet();
}
public Student() {
super();
this.id = 0;
this.name = null;
this.courses = new HashSet();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
return true;
}
public String toString() {
return "学号:"+id+" " +"姓名:"+name;
}
//遍历输出所选课程
public void travese()
{
if(courses.size()>0)
{
for (Course course : courses) {
System.out.println(course);
}
}
else
{
System.out.println("还没有选课");
}
}
public int compareTo(Student s) {
int result=this.id-s.id;
return result;
}
}
//管理端
package selectCourse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class AdministratorOp {
//管理端,用来管理学生信息和备选课程
List students = new ArrayList();
Map map1 = new HashMap();
List courses = new ArrayList();
Map map2 = new HashMap();
Scanner in = new Scanner(System.in);
public AdministratorOp() {
}
//~~~~~~~~~~~~~~~~~从文件读入List~~~~~~~~~~~~~~~~~~~~~
public void load1() {
File file = new File("students.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
students = (List) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void load2() {
File file = new File("courses.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
courses = (List) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//将信息写回文件
public void save1() {
File file = new File("students.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(students);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void save2() {
File file = new File("courses.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(courses);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根据List来创建Map~~~~~~~~~~~~~~~~~~~~~~~
public void createMap1() {
for (int i = 0; i < students.size(); i++) {
map1.put(students.get(i).getId(), students.get(i));
}
}
public void createMap2() {
for (int i = 0; i < courses.size(); i++) {
map2.put(courses.get(i).getId(), courses.get(i));
}
}
// ~~~~~~~~~~~~~~~~~~~~~~ 增删查改~~~~~~~~~~~~~~~~~~~~~~~
// 增加学生基本信息
public void add() {
System.out.println("输入学生信息,输入0结束");
while (true) {
int id = in.nextInt();
if(id!=0) {
String name = in.next();
Student s = new Student(id, name);
students.add(s);
Collections.sort(students);
map1.put(id, s);
System.out.println("添加成功");
}
if (id == 0) {
break;
}
}
}
// 删除学生信息
public void del() {
while(true) {
int id = in.nextInt();
Student s = map1.get(id);
students.remove(s);
map1.remove(id);
System.out.println("移除成功");
if (id == 0) {
break;
}
}
}
// 增加课程基本信息
public void add2() {
System.out.println("输入课程信息,输入end结束");
while (true) {
String id = in.nextLine();
if(!id.equals("end"))
{
String name = in.nextLine();
Course cr = new Course(id, name);
courses.add(cr);
map2.put(id, cr);
System.out.println("添加成功");
}
else{
//System.out.println("添加结束");
break;
}
}
}
// 删除课程信息
public void del2() {
while(true) {
String id = in.next();
if(!id.equals("end")) {
Course cr = map2.get(id);
courses.remove(cr);
map2.remove(id);
System.out.println("移除成功");
}
else
{
break;
}
}
}
// 根据学号查找学生
public void query1() {
System.out.println("请输入要查询的学生学号:");
if (in.hasNext()) {
int id = in.nextInt();
System.out.println(map1.get(id));
map1.get(id).travese();
}
}
// 根据课程号查找课程
public void query2() {
System.out.println("请输入要查询的课程号:");
if (in.hasNext()) {
String id = in.nextLine();
System.out.println(map2.get(id));
}
}
// 修改学生基本信息
public void modify1() {
System.out.println("请输入要修改的学生的学号:");
int id = in.nextInt();
Student s = map1.get(id);
System.out.println("输入修改后的学生信息:");
int no = in.nextInt();
String name = in.next();
int i = students.indexOf(s);
students.set(i, new Student(no, name));
Collections.sort(students);
map1.remove(id);
map1.put(no, new Student(no, name));
System.out.println("修改成功");
}
// 修改课程信息
public void modify2() {
System.out.println("请输入要修改的课程的课程号:");
String id = in.nextLine();
Course cr = map2.get(id);
System.out.println("输入修改后的课程信息:");
String no = in.nextLine();
String name = in.nextLine();
int i = courses.indexOf(cr);
courses.set(i, new Course(no, name));
map2.remove(id);
map2.put(no, new Course(no, name));
System.out.println("修改成功");
}
// ~~~~~~~~~~~~~~~~~~~~~~ 遍历list~~~~~~~~~~~~~~~~~~~~~~~
void display1() {
System.out.println("所有的学生信息:");
for (Student s : students) {
System.out.println(s.toString());
s.travese();
}
}
void display2() {
System.out.println("所有的备选课程信息:");
for (Course course : courses) {
System.out.println(course.toString());
}
}
public void close()
{
in.close();
}
}
//学生操作端
package selectCourse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class StudentOp {
Scanner in = new Scanner(System.in);
Student st;
List students = new ArrayList();
List courses = new ArrayList();
Map map = new HashMap();
public StudentOp(int no) {
load3(no);
load4();
}
// ~~~~~~~~~~~~~~~~~从文件读入信息~~~~~~~~~~~~~~~~~~~~~
public void load3(int n) {
File file = new File("students.txt");
FileInputStream fis;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
students = (List) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < students.size(); i++) {
if (n == students.get(i).getId()) {
st = students.get(i);
break;
}
}
}
public void load4() {
File file = new File("courses.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
courses = (List) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 将信息写回文件
public void save3() {
File file = new File("students.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(students);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void save4() {
File file = new File("courses.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(courses);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根据List来创建Map~~~~~~~~~~~~~~~~~~~~~~~
public void createMap() {
for (int i = 0; i < courses.size(); i++) {
map.put(courses.get(i).getId(), courses.get(i));
}
//遍历map
/*Set set = map.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + " " + map.get(key));
} */
}
//遍历显示备选课程
public void displayAllCourse() {
System.out.println("所有的备选课程信息:");
for (Course course : courses) {
System.out.println(course.toString());
}
}
//根据课程号查询备选课程
public void queryCourse() {
System.out.println("请输入要查询的课程号:");
String str = in.next();
System.out.println(str);
System.out.println((map.containsKey(str) ? "yes" : "no"));
System.out.println(map.get(str));
}
//显示所选课程
public void display() {
System.out.println("所选课程:");
st.travese();
}
//增加所选课程
public void addSelect() {
System.out.println("输入所选课程的课程号,输入end结束");
while (true) {
String id = in.nextLine();
if (!id.equals("end")) {
Course cr = map.get(id);
st.getCourses().add(cr);
System.out.println("选课成功");
} else {
// System.out.println("添加结束");
break;
}
}
}
//减少所选课程
public void deleteSelect() {
System.out.println("要删除课程的课程号,输入end结束");
while (true) {
String id = in.nextLine();
if (!id.equals("end")) {
Course cr = map.get(id);
st.getCourses().remove(cr);
System.out.println("删除成功");
} else {
// System.out.println("添加结束");
break;
}
}
}
public void close() {
in.close();
}
}
//测试类
package selectCourse;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//~~~~~~~~~~~~~测试管理端~~~~~~~~~~~~~~~~~~~~~~~~~~
/*添加学生
AdministratorOp a1=new AdministratorOp();
a1.add();
//a1.display1();
// a1.close();
a1.save1();*/
/*添加课程
AdministratorOp a2=new AdministratorOp();
a2.add2();
//a2.display2();
a2.close();
a2.save2();*/
/* // 测试删除,查找,修改
AdministratorOp a3=new AdministratorOp();
a3.load1();
a3.createMap1();
a3.load2();
a3.createMap2();
// a3.display1();
// a3.display2();
// a3.del();
// a3.display1();
// a3.del2();
// a3.display2();
// a3.query1();
// a3.query2();
// a3.modify1();
// a3.display1();
// a3.modify2();
// a3.display2();
a3.close();
// a3.save1();
// a3.save2();*/
//~~~~~~~~~~~~~~~~测试学生端~~~~~~~~~~~~~~~~~~~~~~~~~
/*Scanner in=new Scanner(System.in);
System.out.println("请输入学号:");
int id=in.nextInt();
StudentOp sto=new StudentOp(id);
sto.createMap();
//sto.displayAllCourse();
//sto.queryCourse();
// sto.addSelect();
// sto.deleteSelect();
sto.display();
sto.close();
in.close();
// sto.save3();
// sto.save4();
*/ }
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
java选课系统_java实现选课系统相关推荐
- java学生选课系统_java学生选课系统(完整源代码.doc
PAGE PAGE 3 课程设计说明文档 学生选课管理系统的设计与实现 学校:渤海大学 学院:信息科学与技术学院 专业:信息管理与信息系统10-5 姓名:陈功发 程磊 段晶 黄一媛 一.课程设计目的 ...
- java 外卖订餐系统_java外卖订餐系统小项目
本文实例为大家分享了java外卖订餐系统的具体代码,供大家参考,具体内容如下 执行结果: 通过选择功能序号,执行响应的功能: 代码实现: package 外卖订餐系统; /* * 代码优点,使用 循环 ...
- 教材订购模块java代码实现_java教材征订系统
每天记录学习,每天会有好心情.*^_^* 今天将为大家分析一个基于web的java教材征订系统,因此本系统选择学校的教材征订为研究对象,以实现教材征订网络化管理.提高工作效率.减少教材征订中的错误为目 ...
- java报名系统_java在线报名系统
每天记录学习,每天会有好心情.*^_^* 今天记录的项目是基于web的java在线报名系统,网上培训报名系统可以在不同的地方上网多个报名点,可以选择不同的课程,填写自己的报名必要的资料,管理员需要动态 ...
- java对嵌入式_Java用于嵌入式系统的优点
Java用于嵌入式系统的优点 与个人计算机这样的通用计算机系统不同,嵌入式系统通常执行的是带有特定要求的预先定义的任务.yjbys小编下面为你整理了关于Java用于嵌入式系统的优点,希望对你有所帮助. ...
- java 字体名字_JAVA:获取系统中可用的字体的名字
import java.awt.*; public class GetLocalFontFamily { public static void main(String[] agrs) { //获取系统 ...
- java控制系统音量_Java 控制 Windows 系统音量-Go语言中文社区
目录 1.使用 Java 来控制 Windows 系统音量,使用 JNA 调用 windows 底层 API 因为有点麻烦,所以这里采用纯 Java API结合 VBS 脚本的方式进行控制. 2.可以 ...
- java 平台级模块系统_Java平台模块系统公众审查未能通过
java 平台级模块系统 在过去的几周里,Java世界中的戏剧,阴谋和政治活动异常高涨,最终在本周的JSR 376 Java平台模块系统公共评审投票中达到了顶峰. Java模块化(包括Java平台模块 ...
- cas java单点登录_java单点登录系统CAS的简单使用
http://blog.csdn.net/yunye114105/article/details/7997041 参考: http://blog.csdn.net/diyagea/article/de ...
最新文章
- Halcon与QT的联合编程(2)
- web渗透漏洞实例讲解视频课程
- python热部署_定时任务-Quartz(热部署、冷部署)
- 海洋CMS仿RiPro主题风格自适应模板
- serialport 延时计时器 修改_为了夜经济,青岛真是拼了!地铁公交延时运营,再也不怕没车了...
- 林语堂:读书须有胆识,有眼光,有毅力
- 不讲武德,Java分布式面试题集合含答案!
- chrome浏览器开发常用快捷键之基础篇-遁地龙卷风
- 基于asp.net729在校大学生助学贷款管理系统
- MySQL主从复制与读写分离
- MFC求一元二次方程的根(三种情况:相同根,不同根,虚根)
- turn.js学习手册
- Web前端--HTML+CSS+JS新型冠状病毒射击小游戏
- Android Fragmnet-Fragment数据交换以及ListFragment的使用
- Redis集群入门实践教程
- MySQL的背景、字体换色
- cad序列号2016申请号_安装 CAD 踩坑
- ThreadX(三)------线程thread
- html 原生弹出框,html、css和js原生写一个模态弹出框,顺便解决父元素半透明子元素不透明效果...
- 【电子技术综合设计】数字钟(包含计数模块、12/24进制切换模块以及闹钟模块)
热门文章
- 写一个函数,计算一个整数数组的平均值
- 写作辅助软件_文案写作系统_软文写作服务_写作软件服务商|Giiso智搜
- 计算机中十进制转二进制逻辑原理,.计算机中为什么要采用二进制?及二进制的基本运算规则,还有.二进制数据与十进制、八进制、十六进制数据之间的转换方法?...
- 网络安全学习篇28_阶段一小结篇_木马的原理及木马防范
- 陪玩app源码,陪玩系统开发约单下单逻辑处理规则
- wikijs使用docker安装
- celery5.2.1以下版本任务调用多耗费1秒
- 浅谈图书情报在元宇宙中的定位和发展
- 洛谷 P5027 Barracuda 题解
- postman-SOAP 请求