手记

学生选课系统,加油啦啦啦~~~

course类

package com.collection_map;

public class Course {
    public String id;
    public String name;
    public Course(String id,String name){
        this.id=id;
        this.name=name;
    }
    public Course(){

    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        if(obj==null){
            return false;
        }
        if(!(obj instanceof Course))
            return false;
        Course course=(Course) obj;
        if(this.name==null){
            if(course.name==null)
                return true;
            else
                return false;
        }else{
            if(this.name.equals(course.name))
                return true;
            else 
                return false;
        }
    }
}

Student类

package com.collection_map;

import java.util.HashSet;
import java.util.Set;

public class Student implements Comparable<Student> {
    public String id;
    public String name;
    public Set<Course> courses;
    public Student(String id,String name){
        this.id=id;
        this.name=name;
        this.courses=new HashSet<Course>();
    }
    @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;
        Student other = (Student) obj;
        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) {
        // TODO Auto-generated method stub
        return this.id.compareTo(o.id);
    }
}

CollectionTest类

package com.collection_map;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionTest {
//通过Collections.sort()方法,对Integer泛型的List进行排序
    public void testSort1(){
        List<Integer> integerList=new ArrayList<Integer>();
        Random random=new Random();
        Integer k;
        for(int i=0;i<10;i++){
            do{
                k=random.nextInt(100);
            }while(integerList.contains(k));
            integerList.add(k);
            System.out.println("成功添加整数:"+k);
        }
        System.out.println("----------排序前---------");
        for(Integer integer:integerList){
            System.out.println("元素:"+integer);
        }
        Collections.sort(integerList);
        System.out.println("----------排序后---------");
        for(Integer integer:integerList){
            System.out.println("元素:"+integer);
        }
    }

    //对String泛型List进行排序
    public void testSort2(){
        List<String> stringList=new ArrayList<String>();
        stringList.add("microsoft");
        stringList.add("google");
        stringList.add("lenovo");
        System.out.println("----------排序前---------");
        for(String string:stringList){
            System.out.println("元素:"+string);
        }
        Collections.sort(stringList);
        System.out.println("----------排序后---------");
        for(String string:stringList){
            System.out.println("元素:"+string);
        }
    }
    //对随机string排序
    public void testSort22(){
        List<String> stringList=new ArrayList<String>();
        for(int i=0;i<10;i++){
            StringBuilder sb=new StringBuilder();
            Random rd=new Random();
            do{
                int len=rd.nextInt(10);
                for(int j=0;j<len;j++){
                    char c=(char)(rd.nextInt(25)+'a');
                    sb.append(c);
                }
            }while(stringList.contains(sb));
            stringList.add(sb.toString());
        }
        System.out.println("----------排序前---------");
        for(String string:stringList){
            System.out.println("元素:"+string);
        }
        Collections.sort(stringList);
        System.out.println("----------排序后---------");
        for(String string:stringList){
            System.out.println("元素:"+string);
        }
    }

    //对其他类型泛型的List进行排序
    public void testSort3(){
        List<Student> studentList=new ArrayList<Student>();
        Random random=new Random();     
        studentList.add(new Student(random.nextInt(1000)+"","Mike"));
        studentList.add(new Student(random.nextInt(1000)+"","Angela"));
        studentList.add(new Student(random.nextInt(1000)+"","Lucy"));
        System.out.println("----------排序前---------");
        for(Student student:studentList){
            System.out.println("学生:"+student.id+":"+student.name);
        }
        Collections.sort(studentList);
        System.out.println("----------排序后---------");
        for(Student student:studentList){
            System.out.println("学生:"+student.id+":"+student.name);
        }
        Collections.sort(studentList,new StudentComparator());
        System.out.println("-----按姓名排序------");
        for(Student student:studentList){
            System.out.println("学生:"+student.id+":"+student.name);
    }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       CollectionTest ct=new CollectionTest();
       //ct.testSort1();
       //ct.testSort2();
      // ct.testSort22();
       ct.testSort3();
    }

}

