实现步骤:
- 构造课程类Course
- 构造课程列表类CourseList
- 构造学生类Student
- 构造学生--课程映射StudentMap类
- 主运行程序类Index类
Course.java:
package dk.courses;
/**
* @author Administrator
* 课程类
*/
public class Course {
private String id;
private String name;
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
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 toString(){
return "课程编号:"+this.getId()+"\t课程名称:"+this.getName();
}
/*public boolean equals(Object obj){
if(this==obj)
return true;
if(obj==null)
return false;
if(!(obj instanceof Course))
return false;
Course c = (Course)obj;
if(this.name==null){
if(c.name==null)
return true;
else
return false;
}else{
if(this.name.equals(c.name))
return true;
else
return false;
}
}*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
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 (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
CourseList.java:
package dk.courses;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
/**
* @author Administrator
* 课程列表类
*/
public class CourseList {
public static List<Course> courselist = new ArrayList<Course>();;//课程集合
public CourseList() {}
public List<Course> getCourselist() {
return courselist;
}
public void setCourselist(List<Course> courselist) {
CourseList.courselist = courselist;
}
//添加课程
public static void addCourse(String id, String name){
Course c = new Course(id, name);
System.out.println("添加了课程:\t"+"课程编号:"+c.getId()+"\t"+"课程名称:"+c.getName());
courselist.add(c);
}
//查询课程
public static Course getCourse(String id){
Iterator<Course> it = CourseList.courselist.iterator();
while(it.hasNext()){
Course c = it.next();
if(c.getId()==id)
return c;
}
return null;
}
//获取课程在集合中的索引
public static int getCourseIndex(String id){
int len = CourseList.courselist.size();
for(int i=0; i<len;i++){
Course c = CourseList.courselist.get(i);
if(c.getId()==id)
return i;
}
return -1;
}
//获取课程在集合中的索引
public static int getCourseIndex(Course c){
return CourseList.courselist.indexOf(c);
}
//修改课程
public static boolean setCourse(String id, String name){
int index = getCourseIndex(id);
if(index>=0){
Course c = CourseList.courselist.get(index);
c.setName(name);
CourseList.courselist.set(index, c);
return true;
}
return false;
}
//移除课程
public static boolean removeCourse(String id){
int index = getCourseIndex(id);
if(index>=0){
return CourseList.courselist.remove(index) != null;
}
return false;
}
//移除所有课程
public static boolean removeAllCourse(){
CourseList.courselist.clear();
return true;
//return this.courselist.removeAll(courselist);
}
//获取所有课程
public static void getAllCourse(){
Iterator<Course> it = CourseList.courselist.iterator();
show("=========================================================");
show("课程列表如下:(共有"+CourseList.courselist.size()+"门课程)");
show("=========================================================");
while(it.hasNext()){
Course c = it.next();
show(c.toString());
}
show("=========================================================");
}
public static void show(Object obj){
System.out.println(obj.toString());
}
//查找是否存在课程
public static boolean containCourse(Course c){
return CourseList.courselist.contains(c);
}
}
Student.java
package dk.students;
import java.util.ArrayList;
import dk.courses.Course;
import dk.courses.CourseList;
/**
* @author Administrator
* 学生类
*/
public class Student implements Comparable<Student>{
private String id;
private String name;
private ArrayList<String> courses;//储存课程编号
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
this.courses = new ArrayList<String>();
}
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 ArrayList<String> getCourses() {
return courses;
}
public void setCourses(ArrayList<String> courses) {
this.courses = courses;
}
//选择课程
public boolean selectCourses(String id){
if(CourseList.getCourseIndex(id)==-1){
CourseList.show("课程不存在!");
return false;
}
Course c = CourseList.getCourse(id);
show("您选择了以下课程:");
show(c.toString());
return this.courses.add(id);
}
//获取课程信息
public void printCourses(){
show("=========================================================");
show(this.toString());
show("=========================================================");
show("课表如下:");
for(String id:this.courses){
Course c = CourseList.getCourse(id);
show(c.toString());
}
show("=========================================================");
}
//删除课程
public boolean removeCourse(String id){
int index = this.getIndex(id);
if(index==-1){
return false;
}
this.courses.remove(index);
return true;
}
public int getIndex(String id){
for(int i=0;i<this.courses.size();i++){
if(this.courses.get(i)==id)
return i;
}
return -1;
}
public String toString(){
return "学号:"+this.getId()+"\t姓名:"+this.getName()+"\t共选择了门"+this.courses.size()+"课";
}
public static void show(Object obj){
System.out.println(obj.toString());
}
@Override
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;
}
@Override
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 == 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;
}
@Override
public int compareTo(Student o) {
//return this.name.compareTo(o.name);
return this.id.compareTo(o.id);
}
}
StudentMap.java
package dk.map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import dk.students.Student;
public class StudentMap {
public Map<String, Student> students;
private Scanner console;
public StudentMap(){
this.students = new HashMap<String, Student>();
}
//创建键值对
public void putStudents(){
console = new Scanner(System.in);
int i = 0;
while(i<3){
System.out.println("请输入学生ID");
String Id = console.next();
Student st = this.students.get(Id);
if(st==null){
show("请输入学生的姓名");
String Name = console.next();
Student student = new Student(Id,Name);
this.students.put(Id, student);
show("成功添加了学生:"+students.get(Id).getName());
i++;
}else{
show("该学生Id已被占用");
continue;
}
}
}
//获取键值对
public void getKeySet(){
Set<String> keyset = students.keySet();
show("总有"+students.size()+"个学生");
for(String id:keyset){
Student st = students.get(id);
if(st!=null){
show("学生:"+st.getName());
}
}
}
//删除键值对
public void removeKey(){
while(true){
show("请输入要删除学生的Id:");
String Id = console.next();
Student st = students.get(Id);
if(st==null){
show("学生信息不存在!");
continue;
}
students.remove(Id);
show("成功删除学生"+st.getName());
break;
}
}
//遍历集合
public void printEntrySet(){
Set<Entry<String, Student>> entryset = students.entrySet();
for(Entry<String,Student>entry:entryset){
show("取得的键值:"+entry.getKey());
show("取得的值:"+entry.getValue().toString());
}
}
//修改键值对
public void setKey(){
while(true){
show("请输入要修改的学生Id:");
String Id = console.next();
Student st = students.get(Id);
if(st==null){
show("学生不存在,请重新输入");
continue;
}
show("当前学生Id对应的学生姓名:"+st.getName());
show("请重新输入学生姓名:");
String newName = console.next();
Student nst = new Student(Id,newName);
students.put(Id, nst);
show("修改学生成功!");
getKeySet();
break;
}
}
public void test(){
students.containsValue(new Student(null,"001"));
}
public static void show(Object obj){
System.out.println(obj.toString());
}
}
Index.java
package dk.main;
import dk.courses.Course;
import dk.courses.CourseList;
import dk.students.Student;
public class Index {
public static void main(String[] args){
//CourseList list = new CourseList();
CourseList.show("开始添加课程");
CourseList.show("=========================================================");
CourseList.addCourse("001","Java课程设计");
CourseList.addCourse("002","数据结构");
CourseList.addCourse("003","高等数学");
CourseList.addCourse("004","Java课程设计");
CourseList.addCourse("005","计算机网络基础");
CourseList.getAllCourse();
CourseList.setCourse("001", "C语言程序设计");
CourseList.getAllCourse();
Course c = CourseList.getCourse("002");
Course c1 = new Course("0088",c.getName());
CourseList.show(CourseList.getCourseIndex(c1));
//Course c = CourseList.getCourse("001");
//Course c1 = new Course(c.getId(),c.getName());
CourseList.show(CourseList.containCourse(c1));
CourseList.removeCourse("004");
//CourseList.removeAllCourse();
CourseList.show(c.toString());
CourseList.getAllCourse();
Student s = new Student("007","DekingChen");
s.selectCourses("001");
s.selectCourses("002");
s.selectCourses("003");
s.selectCourses("005");
s.removeCourse("003");
s.printCourses();
}
}
运行结果:
开始添加课程
=========================================================
添加了课程: 课程编号:001 课程名称:Java课程设计
添加了课程: 课程编号:002 课程名称:数据结构
添加了课程: 课程编号:003 课程名称:高等数学
添加了课程: 课程编号:004 课程名称:Java课程设计
添加了课程: 课程编号:005 课程名称:计算机网络基础
=========================================================
课程列表如下:(共有5门课程)
=========================================================
课程编号:001 课程名称:Java课程设计
课程编号:002 课程名称:数据结构
课程编号:003 课程名称:高等数学
课程编号:004 课程名称:Java课程设计
课程编号:005 课程名称:计算机网络基础
=========================================================
=========================================================
课程列表如下:(共有5门课程)
=========================================================
课程编号:001 课程名称:C语言程序设计
课程编号:002 课程名称:数据结构
课程编号:003 课程名称:高等数学
课程编号:004 课程名称:Java课程设计
课程编号:005 课程名称:计算机网络基础
=========================================================
1
true
课程编号:002 课程名称:数据结构
=========================================================
课程列表如下:(共有4门课程)
=========================================================
课程编号:001 课程名称:C语言程序设计
课程编号:002 课程名称:数据结构
课程编号:003 课程名称:高等数学
课程编号:005 课程名称:计算机网络基础
=========================================================
您选择了以下课程:
课程编号:001 课程名称:C语言程序设计
您选择了以下课程:
课程编号:002 课程名称:数据结构
您选择了以下课程:
课程编号:003 课程名称:高等数学
您选择了以下课程:
课程编号:005 课程名称:计算机网络基础
=========================================================
学号:007 姓名:DekingChen 共选择了门3课
=========================================================
课表如下:
课程编号:001 课程名称:C语言程序设计
课程编号:002 课程名称:数据结构
课程编号:005 课程名称:计算机网络基础
=========================================================