Java硬核程序设计实验“集合与泛型”详解
❥ 作为上学年 “Java硬核程序设计实验详解” 系列补充 ❥
目录
❀1. 题目:编写程序练习泛型和List集合的基本使用
❀2. 题目:编写程序练习泛型和Map集合的基本使用
❀3. 题目:按照教材[案例7-8]的思路,在其基础上设计一个图书购物车
❀4. 题目:在Eclipse 下完成如下实践项目 (选做题)
:star:️4. 架构&源代码:
:sunny:| 一、实验目的
1.理解集合的概念、体系结构、分类及使用场景
2.掌握Set接口及主要实现类
3.掌握List接口及主要实现类
4.掌握Map接口及主要实现类
5.理解泛型的作用,并掌握类型安全检查在集合中的应用
:sunny:| 二、实验内容
❀1. 题目:编写程序练习泛型和List集合的基本使用
(1)创建一个只能容纳String对象名为strNames的ArrayList集合;
(2)按顺序往集合中添加5个字符串对象:“信电学院”、“数理学院”、“机电学院”、“土木学院”、“食品学院”;
(3)对集合进行遍历(掌握几种遍历集合的方法),分别打印集合中的每个元素的位置与内容;
(4)首先打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。
(5)本题测试程序的名称为SY6_1_List,包名为com.xzit.sy6
☞ 思考 :
如果将第(1)中的ArrayList集合换成HashSet集合,程序有什么不同?编程测试。
实验结果:
若更换为Hashset则不能用普通for循环遍历
❀2. 题目:编写程序练习泛型和Map集合的基本使用
(1)创建一个Book类,该类包含图书的ISBN号、图书名称、作者、出版社、出版日期、图书价格等属性。
(2)创建一个Map<Key,Values>类型的对象booksmap,其中Key的类型为图书的ISBN号、Values类型为Book类对象。
(3)往booksmap集合中添加5个“键-值”对象,即五个ISBN号对应于五本书籍信息。
(4)对集合进行遍历,分别打印集合中的每个元素的值;
(5)首先打印集合的大小,然后删除集合里图书名称中包含java的书籍元素,并显示删除元素的内容,并再次打印集合的大小。
(6)本题测试程序的名称为SY6_2_Map,包名为com.xzit.sy6
实验结果:
❀3. 题目:按照教材[案例7-8]的思路,在其基础上设计一个图书购物车
存储用户购买图书的信息。能够打印出购物车中图书的信息,图书价格、小计和总的费用。用HashMap类模拟购物车。
实现思路指导:
(1)新建图书类(Product),该步骤第2题已实现。
(2)新建BookShop类,该类实现对所购图书信息的封装。
(3)新建书库类BookStore,书库里存放了大量的图书,这里我们用ArrayList来存放,并初始化部分图书信息。
(4)新建顾客类Customer。每个顾客进入书库都可以获得一个购物车,这个购物车用HashMap来实现,图书的编号是它的键,捆扎后的BookShop是它的值。
(5)新建测试主类SY6_3_BuyCar。
实验结果:
❀4. 题目:在Eclipse 下完成如下实践项目 (选做题)
1.[SY6_4.java]学生选课系统的设计与实现。一个学生可以选修多门课程、一门课程可以被多个学生选,但一门课程不能被一个学生选择多次。
功能要求:
(1)运行程序时,先显示一个顶层菜单,如下图6-1所示。
图6-1 顶级菜单
(2)各个菜单命令的含义:0表示退出程序,1表示学生登录,2表示管理员查看各门课程都被哪些学生选了。
(3)学生登录信息存放在StudentVDB类中类型为Map的Students对象中。在登录过程中,学号或密码输入不正确的次数有三次输入机会,如果三次都不成功,返回到如图7-1所示的界面,如果登录成功,显示如图6-2所示的界面。
图6-2 学生登录成功后的界面
(4)完成学生界面中各个命令对应的功能能。课程信息存放在CourseVDB类中。当学生输入“0”时返回图1界面,此时该学生的这次选课就结束了。可以等待下一个学生来进行登录。
设计要求:采用如下包结构,不能改变次包结构,但可以在其中增加新类或新建包。
(5)本实验测试主类为SY6_4.java
实验结果:
:sunny:| 三、实验结果
:star:️1. 源代码:
package EXPS.Exp06.com.xzit.sy6; import java.sql.SQLOutput; import java.util.ArrayList; import java.util.Iterator; /** * 班级:19软嵌2 * 学号:20190507223 * 姓名:夏旭 * 实验时间:2020-5-25 * 本程序的功能是:定义应该LIST,存取数据 */ public class SY6_1_LIST { public static void main(String[] args) { ArrayList<String> lis=new ArrayList<>(); lis.add("信电学院"); lis.add("数理学院"); lis.add("机电学院"); lis.add("土木学院"); lis.add("食品学院"); for(int i=0;i<lis.size();i++){ System.out.println("第"+(i+1)+"个数据:"+" "+lis.get(i)); } System.out.println("------------------"); Iterator iterator=lis.iterator(); int num=1; while (iterator.hasNext()){ String node=(String) iterator.next(); System.out.println("第"+(num++)+"个数据:"+" "+node); } System.out.println("----------"); num=1; for(String node: lis){ System.out.println("第"+(num++)+"个数据:"+" "+node); } System.out.println("----------"); System.out.println("当前集合大小:"+lis.size()); String node= lis.get(2); System.out.println("第三个元素为: "+node); lis.remove(2); System.out.println("更新后的第三个元素为 "+lis.get(2)+" "+"集合大小: "+lis.size()); } }
:star:️2. 源代码:
package EXPS.Exp06.com.xzit.sy6; import java.util.*; /** * 班级:19软嵌2 * 学号:20190507223 * 姓名:夏旭 * 实验时间:2020-5-25 * 本程序的功能是:利用Map模拟书籍分类 */ public class SY6_2_Map { public static void main(String[] args) { Map<String,Book> map=new HashMap<>(); map.put("01",new Book("01","java基础","A","清华出版社","2020-01",20.1f)); map.put("02",new Book("02","C++基础","B","清华出版社","2020-02",30.1f)); map.put("03",new Book("03","python基础","C","清华出版社","2020-03",40.1f)); map.put("04",new Book("04","javaScript基础","D","清华出版社","2020-04",50.1f)); map.put("05",new Book("05","pascal基础","E","清华出版社","2020-05",60.1f)); Iterator<String> iterator=map.keySet().iterator(); while(iterator.hasNext()){ String key=iterator.next(); Book node=map.get(key); System.out.println(node); } System.out.println("------------"); System.out.println("当前集合大小为"+map.size()); iterator=map.keySet().iterator(); List<String> delQue=new LinkedList<>(); while(iterator.hasNext()){ String key=iterator.next(); Book node=map.get(key); if(node.getName().contains("java")){ delQue.add(key); } } for(int i=0;i<delQue.size();i++){ System.out.println("删除的内容:"+map.get(delQue.get(i))); map.remove(delQue.get(i)); } System.out.println("更新后的大小:"+map.size()); } }
:star:️3. 源代码:
package EXPS.Exp06.com.xzit.sy6; /** * 班级:19软嵌2 * 学号:20190507223 * 姓名:夏旭 * 实验时间:2020-5-25 * 本程序的功能是:模拟购物车 */ public class SY6_3_BuyCar { public static void main(String[] args) { Customer customer=new Customer(); customer.put(BookStore.products.get(0)); customer.put(BookStore.products.get(1)); customer.put(BookStore.products.get(2)); customer.put(BookStore.products.get(3)); customer.put(BookStore.products.get(4)); customer.put(BookStore.products.get(2)); customer.list(); } }
:star:️4. 架构&源代码:
架构:
代码:
package EXPS.Exp06.Exam01.src.com.xzit.main; import EXPS.Exp06.Exam01.src.com.xzit.business.AdimBusiness; import EXPS.Exp06.Exam01.src.com.xzit.business.StudentBusiness; import EXPS.Exp06.Exam01.src.com.xzit.params.CourseVDB; import EXPS.Exp06.Exam01.src.com.xzit.params.StudentVDB; import EXPS.Exp06.Exam01.src.com.xzit.vo.Student; /** * 班级:19软嵌2 * 学号:20190507223 * 姓名:夏旭 * 实验时间:2020-5-25 * 本程序的功能是:模拟选课系统 */ import java.util.Scanner; public class Main { public StudentVDB studentVDB=new StudentVDB(); public CourseVDB courseVDB=new CourseVDB(); public Scanner sc=new Scanner(System.in); public static void main(String[] args) { new Main(); } public Main() { while (true){ printTopMenu(); int type= sc.nextInt(); if(type==0){ System.exit(0); } else if(type==1){ StudentBusiness studentBusiness=new StudentBusiness(this); if(studentBusiness.login()==true){ studentBusiness.running(); } } else if(type==2){ sc.skip("n"); System.out.println("请输入管理员口令"); String pw=sc.nextLine(); if(pw.equals("ImAdmin")){ AdimBusiness adimBusiness=new AdimBusiness(this); adimBusiness.running(); } else{ System.out.println("口令错误"); } } else{ System.out.println("非法输入"); } } } public static void printTopMenu(){ System.out.println("-------------顶级菜单---------------"); System.out.println("| 0-退出 1-学生登录 2-管理员操作 |"); System.out.println("-----------------------------------"); System.out.println("请输入菜单命令(输入数字)"); } } package EXPS.Exp06.Exam01.src.com.xzit.business; import EXPS.Exp06.Exam01.src.com.xzit.main.Main; import EXPS.Exp06.Exam01.src.com.xzit.vo.Course; import EXPS.Exp06.Exam01.src.com.xzit.vo.Student; public class AdimBusiness { public Main main; public AdimBusiness(Main main) { this.main = main; } public void running(){ System.out.println("------管理员模式------"); boolean flag=false; while (!flag){ this.printmenu(); int type=main.sc.nextInt(); main.sc.skip("n"); if(type==0){ flag=true; } else if(type==1){ System.out.println("输入该学生学号(ID)"); String ID=main.sc.nextLine(); int res=this.QueryStuSelectedCourse(ID); if(res==-1){ System.out.println("暂无已选课程"); } else if(res==0){ System.out.println("学生未找到"); } } else if(type==2){ System.out.println("输入课程号(ID)"); String ID=main.sc.nextLine(); int res=this.QueryCouSelectedByStu(ID); if(res==-1){ System.out.println("暂无已选课程"); } else if(res==0){ System.out.println("课程未找到"); } } else if(type==3){ System.out.println("添加或删除? 0-返回 1-添加 2-删除"); String mulType=main.sc.nextLine(); if(mulType.equals("1")){ System.out.println("输入要添加学生的学号(ID)"); String ID=main.sc.nextLine(); System.out.println("输入姓名"); String name=main.sc.nextLine(); System.out.println("设置密码"); String pw=main.sc.nextLine(); if(this.addStudent(ID,new Student(ID,name,pw))){ System.out.println("添加成功"); } else{ System.out.println("添加失败,账号已经存在"); } } else if(mulType.equals("2")){ System.out.println("输入要删除学生的学号(ID)"); String ID=main.sc.nextLine(); if(this.delStudent(ID)){ System.out.println("删除成功"); } else{ System.out.println("ID不存在"); } } } else if(type==4){ System.out.println("添加或删除? 0-返回 1-添加 2-删除"); String mulType=main.sc.nextLine(); if(mulType.equals("1")){ System.out.println("输入要添加课程课程号(ID)"); String ID=main.sc.nextLine(); System.out.println("输入课程名"); String name=main.sc.nextLine(); if(this.addCourse(ID,new Course(ID,name))){ System.out.println("添加成功"); } else{ System.out.println("添加失败,课程已经存在"); } } else if(mulType.equals("2")){ System.out.println("输入要删除课程的课程号(ID)"); String ID=main.sc.nextLine(); if(this.delCourse(ID)){ System.out.println("删除成功"); } else{ System.out.println("课程不存在"); } } } else{ System.out.println("非法输入"); } } } public void printmenu(){ System.out.println(""); System.out.println(""); System.out.println("|-=****************************管理员菜单************************=-|"); System.out.println("| 0-返回 1-查询指定学生已选课表 2-查询指定课程已选学生 3-添加(删除)学生 4-添加(删除)课程|"); System.out.println("-=**************************************************************=-"); } public int QueryStuSelectedCourse(String ID){ for(Student i:main.studentVDB.studentsDB){ if(i.getId().equals(ID)){ if (i.getCourse_Set().size()==0) return -1; for(Course j:i.getCourse_Set()){ System.out.println(j); } return 1; } } return 0; } public int QueryCouSelectedByStu(String ID){ for (Course i:main.courseVDB.courseDB){ if(i.getID().equals(ID)){ if(i.getStudents_Set().size()==0) return -1; for(Student j:i.getStudents_Set()){ System.out.println(j); } return 1; } } return 0; } public boolean addStudent(String ID,Student newNode){ for(Student i:main.studentVDB.studentsDB){ if(i.getId().equals(ID)){ return false; } } main.studentVDB.studentsDB.add(newNode); return true; } public boolean delStudent(String ID){ return main.studentVDB.studentsDB.remove(ID); } public boolean addCourse(String ID,Course newNode){ for(Course i:main.courseVDB.courseDB){ if(i.getID().equals(ID)){ return false; } } main.courseVDB.courseDB.add(newNode); return true; } public boolean delCourse(String ID){ return main.courseVDB.courseDB.remove(ID); } } package EXPS.Exp06.Exam01.src.com.xzit.business; import EXPS.Exp06.Exam01.src.com.xzit.main.Main; import EXPS.Exp06.Exam01.src.com.xzit.vo.Course; import EXPS.Exp06.Exam01.src.com.xzit.vo.Student; import java.util.HashSet; public class StudentBusiness { public Main main; public Student nowUser=null; public StudentBusiness(Main main) { this.main = main; } public StudentBusiness() { } public void running(){ System.out.println("欢迎同学"+nowUser.getName()); boolean flag=false; while (flag==false){ this.printMenu(); int type=main.sc.nextInt(); main.sc.skip("n"); if(type==0){ flag=true; } else if(type==1){ System.out.println("输入查询类型 1-所有选修课 2-已选选修课"); int queryType=main.sc.nextInt(); QueryCourseList(queryType); } else if(type==2){ System.out.println("请输入选修课程课程号"); String courseID=main.sc.next(); if(addSelectCourse(courseID)==true){ System.out.println("添加成功"); } else{ System.out.println("添加失败,请检查是否已添加或课程不存在"); } } else if(type==3){ System.out.println("确定吗? 输入Y确定,否则返回"); String confirm=main.sc.nextLine(); if(confirm.equals("Y")){ nowUser.setCourse_Set(new HashSet<>()); System.out.println("删除完成"); } } else if(type==4){ System.out.println("请输入要删除的选修课程课程号"); String courseID=main.sc.next(); if(delSelectedCourse(courseID)==true){ System.out.println("删除成功"); } else{ System.out.println("删除失败,请检查是否已经选修该课程"); } } else{ System.out.println("无效的输入"); } } } public void printMenu(){ System.out.println(""); System.out.println(""); System.out.println("|-=****************************学生菜单************************=-|"); System.out.println("| 0-返回 1-查询选课表 2-添加选修课程 3-全部删除已选课程 4-删除某门课程|"); System.out.println("-=**************************************************************=-"); } public boolean login(){ int count=3; main.sc.skip("n"); while (count>0) { System.out.println("请输入账号(学号):"); String username=main.sc.nextLine(); System.out.println("请输入密码:"); String password=main.sc.nextLine(); int flag=test_login(username,password); if(flag==1) return true; else if(flag==0) System.out.println("密码错误,你还有"+(--count)+"次机会"); else System.out.println("用户不存在,你还有"+(--count)+"次机会"); if(count==0) System.out.println("退回至主界面"); } return false; } public int test_login(String username,String password){ for (Student i:this.main.studentVDB.studentsDB) { String ID=i.getId(); if(username.equals(ID)){ if(i.getPassword().equals(password)){ nowUser=i; return 1; } return 0; } } return -1; } public void QueryCourseList(int type){ if (type==1) { if(main.courseVDB.courseDB.size()==0){ System.out.println("暂无课程"); return; } for(Course i:main.courseVDB.courseDB){ System.out.println(i); } } else { if(nowUser.getCourse_Set().size()==0){ System.out.println("暂无已选课程"); return; } else{ for (Course i:nowUser.getCourse_Set()){ System.out.println(i); } } } } public boolean addSelectCourse(String s){ HashSet<Course> selectde=nowUser.getCourse_Set(); for (Course i:selectde){ if(i.getID().equals(s)) return false; } for (Course i:main.courseVDB.courseDB){ if(i.getID().equals(s)){ i.getStudents_Set().add(nowUser); nowUser.getCourse_Set().add(i); return true; } } return false; } public boolean delSelectedCourse(String s){ HashSet<Course> selectde=nowUser.getCourse_Set(); Course node=null; for (Course i:selectde){ if(i.getID().equals(s)) { node=i; } } if (node!=null){ nowUser.getCourse_Set().remove(node); return true; } return false; } } package EXPS.Exp06.Exam01.src.com.xzit.params; import EXPS.Exp06.Exam01.src.com.xzit.vo.Course; import java.util.ArrayList; import java.util.List; public class CourseVDB { public static List<Course> courseDB=new ArrayList<>(); static { courseDB.add(new Course("xz01","C++基础")); courseDB.add(new Course("xz02","Java基础")); courseDB.add(new Course("xz03","Python基础")); courseDB.add(new Course("xz04","Pascal基础")); courseDB.add(new Course("xz05","Kotlin基础")); courseDB.add(new Course("xz06","go基础")); } } package EXPS.Exp06.Exam01.src.com.xzit.params; import EXPS.Exp06.Exam01.src.com.xzit.vo.Student; import java.util.ArrayList; import java.util.List; public class StudentVDB { public static List<Student> studentsDB=new ArrayList<>(); static { studentsDB.add(new Student("201901","赵一","201901")); studentsDB.add(new Student("201902","钱二","201902")); studentsDB.add(new Student("201903","孙三","201903")); studentsDB.add(new Student("201904","李四","201904")); studentsDB.add(new Student("201905","周五","201905")); studentsDB.add(new Student("201906","吴六","201906")); studentsDB.add(new Student("201907","郑七","201907")); studentsDB.add(new Student("201908","王八","201908")); } } package EXPS.Exp06.Exam01.src.com.xzit.vo; import java.util.HashSet; public class Course { private String ID; private String name; private HashSet<Student> students_Set; public Course() { } public Course(String ID, String name) { this.ID = ID; this.name = name; this.students_Set=new HashSet<>(); } 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 HashSet<Student> getStudents_Set() { return students_Set; } public void setStudents_Set(HashSet<Student> students_Set) { this.students_Set = students_Set; } @Override public String toString() { return "课程号: "+ID+"t"+"课程名" +name; } } package EXPS.Exp06.Exam01.src.com.xzit.vo; import java.util.HashSet; public class Student { private String Id; private String name; private String password; private HashSet<Course> course_Set; public Student() { } public Student(String id,String name, String password) { this.Id = id; this.password = password; this.name=name; this.course_Set=new HashSet<>(); } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public HashSet<Course> getCourse_Set() { return course_Set; } public void setCourse_Set(HashSet<Course> course_Set) { this.course_Set = course_Set; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "学号: "+Id+"t"+"姓名 :"+name; } }
四、实验总结
通过这次实验,我理解了集合的概念、体系结构、分类及使用场景;掌握了Set接口及主要实现类;List接口及主要实现类;Map接口及主要实现类。
理解了泛型的作用,并掌握类型安全检查在集合中的应用。