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<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();
*/ }
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
原文链接:https://blog.csdn.net/wrxingkong/article/details/81486148
http://www.zzvips.com/article/176836.html
页:
[1]