最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)選課系統(tǒng)

 更新時間:2019年02月20日 10:06:24   作者:wrxingkong  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)選課系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)選課系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

這個程序主要是練習(xí)IO(文件讀寫,序列化),集合框架的使用

學(xué)生端可以實(shí)現(xiàn),查找課程,增加選課,刪除選課的功能

管理端可以實(shí)現(xiàn)對備選課程,學(xué)生信息的增刪查改

缺點(diǎn):登陸操作沒有實(shí)現(xiàn)密碼驗證和多態(tài)。           

另外map對象明明put了,可是get的時候竟然會取到null,而且嘗試多次,有時候成功,有時候取到null,并不確定。據(jù)說這是由多線程引起的map取值為null,因為多線程部分還沒開始學(xué)習(xí),所以也沒做修改。

//課程信息
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;
 }
 
}
//學(xué)生信息
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 "學(xué)號:"+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 {
//管理端,用來管理學(xué)生信息和備選課程 
 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();
 }
 
 }
 
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根據(jù)List來創(chuàng)建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));
 }
 }
 
 // ~~~~~~~~~~~~~~~~~~~~~~ 增刪查改~~~~~~~~~~~~~~~~~~~~~~~
 // 增加學(xué)生基本信息
 public void add() {
 System.out.println("輸入學(xué)生信息,輸入0結(jié)束");
 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;
  }
 }
 }
 
 // 刪除學(xué)生信息
 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結(jié)束");
 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("添加結(jié)束");
  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;
  }
 }
 }
 
 // 根據(jù)學(xué)號查找學(xué)生
 public void query1() {
 System.out.println("請輸入要查詢的學(xué)生學(xué)號:");
 if (in.hasNext()) {
  int id = in.nextInt();
  System.out.println(map1.get(id));
  map1.get(id).travese();
 }
 }
 
 // 根據(jù)課程號查找課程
 public void query2() {
 System.out.println("請輸入要查詢的課程號:");
 if (in.hasNext()) {
  String id = in.nextLine();
  System.out.println(map2.get(id));
 }
 }
 
 // 修改學(xué)生基本信息
 public void modify1() {
 System.out.println("請輸入要修改的學(xué)生的學(xué)號:");
 int id = in.nextInt();
 Student s = map1.get(id);
 System.out.println("輸入修改后的學(xué)生信息:");
 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("所有的學(xué)生信息:");
 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();
}
}
//學(xué)生操作端
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();
 }
 
 }
 
//~~~~~~~~~~~~~~~~~~~~~~~~~ 根據(jù)List來創(chuàng)建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());
 }
 }
 
//根據(jù)課程號查詢備選課程
 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結(jié)束");
 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("添加結(jié)束");
  break;
  }
 }
 }
 
//減少所選課程
 public void deleteSelect() {
 System.out.println("要刪除課程的課程號,輸入end結(jié)束");
 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("添加結(jié)束");
  break;
  }
 }
 }
 
 public void close() {
 in.close();
 }
}
//測試類
package selectCourse;
 
import java.util.Scanner;
 
public class Test {
 
 public static void main(String[] args) {
//~~~~~~~~~~~~~測試管理端~~~~~~~~~~~~~~~~~~~~~~~~~~
   /*添加學(xué)生
 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();*/
//~~~~~~~~~~~~~~~~測試學(xué)生端~~~~~~~~~~~~~~~~~~~~~~~~~
  /*Scanner in=new Scanner(System.in);
  System.out.println("請輸入學(xué)號:");
  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();
*/ }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • jenkins如何通過pipeline部署springboot項目

    jenkins如何通過pipeline部署springboot項目

    為了提高SpringBoot項目的部署效率和規(guī)范性,建議將項目代碼和部署腳本分離,項目代碼倉庫專注業(yè)務(wù)邏輯,構(gòu)建為jar包;另外設(shè)立獨(dú)立代碼倉庫存放Jenkinsfile等部署配置文件,在Jenkins中配置pipeline,自動拉取項目代碼進(jìn)行構(gòu)建和部署
    2024-09-09
  • Java?輕松掌握字符緩沖流的使用

    Java?輕松掌握字符緩沖流的使用

    這篇文章主要介紹了Java的字符緩沖流用法,字符緩沖流的用途很多,主要是幾個構(gòu)造方法的使用,在項目開發(fā)中經(jīng)常會用到,需要的朋友參考下吧
    2022-04-04
  • java中的構(gòu)造函數(shù)什么時候被調(diào)用執(zhí)行

    java中的構(gòu)造函數(shù)什么時候被調(diào)用執(zhí)行

    這篇文章主要介紹了java中的構(gòu)造函數(shù)什么時候被調(diào)用執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java防鎖屏小程序代碼實(shí)例

    Java防鎖屏小程序代碼實(shí)例

    這篇文章主要介紹了Java防鎖屏小程序代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • 基于Java實(shí)現(xiàn)簡易的七星彩號碼生成器

    基于Java實(shí)現(xiàn)簡易的七星彩號碼生成器

    七星彩是中國體育彩票的一種玩法,由中國國家體育總局體育彩票管理中心統(tǒng)一發(fā)行。本文為大家準(zhǔn)備了一個七星彩號碼生成器Java工具類,感興趣的可以了解一下
    2022-08-08
  • Java的jstack命令使用示例詳解

    Java的jstack命令使用示例詳解

    jstack 命令非常的簡單,我們可以通過 jstack -h 或者 jstack -help 命令查看它的用法詳情,今天通過本文重點(diǎn)給大家介紹Java的jstack命令使用,感興趣的朋友一起看看吧
    2022-03-03
  • 解決pageHelper分頁失效以及如何配置問題

    解決pageHelper分頁失效以及如何配置問題

    這篇文章主要介紹了解決pageHelper分頁失效以及如何配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring 注解編程模型相關(guān)知識詳解

    Spring 注解編程模型相關(guān)知識詳解

    這篇文章主要介紹了Spring 注解編程模型相關(guān)知識詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • 淺談Java中注解Annotation的定義、使用、解析

    淺談Java中注解Annotation的定義、使用、解析

    下面小編就為大家?guī)硪黄獪\談Java中注解Annotation的定義、使用、解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    這篇文章主要介紹了springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12

最新評論

金华市| 即墨市| 建宁县| 兖州市| 宁津县| 满城县| 安宁市| 邳州市| 东兴市| 溧阳市| 长岛县| 彭山县| 青河县| 厦门市| 仙桃市| 玛多县| 西藏| 嘉鱼县| 铜川市| 河北省| 嘉祥县| 连江县| 东港市| 铜陵市| 资溪县| 招远市| 定襄县| 万荣县| 张家口市| 安庆市| 若羌县| 旺苍县| 乌兰浩特市| 襄汾县| 文安县| 温宿县| 吉木萨尔县| 宁德市| 射阳县| 前郭尔| 博白县|