ListTest类

package com.collection_map;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    //用于存放备选课程
   public List courseToSelect;
   public ListTest(){
       this.courseToSelect=new ArrayList();
   }
   public void testAdd(){
       Course cr1=new Course("1","数据结构");
       courseToSelect.add(cr1);
       Course temp=(Course)courseToSelect.get(0);
       System.out.println("添加了课程:"+temp.id+":"+temp.name);
       Course cr2=new Course("2","C语言");
       courseToSelect.add(0,cr2);
       Course temp2=(Course)courseToSelect.get(0);
       System.out.println("添加了课程:"+temp2.id+":"+temp2.name);
       Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
       courseToSelect.addAll(Arrays.asList(course));
       Course temp3=(Course)courseToSelect.get(2);
       Course temp4=(Course)courseToSelect.get(3);
       System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name);
       Course[] course2={new Course("5","高等数学"),new Course("6","英语")};
       courseToSelect.addAll(2,Arrays.asList(course2));
       Course temp5=(Course)courseToSelect.get(2);
       Course temp6=(Course)courseToSelect.get(3);
       System.out.println("添加了两门课程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name);
   }
   public void testGet(){
       int size=courseToSelect.size();
       System.out.println("有如下课程可选:");
       for(int i=0;i<size;i++){
           Course cr=(Course)courseToSelect.get(i);
           System.out.println("课程:"+cr.id+":"+cr.name);
    }
   }
//  通过迭代器来遍历List,迭代器本身也是一个接口
   public void testIterator(){
       Iterator it=courseToSelect.iterator();
       System.out.println("有如下课程可选(通过迭代器):");
       while(it.hasNext()){
           Course cr=(Course)it.next();
          System.out.println("课程:"+cr.id+":"+cr.name);
       }
   }
  //通过for each方法访问集合元素
   public void testForEach(){
       System.out.println("有如下课程可选(通过foreach):");
       for(Object obj:courseToSelect){
           Course cr=(Course)obj;
           System.out.println("课程:"+cr.id+":"+cr.name);
       }
   }
   //修改List中的元素
   public void testModify(){
       courseToSelect.set(4,new Course("7","毛概"));
   }
   //删除List中的元素
  /* public void testRemove(){
       //Course cr=(Course)courseToSelect.get(4);
       //System.out.println("我是课程:"+cr.id+":"+cr.name+"我即将被删除");
       courseToSelect.remove(4);
       System.out.println("成功删除课程!!");
       testForEach();
   }*/
   public void testRemoveAll(){
       System.out.println("即将删除4和5位置上删除");
       Course[] courses={(Course)courseToSelect.get(4),(Course)courseToSelect.get(5)};
       courseToSelect.removeAll(Arrays.asList(courses));
       System.out.println("成功删除课程!!");
       testForEach();
   }
   public static void main(String[] args) {
    ListTest lt=new ListTest();
    lt.testAdd();
    lt.testGet();
    lt.testIterator();
    lt.testModify();
    lt.testForEach();
    //lt.testRemove();
    lt.testRemoveAll();
}
}

MapTest类

