java实现选课系统
作者:wrxingkong 发布时间:2021-08-15 07:43:44
标签: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<Student>{
private int id;
private String name;
private Set<Course> courses;
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
this.courses = new HashSet<Course>();
}
public Student() {
super();
this.id = 0;
this.name = null;
this.courses = new HashSet<Course>();
}
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<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> 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<Student> students = new ArrayList<Student>();
Map<Integer, Student> map1 = new HashMap<Integer, Student>();
List<Course> courses = new ArrayList<Course>();
Map<String, Course> map2 = new HashMap<String, Course>();
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<Student>) 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<Course>) 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<Student> students = new ArrayList<Student>();
List<Course> courses = new ArrayList<Course>();
Map<String, Course> map = new HashMap<String, Course>();
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<Student>) 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<Course>) 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<String> set = map.keySet();
Iterator<String> 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();
*/ }
}
来源:https://blog.csdn.net/wrxingkong/article/details/81486148


猜你喜欢
- 动态规划的基本思想是将待求解问题分解成若干个子问题,先求解子问题,并将这些子问题的解保存起来,如果以后在求解较大子问题的时候需要用到这些子问
- 1. 对象的创建对象创建的主要流程:1.类加载检查虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引
- 正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径。在本地工程和服务
- Struts2 Action/动作动作是Struts2框架的核心,因为他们的任何MVC(模型 - 视图 - 控制器)框架。每个URL将被映射
- 简述每个项目从新建开始我们或多或少都会导入各种依赖库,如果项目中只有一个module的话,对于依赖库版本的管理很容易,但是多个module的
- Java中重载与重写的区别首先我们来讲讲:重载(Overloading) (1) 方法重载是让类以统一的
- //计算耗时任务所需的秒数public int GetTimeSpan(DateTime dtStart, DateTime dtEnd){
- 这篇文章主要介绍了Java接口统一样式返回模板简介,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 在Android上,不止一个途径来侦听用户和应用程序之间交互的事件。对于用户界面里的事件,侦听方法就是从与用户交互的特定视图对象截获这些事件
- 前言RecyclerView是我们常用的列表控件,一般来说当Item的数据改变的时候我们需要刷新当前的Item 。如何刷新 RV 的列表?基
- Mybatis与Ibatis的区别: 1、Mybatis实现了接口绑定,使用更加方便 在ibatis2.x中我们需要在DAO的实现类中指定具
- 基本语法C#,又名Csharp,天朝喜欢叫C井。C#是一种面向对象的编程语言。在面向对象的程序设计方法中,程序有各种相互交互的对象组成。相同
- 本文实例为大家分享了convinientbanner顶部广告轮播控件的具体代码,供大家参考,具体内容如下gradle中添加compile &
- 应用程序初始化时需要批量的向sqlite中插入大量数据,单独的使用for+Insert方法导致应用响应缓慢,因为 sqlite插入数据的时候
- 本文实例讲述了Android编程之控件ListView使用方法。分享给大家供大家参考。具体分析如下:控件ListView是一个重要的控件,可
- 本文实例讲述了Android编程实现对电池状态的监视功能。分享给大家供大家参考,具体如下:最近在开发一个与GPS相关的项目,因为其中涉及到了
- C#都没人用了吗,网上想找个现成的雪花分形代码,都没找见,有C++,有python,有java的,就没有C#的,自己试试写一个吧。publi
- 有时候,根据业务逻辑的需求,我们想要获取到某个接口的所有实现类。在这里大致介绍两种方式:1.借助Spring容器实现Spring作为一个容器
- 推荐第三种方式,简单快捷不卡。第一种:jjdxm_updateGitHub地址:jjdxmashl/jjdxm_update效果图:点击立即
- 1.相关概念Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建