目录
前言
代码
总结
前言
实验目的与要求 | 了解接口与多态的应用场景,熟练掌握接口的定义与实现类的编写,了解面向对象的设计原则,能够灵活应用接口、继承和组合构建更具弹性的软件架构,初步体验面向对象设计原则应用对于提升程序质量的好处。 |
实验内容 | 以赛促学,学校非常重视课外科技竞赛,参加课外科技竞赛是非常重要的第二课堂活动,课外科技竞赛赛事众多,有ACM算法类竞赛,有“互联网+”创新创业类竞赛等,不同同学关注不同比赛(可能关注多个赛事),编程模拟实现赛事发布与接收,要求实现赛事精准推送(即只要发布赛事,学生只会自动收到关注的赛事信息且不会遗漏) |
这里我是先创建学生名字,再询问学生要关注的赛事(这里使用map存),最后学校发布赛事,遍历这里的map,通过key比对每个学生的关注是否与发布的赛事相同,相同则将相同的赛事信息与Student放进新的map中,最后显示出来。
代码
Student:定义Student
public class Student {
private String name;
public Student(String name) {
// TODO Auto-generated method stub
this.name = name;
}
public String getName(String name) {
// TODO Auto-generated method stub
return name;
}
@Override
public String toString() {
return name;
}
}
Competition:定义Competition
public class Competition{
private String cname;
private String inf;
public Competition(String cname, String inf) {
// TODO Auto-generated method stub
this.inf = inf;
this.cname = cname;
}
public String getName(String cname) {
// TODO Auto-generated method stub
return cname;
}
public String getNo(String inf) {
// TODO Auto-generated method stub
return inf;
}
@Override
public String toString() {
return "赛名:'" + cname + '\'' + ", 信息:'" + inf + '\'' ;
}
}
Create:用于创建学生和学校发布竞赛
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Create {
List<Student> students = new ArrayList<>();
List<Competition> competitions = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
public List<Student> createStudent() {
System.out.println("开始创建学生");
int num = 1;
System.out.println("请输入学生" + num + "的名字:");
num++;
String sname;
while (!(sname = scanner.nextLine()).isEmpty()) { // 输入完毕直接按回车
System.out.println("请输入学生" + num + "的名字:");
Student student = new Student(sname);
students.add(student);
num++;
}
System.out.println("创建学生结束");
return students;
}
public List<Competition> createCompetition() { //这里可以创建重复的竞赛
System.out.println("您选择创建多少个竞赛?");
int comGeShu = scanner.nextInt();
for (int i = 0; i < comGeShu; i++) {
String cname = get();
System.out.println("请输入竞赛" + cname + "的简介:");
String inf = scanner.next();
Competition competition = new Competition(cname, inf);
competitions.add(competition);
}
return competitions;
}
public String get() {
System.out.println("-------------------------");
System.out.println(" 请选择竞赛 ");
System.out.println("1.-- 天梯赛 --");
System.out.println("2.-- 中国计算机设计大赛 --");
System.out.println("3.-- 蓝桥杯个人赛软件类 --");
System.out.println("4.-- “互联网+”比赛 --");
System.out.println("-------------------------");
System.out.println("给出您的选择:");
int competition = scanner.nextInt();
String cname = null;
if (competition == 1) {
cname = "天梯赛";
} else if (competition == 2) {
cname = "中国计算机设计大赛";
} else if (competition == 3) {
cname = "蓝桥杯个人赛软件类";
} else if (competition == 4) {
cname = "互联网比赛";
} else {
System.out.println("未找到该竞赛");
}
return cname;
}
}
Choose:学生选择关注的赛事
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Choose { //学生选择收藏
List<Student> students = new ArrayList<>();
static HashMap<Student, List<String>> want = new HashMap<>();
static Scanner scanner = new Scanner(System.in);
public Choose(List<Student> students) {
this.students = students;
}
public HashMap<Student, List<String>> ChooseAll() { // 把学生和他的关注列表放入map
for (int i = 0; i < students.size(); i++) {
List<String> oneChoose = new ArrayList<>();
oneChoose = find(i);
Student sname = students.get(i);
want.put(sname, oneChoose);
}
return want;
}
public List<String> find(int i) { // 选择单个学生想要关注的竞赛并放入列表
List<String> competitions = new ArrayList<>();
System.out.println("");
System.out.println("选择" + students.get(i) + "想关注的竞赛");
System.out.println("-------------------------");
System.out.println("1.-- 天梯赛 --");
System.out.println("2.-- 中国计算机设计大赛 --");
System.out.println("3.-- 蓝桥杯个人赛软件类 --");
System.out.println("4.-- “互联网+”比赛 --");
System.out.println("-------------------------");
System.out.println("");
System.out.println("给出您的选择:");
String competition;
while (!(competition = scanner.nextLine()).isEmpty()) {
System.out.println("给出您的选择:");
if (competition.equals("1")) {
competitions.add("天梯赛");
} else if (competition.equals("2")) {
competitions.add("中国计算机设计大赛");
} else if (competition.equals("3")) {
competitions.add("蓝桥杯个人赛软件类");
} else if (competition.equals("4")) {
competitions.add("互联网比赛");
} else {
System.out.println("未找到该竞赛");
}
}
return competitions;
}
public Set<Student> getKeys() {
Set<Student> stu = new HashSet<>();
stu.addAll(want.keySet());
return stu;
}
Main:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static Create create = new Create();
static List<Student> students = new ArrayList<>();
static List<Competition> competitions = new ArrayList<>();
static HashMap<Student, List<String>> want = new HashMap<>();
static HashMap<Student, List<Competition>> message = new HashMap<>();
public static void main(String[] args) {
students = create.createStudent(); // 创建student
Choose choose = new Choose(students);
want = choose.ChooseAll(); // 创建map以学生为key 以想要的竞赛为value(list)
competitions = create.createCompetition(); // 学校发布competition
System.out.println("学校发布的赛事 :" + competitions.toString());
Set<Student> keys = choose.getKeys();
for (Student key : keys) {
List<Competition> repeat = new ArrayList<>();
repeat = receipt(key);
message.put(key, repeat);
}
Set<Student> Mkeys = getMkeys();
for (Student key : Mkeys) {
System.out.println("学生" + key + " 关注的赛事 :" + message.get(key).toString());
}
}
public static List<Competition> receipt(Student key) {
List<String> values = new ArrayList<>(); // 新list存value
values.addAll(want.get(key));
Competition rep = null;
List<Competition> repeat = new ArrayList<>(); // 重复的赛事信息
for (int i = 0; i < values.size(); i++) { // 从收藏夹找
int flag=0;
for (int j = 0; j < competitions.size() && flag==0; j++) {
String Index = values.get(i);
String queryString = competitions.get(j).toString();
Pattern pattern = Pattern.compile(Index); // 从所有竞赛检索
Matcher matcher = pattern.matcher(queryString);
while (matcher.find()) {
rep = competitions.get(j);
repeat.add(rep);
flag=1;
}
}
}
return repeat;
}
public static Set<Student> getMkeys() {
Set<Student> stu = new HashSet<>();
stu.addAll(message.keySet());
return stu;
}
}
总结
方法与类的定义不够简洁严谨,结构较复杂且一些输入格式的错误未解决。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)