package com.collection_map;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class MapTest {
    public Map<String,Student> students;
    //构造器中初始化students属性
    public MapTest(){
        this.students=new HashMap<String,Student>();
    }
    //测试添加:输入学生ID,判断是否被占用
    public void testPut(){
        Scanner console=new Scanner(System.in);
        int i=0;
        while(i<3){
            System.out.println("请输入学生ID:");
            String ID=console.next();
            //判断该ID是否被占用
            Student st=students.get(ID);
            if(st==null){
                //输入学生姓名
                System.out.println("请输入学生姓名:");
                String name=console.next();
                //创建新的学生对象
                Student newStudent=new Student(ID,name);
                //通过调用students的put方法,添加ID-学生映射
                students.put(ID, newStudent);
                System.out.println("成功添加学生:"+students.get(ID).name);
                i++;
            }else{
                System.out.println("该学生ID已被占用!!");
                continue;
            }

        }
    }

    //测试Map的KeySet方法
    public void testKeySet(){
        //通过keySet方法,返回Map中所有“键”的Set集合
        Set<String> KeySet=students.keySet();
        System.out.println("总共有:"+students.size()+"个学生");
        //遍历KeySet,取得每一个键,再调用get方法取得每个键对应的value
        for(String stuId:KeySet){
            Student st=students.get(stuId);
            if(st!=null){
                System.out.println("学生:"+st.name);
            }
        }
    }
    //测试删除Map总的映射
    public void testRemove(){
        System.out.println("请输入要删除学生的Id!");
        Scanner console=new Scanner(System.in);
        while(true){
        String ID=console.next();
        Student st=students.get(ID);
        if(st==null){
            System.out.println("该ID不存在!!");
            continue;
        }
        students.remove(ID);
        System.out.println("成功删除学生:"+st.name);
        break;
    }
    }

    //通过entrySet方法来遍历MAp
    public void testEntrySet(){
        //通过entrySet方法,返回Map中的所有键值对
        Set<Entry<String,Student>> entrySet=students.entrySet();
        for(Entry<String,Student> entry:entrySet){
            System.out.println("取得键:"+entry.getKey());
            System.out.println("对应的值"+entry.getValue().name);
        }
    }

    //利用put方法修改Map中已有的映射
    public void testModify(){
        System.out.println("请输入要修改学生的Id!");
        Scanner console=new Scanner(System.in);
        while(true){
        String stuID=console.next();
        Student student=students.get(stuID);
        if(student==null){
            System.out.println("该ID不存在!!");
            continue;
        }
        System.out.println("当前该学生ID,所对应的学生为:"+student.name);
        System.out.println("请输入新的学生姓名:");
        String name=console.next();
        Student newStudent=new Student(stuID,name);
        students.put(stuID, newStudent);
        System.out.println("修改成功!!!");
        break;
    }
    }

    //测试Map中是否包含某个KEY值或者某个Value值
    public void testContansKeyOrValue(){
        //在MAp中,用containsKey()方法判断是否包含某个key值
        //用containsValue()方法,来判断是否包含某个Value值
        System.out.println("请输入要查询学生的Id!");
        Scanner console=new Scanner(System.in);
        String id=console.next();
        System.out.println("您输入的学生ID为:"+id+",在学生映射中是否存在:"+students.containsKey(id));
        if(students.containsKey(id))
            System.out.println("对应的学生为:"+students.get(id).name);
        System.out.println("请输入要查询学生的姓名");
        String name=console.next();
        if(students.containsValue(new Student(null,name)))
            System.out.println("在映射表中确实包含学生:"+name);
        else
            System.out.println("在学生映射表中不存在该学生!");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       MapTest mt=new MapTest();
       mt.testPut();
       mt.testKeySet();
       //mt.testRemove();
       //mt.testEntrySet();
       //mt.testModify();
       //mt.testEntrySet();
       mt.testContansKeyOrValue();
    }

}

SetTest类

package com.collection_map;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SetTest {
    public List<Course> coursesToSelect;
    private Scanner console;
    public SetTest(){
        coursesToSelect=new ArrayList<Course>();
        console=new Scanner(System.in);
    }
    public void testAdd(){
       Course cr1=new Course("1","数据结构");
       coursesToSelect.add(cr1);
       Course temp=(Course)coursesToSelect.get(0);
       //System.out.println("添加了课程:"+temp.id+":"+temp.name);
       Course cr2=new Course("2","C语言");
       coursesToSelect.add(0,cr2);
       Course temp2=(Course)coursesToSelect.get(0);
       //System.out.println("添加了课程:"+temp2.id+":"+temp2.name);
       Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
       coursesToSelect.addAll(Arrays.asList(course));
       Course temp3=(Course)coursesToSelect.get(2);
       Course temp4=(Course)coursesToSelect.get(3);
       //System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name);
       Course[] course2={new Course("5","高等数学"),new Course("6","英语")};
       coursesToSelect.addAll(2,Arrays.asList(course2));
       Course temp5=(Course)coursesToSelect.get(2);
       Course temp6=(Course)coursesToSelect.get(3);
       //System.out.println("添加了两门课程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name);
    }
  //通过for each方法访问集合元素
    public void testForEach(){
       System.out.println("有如下课程可选(通过foreach):");
       for(Object obj:coursesToSelect){
           Course cr=(Course)obj;
           System.out.println("课程:"+cr.id+":"+cr.name);
       }
    }

    //测试List的contains方法
    public void testListContains(){
        Course course=coursesToSelect.get(0);
        System.out.println("取得课程:"+course.name);
        System.out.println("备选课程中是否包含课程:"+course.name+","+coursesToSelect.contains(course));
        System.out.println("请输入课程名称:");
        String name=console.next();
        Course course2=new Course(course.id,course.name);
        course.name=name;
        System.out.println("新创建课程:"+course2.name);
        System.out.println("备选课程中是否包含课程:"+course2.name+","+coursesToSelect.contains(course2));
         //通过index()方法来取得某元素的索引位置
        if(coursesToSelect.contains(course2))
            System.out.println("课程:"+course2.name+"的索引位置为:"+coursesToSelect.indexOf(course2));
        }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     SetTest st=new SetTest();
     st.testAdd();
     st.testListContains();
    st.testForEach();
//     Student student=new Student("1","小明");
//     System.out.println("欢迎学生:"+student.name+"选课!!");
//     //创建一个Scanner对象,用来接收从键盘输入的课程ID
//     Scanner console=new Scanner(System.in);
//     for(int i=0;i<3;i++){
//       System.out.println("请输入课程ID");
//       String courseId=console.next();
//       for(Course cr:st.coursesToSelect){
//           if(cr.id.equals(courseId)){
//               student.courses.add(cr);
//           }
//       }
//     }
//     //打印输出学生所选课程
//     //Set遍历只能用for each或者iterator
//     //Set中添加某个对象,无论添加多少次,最终只会保留一个该对象(第一次添加的)
//     for(Course cr:student.courses){
//       System.out.println("选择了课程:"+cr.id+":"+cr.name);
//     }
    }

}

StudentComparator类

package com.collection_map;

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student arg0, Student arg1) {
        // TODO Auto-generated method stub
        return arg0.name.compareTo(arg1.name);
    }

}

TestGenetic类

package com.collection_map;

import java.util.ArrayList;
import java.util.List;

public class TestGenetic {
    //带有泛型--Course的List类型属性
    public List<Course> courses;
    public TestGenetic(){
        this.courses=new ArrayList<Course>();
    }
    public void testAdd(){
        Course cr1=new Course("1","大学语文");
        courses.add(cr1);
        Course cr2=new Course("2","java");
        courses.add(cr2);
        //泛型集合中,不能添加泛型规定的类型及其子类型以外的对象,否则会报错
    }
    public void testForEach(){
        for(Course cr:courses){
            System.out.println(cr.id+":"+cr.name);
        }
    }
    //泛型结合可以添加泛型的子类型的对象实例
    public void testChild(){
        ChildCourse ccr=new ChildCourse();
        ccr.id="3";
        ccr.name="我是子类型的课程类型实例";
        courses.add(ccr);
    }
    //泛型不能使用基本类型
    public void testBasicType(){
        List<Integer>list=new ArrayList<Integer>();
        list.add(1);
        System.out.println("基本类型必须使用包装类作为泛型"+list.get(0));
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGenetic tg=new TestGenetic();
        tg.testAdd();
        tg.testForEach();
        tg.testChild();
        tg.testForEach();
        tg.testBasicType();
    }

}
5人推荐
随时随地看视频
慕课网APP