这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
zh:courses:java2025:ch06 [2025/03/23 20:33] pzczxs 创建 |
zh:courses:java2025:ch06 [2025/04/04 11:26] (当前版本) pzczxs 讨论状态变化了 |
||
---|---|---|---|
行 1: | 行 1: | ||
====== 第六章:集合类 ====== | ====== 第六章:集合类 ====== | ||
===== 课件 ===== | ===== 课件 ===== | ||
- | 下载:集合类 | + | 下载:{{ :zh:courses:java2025:ch06.pptx |集合类}} |
===== List的实现类:ArrayList类 ===== | ===== List的实现类:ArrayList类 ===== | ||
行 736: | 行 736: | ||
===== 上机作业 ===== | ===== 上机作业 ===== | ||
- | * 建立一个Corpus类,用于存储专利文献集合(每篇专利文献的信息见[[zh:courses:java2024:ch05#上机作业|第五章最后一道上机作业]]),建立查询和删除方法,可根据专利申请号、申请日期、IPC分类号和CPC分类号查询和删除相应专利文献。 | + | * 建立一个Corpus类,用于存储专利文献集合(每篇专利文献的信息见[[zh:courses:java2025:ch05#上机作业|第五章最后一道上机作业]]),建立查询和删除方法,可根据专利申请号、申请日期、IPC分类号和CPC分类号查询和删除相应专利文献。 |
- | 【参考答案一】 | + | |
- | <file java Classification.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Classification { | + | |
- | protected char section; | + | |
- | protected int mainClass; | + | |
- | protected char subclass; | + | |
- | protected int mainGroup; | + | |
- | protected int subgroup; | + | |
- | + | ||
- | public final static char SEPARATOR = '/'; | + | |
- | + | ||
- | public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | this.section = section; | + | |
- | this.mainClass = mainClass; | + | |
- | this.subclass = subclass; | + | |
- | this.mainGroup = mainGroup; | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | // example: G09F 17/30 | + | |
- | public Classification(String symbol) { | + | |
- | this.section = symbol.charAt(0); | + | |
- | this.mainClass = Integer.parseInt(symbol.substring(1, 3)); | + | |
- | this.subclass = symbol.charAt(3); | + | |
- | + | ||
- | int pos = symbol.indexOf(SEPARATOR); | + | |
- | this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim()); | + | |
- | this.subgroup = Integer.parseInt(symbol.substring(pos + 1)); | + | |
- | } | + | |
- | + | ||
- | public char getSection() { | + | |
- | return section; | + | |
- | } | + | |
- | + | ||
- | public void setSection(char section) { | + | |
- | this.section = section; | + | |
- | } | + | |
- | + | ||
- | public int getMainClass() { | + | |
- | return mainClass; | + | |
- | } | + | |
- | + | ||
- | public void setMainClass(int mainClass) { | + | |
- | this.mainClass = mainClass; | + | |
- | } | + | |
- | + | ||
- | public char getSubclass() { | + | |
- | return subclass; | + | |
- | } | + | |
- | + | ||
- | public void setSubclass(char subclass) { | + | |
- | this.subclass = subclass; | + | |
- | } | + | |
- | + | ||
- | public int getMainGroup() { | + | |
- | return mainGroup; | + | |
- | } | + | |
- | + | ||
- | public void setMainGroup(int mainGroup) { | + | |
- | this.mainGroup = mainGroup; | + | |
- | } | + | |
- | + | ||
- | public int getSubgroup() { | + | |
- | return subgroup; | + | |
- | } | + | |
- | + | ||
- | public void setSubgroup(int subgroup) { | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Classification other = (Classification) obj; | + | |
- | if (mainClass != other.mainClass || mainGroup != other.mainGroup || | + | |
- | section != other.section || subclass != other.subclass || subgroup != other.subgroup) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(section); | + | |
- | + | ||
- | sb.append(String.format("%2s", mainClass).replace(' ', '0')); | + | |
- | + | ||
- | sb.append(subclass); | + | |
- | + | ||
- | sb.append(String.format("%4s", mainGroup)); | + | |
- | + | ||
- | sb.append(SEPARATOR); | + | |
- | + | ||
- | sb.append(subgroup); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Ipc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Ipc extends Classification { | + | |
- | + | ||
- | public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | } | + | |
- | + | ||
- | public Ipc(String symbol) { | + | |
- | super(symbol); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | char section = 'A'; | + | |
- | int mainClass = 4; | + | |
- | char subclass = 'B'; | + | |
- | int mainGroup = 55; | + | |
- | int subgroup = 56; | + | |
- | + | ||
- | Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | System.out.println(ipc); | + | |
- | + | ||
- | String symbol = "G09F 17/30"; | + | |
- | System.out.println(new Ipc(symbol)); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Cpc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Cpc extends Classification { | + | |
- | private char position; | + | |
- | private char value; | + | |
- | + | ||
- | public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public Cpc(String symbol, char position, char value) { | + | |
- | super(symbol); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public char getPosition() { | + | |
- | return position; | + | |
- | } | + | |
- | + | ||
- | public void setPosition(char position) { | + | |
- | this.position = position; | + | |
- | } | + | |
- | + | ||
- | public char getValue() { | + | |
- | return value; | + | |
- | } | + | |
- | + | ||
- | public void setValue(char value) { | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (!super.equals(obj)) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | if (getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Cpc other = (Cpc) obj; | + | |
- | if (position != other.position || value != other.value) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Date.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Date { | + | |
- | private int year, month, day; | + | |
- | + | ||
- | public Date(int year, int month, int day) { | + | |
- | this.year = year; | + | |
- | this.month = month; | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | // format: YYYY-MM-DD | + | |
- | public Date(String str) { | + | |
- | String[] parts = str.trim().split("-"); | + | |
- | this.year = Integer.parseInt(parts[0]); | + | |
- | this.month = Integer.parseInt(parts[1]); | + | |
- | this.day = Integer.parseInt(parts[2]); | + | |
- | } | + | |
- | + | ||
- | public int getYear() { | + | |
- | return year; | + | |
- | } | + | |
- | + | ||
- | public void setYear(int year) { | + | |
- | this.year = year; | + | |
- | } | + | |
- | + | ||
- | public int getMonth() { | + | |
- | return month; | + | |
- | } | + | |
- | + | ||
- | public void setMonth(int month) { | + | |
- | this.month = month; | + | |
- | } | + | |
- | + | ||
- | public int getDay() { | + | |
- | return day; | + | |
- | } | + | |
- | + | ||
- | public void setDay(int day) { | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Date other = (Date) obj; | + | |
- | if (day != other.day || month != other.month || year != other.year) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(year); | + | |
- | if (month < 10) { | + | |
- | sb.append("0" + month); | + | |
- | } else { | + | |
- | sb.append(month); | + | |
- | } | + | |
- | if (day < 10) { | + | |
- | sb.append("0" + day); | + | |
- | } else { | + | |
- | sb.append(day); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Patent.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Patent { | + | |
- | private String applicationNo; | + | |
- | private Date applicationDate; | + | |
- | private Ipc[] ipcs; | + | |
- | private Cpc[] cpcs; | + | |
- | + | ||
- | private final static String SEPERATOR = ","; | + | |
- | + | ||
- | public Patent(String applicationNo, Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | this.applicationDate = applicationDate; | + | |
- | this.ipcs = ipcs; | + | |
- | this.cpcs = cpcs; | + | |
- | } | + | |
- | + | ||
- | public Patent(String applicationNo, String applicationDate, String multiIpcs, String multiCpcs, char position, char value) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | this.applicationDate = new Date(applicationDate); | + | |
- | + | ||
- | String[] ipcSymbols = multiIpcs.split(SEPERATOR); | + | |
- | this.ipcs = new Ipc[ipcSymbols.length]; | + | |
- | for (int i = 0; i < ipcSymbols.length; i++) { | + | |
- | ipcs[i] = new Ipc(ipcSymbols[i].trim()); | + | |
- | } | + | |
- | + | ||
- | String[] cpcSymbols = multiCpcs.split(SEPERATOR); | + | |
- | this.cpcs = new Cpc[cpcSymbols.length]; | + | |
- | for (int i = 0; i < cpcSymbols.length; i++) { | + | |
- | cpcs[i] = new Cpc(cpcSymbols[i].trim(), position, value); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public String getApplicationNo() { | + | |
- | return applicationNo; | + | |
- | } | + | |
- | + | ||
- | public void setApplicationNo(String applicationNo) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | } | + | |
- | + | ||
- | public Date getApplicationDate() { | + | |
- | return applicationDate; | + | |
- | } | + | |
- | + | ||
- | public void setApplicationDate(Date applicationDate) { | + | |
- | this.applicationDate = applicationDate; | + | |
- | } | + | |
- | + | ||
- | public Ipc[] getIpcs() { | + | |
- | return ipcs; | + | |
- | } | + | |
- | + | ||
- | public void setIpcs(Ipc[] ipcs) { | + | |
- | this.ipcs = ipcs; | + | |
- | } | + | |
- | + | ||
- | public Cpc[] getCpcs() { | + | |
- | return cpcs; | + | |
- | } | + | |
- | + | ||
- | public void setCpcs(Cpc[] cpcs) { | + | |
- | this.cpcs = cpcs; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(applicationNo + "\t" + applicationDate + "\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | for (int i = 0; i < ipcs.length - 1; i++) { | + | |
- | sb.append(ipcs[i] + SEPERATOR); | + | |
- | } | + | |
- | sb.append(ipcs[ipcs.length - 1] + "]\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | for (int i = 0; i < cpcs.length - 1; i++) { | + | |
- | sb.append(cpcs[i] + SEPERATOR); | + | |
- | } | + | |
- | sb.append(cpcs[cpcs.length - 1] + "]"); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java CorpusList.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | import java.util.List; | + | |
- | + | ||
- | public class CorpusList { | + | |
- | private List<Patent> data; | + | |
- | + | ||
- | public CorpusList() { | + | |
- | this.data = new ArrayList<Patent>(); | + | |
- | } | + | |
- | + | ||
- | public CorpusList(List<Patent> data) { | + | |
- | this.data = data; | + | |
- | } | + | |
- | + | ||
- | public CorpusList(Patent[] patents) { | + | |
- | this(); | + | |
- | + | ||
- | for (Patent patent: patents) { | + | |
- | data.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public void add(Patent patent) { | + | |
- | data.add(patent); | + | |
- | } | + | |
- | + | ||
- | public int size() { | + | |
- | return data.size(); | + | |
- | } | + | |
- | + | ||
- | public boolean contains(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Ipc[] ipcs = patent.getIpcs(); | + | |
- | for (Ipc e: ipcs) { | + | |
- | if (e.equals(ipc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Cpc[] cpcs = patent.getCpcs(); | + | |
- | for (Cpc e: cpcs) { | + | |
- | if (e.equals(cpc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationDate().equals(applicationDate)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Ipc[] ipcs = patent.getIpcs(); | + | |
- | for (Ipc e: ipcs) { | + | |
- | if (e.equals(ipc)) { | + | |
- | corpus.add(patent); | + | |
- | break; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Cpc[] cpcs = patent.getCpcs(); | + | |
- | for (Cpc e: cpcs) { | + | |
- | if (e.equals(cpc)) { | + | |
- | corpus.add(patent); | + | |
- | break; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationDate().equals(applicationDate)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (!patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | boolean flag = false; | + | |
- | Ipc[] ipcs = patent.getIpcs(); | + | |
- | for (Ipc e: ipcs) { | + | |
- | if (e.equals(ipc)) { | + | |
- | flag = true; | + | |
- | break; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | if (!flag) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | boolean flag = false; | + | |
- | Cpc[] cpcs = patent.getCpcs(); | + | |
- | for (Cpc e: cpcs) { | + | |
- | if (e.equals(cpc)) { | + | |
- | flag = true; | + | |
- | break; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | if (!flag) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (!patent.getApplicationDate().equals(applicationDate)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | sb.append(patent + "\n"); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | Patent[] patents = new Patent[5]; | + | |
- | patents[0] = new Patent("14725838", "2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I'); | + | |
- | patents[1] = new Patent("14814205", "2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I'); | + | |
- | patents[2] = new Patent("15189723", "2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A'); | + | |
- | patents[3] = new Patent("14711011", "2015-05-13", "B32B 17/10, B32B 27/32", "G06F 3/485", 'L', 'A'); | + | |
- | patents[4] = new Patent("14515267", "2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I'); | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(patents); | + | |
- | System.out.println(corpus); | + | |
- | + | ||
- | CorpusList corpusFound = corpus.find(new Date("2016-06-22")); | + | |
- | System.out.println(corpusFound); | + | |
- | + | ||
- | CorpusList corpusAfterRemovingIpc = corpus.remove(new Ipc("F16F 15/02")); | + | |
- | System.out.println(corpusAfterRemovingIpc); | + | |
- | + | ||
- | CorpusList corpusAfterRemovingCpc =corpus.remove(new Cpc("A23B 5/04", 'F', 'I')); | + | |
- | System.out.println(corpusAfterRemovingCpc); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | 【参考答案二】 | + | |
- | <file java Classification.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Classification { | + | |
- | protected char section; | + | |
- | protected int mainClass; | + | |
- | protected char subclass; | + | |
- | protected int mainGroup; | + | |
- | protected int subgroup; | + | |
- | + | ||
- | public final static char SEPARATOR = '/'; | + | |
- | + | ||
- | public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | this.section = section; | + | |
- | this.mainClass = mainClass; | + | |
- | this.subclass = subclass; | + | |
- | this.mainGroup = mainGroup; | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | // example: G09F 17/30 | + | |
- | public Classification(String symbol) { | + | |
- | this.section = symbol.charAt(0); | + | |
- | this.mainClass = Integer.parseInt(symbol.substring(1, 3)); | + | |
- | this.subclass = symbol.charAt(3); | + | |
- | + | ||
- | int pos = symbol.indexOf(SEPARATOR); | + | |
- | this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim()); | + | |
- | this.subgroup = Integer.parseInt(symbol.substring(pos + 1)); | + | |
- | } | + | |
- | + | ||
- | public char getSection() { | + | |
- | return section; | + | |
- | } | + | |
- | + | ||
- | public void setSection(char section) { | + | |
- | this.section = section; | + | |
- | } | + | |
- | + | ||
- | public int getMainClass() { | + | |
- | return mainClass; | + | |
- | } | + | |
- | + | ||
- | public void setMainClass(int mainClass) { | + | |
- | this.mainClass = mainClass; | + | |
- | } | + | |
- | + | ||
- | public char getSubclass() { | + | |
- | return subclass; | + | |
- | } | + | |
- | + | ||
- | public void setSubclass(char subclass) { | + | |
- | this.subclass = subclass; | + | |
- | } | + | |
- | + | ||
- | public int getMainGroup() { | + | |
- | return mainGroup; | + | |
- | } | + | |
- | + | ||
- | public void setMainGroup(int mainGroup) { | + | |
- | this.mainGroup = mainGroup; | + | |
- | } | + | |
- | + | ||
- | public int getSubgroup() { | + | |
- | return subgroup; | + | |
- | } | + | |
- | + | ||
- | public void setSubgroup(int subgroup) { | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int hashCode() { | + | |
- | final int prime = 31; | + | |
- | int result = 1; | + | |
- | + | ||
- | result = prime * result + mainClass; | + | |
- | result = prime * result + mainGroup; | + | |
- | result = prime * result + section; | + | |
- | result = prime * result + subclass; | + | |
- | result = prime * result + subgroup; | + | |
- | + | ||
- | return result; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Classification other = (Classification) obj; | + | |
- | if (mainClass != other.mainClass || mainGroup != other.mainGroup || | + | |
- | section != other.section || subclass != other.subclass || subgroup != other.subgroup) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(section); | + | |
- | + | ||
- | String strMainClass = String.format("%2s", mainClass).replace(' ', '0'); | + | |
- | sb.append(strMainClass); | + | |
- | + | ||
- | sb.append(subclass); | + | |
- | + | ||
- | String strMainGroup = String.format("%4s", mainGroup); | + | |
- | sb.append(strMainGroup); | + | |
- | + | ||
- | sb.append(SEPARATOR); | + | |
- | + | ||
- | sb.append(subgroup); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Ipc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Ipc extends Classification { | + | |
- | + | ||
- | public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | } | + | |
- | + | ||
- | public Ipc(String symbol) { | + | |
- | super(symbol); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | char section = 'A'; | + | |
- | int mainClass = 4; | + | |
- | char subclass = 'B'; | + | |
- | int mainGroup = 55; | + | |
- | int subgroup = 56; | + | |
- | + | ||
- | Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | System.out.println(ipc); | + | |
- | + | ||
- | String symbol = "G09F 17/30"; | + | |
- | System.out.println(new Ipc(symbol)); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Cpc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Cpc extends Classification { | + | |
- | private char position; | + | |
- | private char value; | + | |
- | + | ||
- | public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public Cpc(String symbol, char position, char value) { | + | |
- | super(symbol); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public char getPosition() { | + | |
- | return position; | + | |
- | } | + | |
- | + | ||
- | public void setPosition(char position) { | + | |
- | this.position = position; | + | |
- | } | + | |
- | + | ||
- | public char getValue() { | + | |
- | return value; | + | |
- | } | + | |
- | + | ||
- | public void setValue(char value) { | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int hashCode() { | + | |
- | final int prime = 31; | + | |
- | int result = super.hashCode(); | + | |
- | + | ||
- | result = prime * result + position; | + | |
- | result = prime * result + value; | + | |
- | + | ||
- | return result; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (!super.equals(obj)) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | if (getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Cpc other = (Cpc) obj; | + | |
- | if (position != other.position || value != other.value) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Date.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Date { | + | |
- | private int year, month, day; | + | |
- | + | ||
- | public Date(int year, int month, int day) { | + | |
- | this.year = year; | + | |
- | this.month = month; | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | // format: YYYY-MM-DD | + | |
- | public Date(String str) { | + | |
- | String[] parts = str.trim().split("-"); | + | |
- | this.year = Integer.parseInt(parts[0]); | + | |
- | this.month = Integer.parseInt(parts[1]); | + | |
- | this.day = Integer.parseInt(parts[2]); | + | |
- | } | + | |
- | + | ||
- | public int getYear() { | + | |
- | return year; | + | |
- | } | + | |
- | + | ||
- | public void setYear(int year) { | + | |
- | this.year = year; | + | |
- | } | + | |
- | + | ||
- | public int getMonth() { | + | |
- | return month; | + | |
- | } | + | |
- | + | ||
- | public void setMonth(int month) { | + | |
- | this.month = month; | + | |
- | } | + | |
- | + | ||
- | public int getDay() { | + | |
- | return day; | + | |
- | } | + | |
- | + | ||
- | public void setDay(int day) { | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Date other = (Date) obj; | + | |
- | if (day != other.day || month != other.month || year != other.year) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(year); | + | |
- | if (month < 10) { | + | |
- | sb.append("0" + month); | + | |
- | } else { | + | |
- | sb.append(month); | + | |
- | } | + | |
- | if (day < 10) { | + | |
- | sb.append("0" + day); | + | |
- | } else { | + | |
- | sb.append(day); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Patent.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | import java.util.Arrays; | + | |
- | import java.util.LinkedHashSet; | + | |
- | import java.util.List; | + | |
- | import java.util.Set; | + | |
- | + | ||
- | public class Patent { | + | |
- | private String applicationNo; | + | |
- | private Date applicationDate; | + | |
- | private LinkedHashSet<Ipc> ipcs; | + | |
- | private LinkedHashSet<Cpc> cpcs; | + | |
- | + | ||
- | private final static String SEPERATOR = ","; | + | |
- | + | ||
- | public Patent(String applicationNo, Date applicationDate, LinkedHashSet<Ipc> ipcs, LinkedHashSet<Cpc> cpcs) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | this.applicationDate = applicationDate; | + | |
- | this.ipcs = ipcs; | + | |
- | this.cpcs = cpcs; | + | |
- | } | + | |
- | + | ||
- | public Patent(String applicationNo, Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | this.applicationDate = applicationDate; | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); | + | |
- | } | + | |
- | + | ||
- | public Patent(String applicationNo, String applicationDate, String multiIpcs, String multiCpcs, char position, char value) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | this.applicationDate = new Date(applicationDate); | + | |
- | + | ||
- | String[] ipcSymbols = multiIpcs.split(SEPERATOR); | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(ipcSymbols.length); | + | |
- | for (int i = 0; i < ipcSymbols.length; i++) { | + | |
- | Ipc ipc = new Ipc(ipcSymbols[i].trim()); | + | |
- | ipcs.add(ipc); | + | |
- | } | + | |
- | + | ||
- | String[] cpcSymbols = multiCpcs.split(SEPERATOR); | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(cpcSymbols.length); | + | |
- | for (int i = 0; i < cpcSymbols.length; i++) { | + | |
- | Cpc cpc = new Cpc(cpcSymbols[i].trim(), position, value); | + | |
- | cpcs.add(cpc); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public String getApplicationNo() { | + | |
- | return applicationNo; | + | |
- | } | + | |
- | + | ||
- | public void setApplicationNo(String applicationNo) { | + | |
- | this.applicationNo = applicationNo; | + | |
- | } | + | |
- | + | ||
- | public Date getApplicationDate() { | + | |
- | return applicationDate; | + | |
- | } | + | |
- | + | ||
- | public void setApplicationDate(Date applicationDate) { | + | |
- | this.applicationDate = applicationDate; | + | |
- | } | + | |
- | + | ||
- | public Set<Ipc> getIpcs() { | + | |
- | return ipcs; | + | |
- | } | + | |
- | + | ||
- | public void setIpcs(Ipc[] ipcs) { | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); | + | |
- | } | + | |
- | + | ||
- | public Set<Cpc> getCpcs() { | + | |
- | return cpcs; | + | |
- | } | + | |
- | + | ||
- | public void setCpcs(Cpc[] cpcs) { | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(applicationDate + "\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | List<Ipc> listIpcs = new ArrayList<Ipc>(ipcs); | + | |
- | for (int i = 0; i < listIpcs.size() - 1; i++) { | + | |
- | sb.append(listIpcs.get(i) + SEPERATOR); | + | |
- | } | + | |
- | sb.append(listIpcs.get(listIpcs.size() - 1) + "]\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | List<Cpc> listCpcs = new ArrayList<Cpc>(cpcs); | + | |
- | for (int i = 0; i < listCpcs.size() - 1; i++) { | + | |
- | sb.append(listCpcs.get(i) + SEPERATOR); | + | |
- | } | + | |
- | sb.append(listCpcs.get(listCpcs.size() - 1) + "]"); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java CorpusList.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | import java.util.List; | + | |
- | import java.util.Set; | + | |
- | + | ||
- | public class CorpusList { | + | |
- | private List<Patent> data; | + | |
- | + | ||
- | public CorpusList() { | + | |
- | this.data = new ArrayList<Patent>(); | + | |
- | } | + | |
- | + | ||
- | public CorpusList(List<Patent> data) { | + | |
- | this.data = data; | + | |
- | } | + | |
- | + | ||
- | public CorpusList(Patent[] patents) { | + | |
- | this(); | + | |
- | + | ||
- | for (Patent patent: patents) { | + | |
- | data.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public void add(Patent patent) { | + | |
- | data.add(patent); | + | |
- | } | + | |
- | + | ||
- | public int size() { | + | |
- | return data.size(); | + | |
- | } | + | |
- | + | ||
- | public boolean contains(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Ipc> ipcs = patent.getIpcs(); | + | |
- | if (ipcs.contains(ipc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Cpc> cpcs = patent.getCpcs(); | + | |
- | if (cpcs.contains(cpc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationDate().equals(applicationDate)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Ipc> ipcs = patent.getIpcs(); | + | |
- | if (ipcs.contains(ipc)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Cpc> cpcs = patent.getCpcs(); | + | |
- | if (cpcs.contains(cpc)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList find(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (patent.getApplicationDate().equals(applicationDate)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (!patent.getApplicationNo().equalsIgnoreCase(applicationNo)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Ipc> ipcs = patent.getIpcs(); | + | |
- | if (!ipcs.contains(ipc)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | Set<Cpc> cpcs = patent.getCpcs(); | + | |
- | if (!cpcs.contains(cpc)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | public CorpusList remove(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return this; | + | |
- | } | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | if (!patent.getApplicationDate().equals(applicationDate)) { | + | |
- | corpus.add(patent); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return corpus.size() == 0? null: corpus; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | for (Patent patent: data) { | + | |
- | sb.append(patent + "\n"); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | Patent[] patents = new Patent[5]; | + | |
- | patents[0] = new Patent("14725838", "2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I'); | + | |
- | patents[1] = new Patent("14814205", "2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I'); | + | |
- | patents[2] = new Patent("15189723", "2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A'); | + | |
- | patents[3] = new Patent("14711011", "2015-05-13", "B32B 17/10, B32B 27/32", "G06F 3/485", 'L', 'A'); | + | |
- | patents[4] = new Patent("14515267", "2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I'); | + | |
- | + | ||
- | CorpusList corpus = new CorpusList(patents); | + | |
- | System.out.println(corpus); | + | |
- | + | ||
- | CorpusList corpusFound = corpus.find(new Date("2016-06-22")); | + | |
- | System.out.println(corpusFound); | + | |
- | + | ||
- | CorpusList corpusAfterRemovingIpc = corpus.remove(new Ipc("F16F 15/02")); | + | |
- | System.out.println(corpusAfterRemovingIpc); | + | |
- | + | ||
- | CorpusList corpusAfterRemovingCpc =corpus.remove(new Cpc("A23B 5/04", 'F', 'I')); | + | |
- | System.out.println(corpusAfterRemovingCpc); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | 【参考答案三】 | + | |
- | <file java Data.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Date { | + | |
- | private int year, month, day; | + | |
- | + | ||
- | public Date(int year, int month, int day) { | + | |
- | this.year = year; | + | |
- | this.month = month; | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | // format: YYYY-MM-DD | + | |
- | public Date(String str) { | + | |
- | String[] parts = str.trim().split("-"); | + | |
- | this.year = Integer.parseInt(parts[0]); | + | |
- | this.month = Integer.parseInt(parts[1]); | + | |
- | this.day = Integer.parseInt(parts[2]); | + | |
- | } | + | |
- | + | ||
- | public int getYear() { | + | |
- | return year; | + | |
- | } | + | |
- | + | ||
- | public void setYear(int year) { | + | |
- | this.year = year; | + | |
- | } | + | |
- | + | ||
- | public int getMonth() { | + | |
- | return month; | + | |
- | } | + | |
- | + | ||
- | public void setMonth(int month) { | + | |
- | this.month = month; | + | |
- | } | + | |
- | + | ||
- | public int getDay() { | + | |
- | return day; | + | |
- | } | + | |
- | + | ||
- | public void setDay(int day) { | + | |
- | this.day = day; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Date other = (Date) obj; | + | |
- | if (day != other.day || month != other.month || year != other.year) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(year); | + | |
- | if (month < 10) { | + | |
- | sb.append("0" + month); | + | |
- | } else { | + | |
- | sb.append(month); | + | |
- | } | + | |
- | if (day < 10) { | + | |
- | sb.append("0" + day); | + | |
- | } else { | + | |
- | sb.append(day); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </filie> | + | |
- | + | ||
- | <file java Classification.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Classification { | + | |
- | protected char section; | + | |
- | protected int mainClass; | + | |
- | protected char subclass; | + | |
- | protected int mainGroup; | + | |
- | protected int subgroup; | + | |
- | + | ||
- | public final static char SEPARATOR = '/'; | + | |
- | + | ||
- | public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | this.section = section; | + | |
- | this.mainClass = mainClass; | + | |
- | this.subclass = subclass; | + | |
- | this.mainGroup = mainGroup; | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | // example: G09F 17/30 | + | |
- | public Classification(String symbol) { | + | |
- | this.section = symbol.charAt(0); | + | |
- | this.mainClass = Integer.parseInt(symbol.substring(1, 3)); | + | |
- | this.subclass = symbol.charAt(3); | + | |
- | + | ||
- | int pos = symbol.indexOf(SEPARATOR); | + | |
- | this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim()); | + | |
- | this.subgroup = Integer.parseInt(symbol.substring(pos + 1)); | + | |
- | } | + | |
- | + | ||
- | public char getSection() { | + | |
- | return section; | + | |
- | } | + | |
- | + | ||
- | public void setSection(char section) { | + | |
- | this.section = section; | + | |
- | } | + | |
- | + | ||
- | public int getMainClass() { | + | |
- | return mainClass; | + | |
- | } | + | |
- | + | ||
- | public void setMainClass(int mainClass) { | + | |
- | this.mainClass = mainClass; | + | |
- | } | + | |
- | + | ||
- | public char getSubclass() { | + | |
- | return subclass; | + | |
- | } | + | |
- | + | ||
- | public void setSubclass(char subclass) { | + | |
- | this.subclass = subclass; | + | |
- | } | + | |
- | + | ||
- | public int getMainGroup() { | + | |
- | return mainGroup; | + | |
- | } | + | |
- | + | ||
- | public void setMainGroup(int mainGroup) { | + | |
- | this.mainGroup = mainGroup; | + | |
- | } | + | |
- | + | ||
- | public int getSubgroup() { | + | |
- | return subgroup; | + | |
- | } | + | |
- | + | ||
- | public void setSubgroup(int subgroup) { | + | |
- | this.subgroup = subgroup; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int hashCode() { | + | |
- | final int prime = 31; | + | |
- | int result = 1; | + | |
- | + | ||
- | result = prime * result + mainClass; | + | |
- | result = prime * result + mainGroup; | + | |
- | result = prime * result + section; | + | |
- | result = prime * result + subclass; | + | |
- | result = prime * result + subgroup; | + | |
- | + | ||
- | return result; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (obj == null || getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Classification other = (Classification) obj; | + | |
- | if (mainClass != other.mainClass || mainGroup != other.mainGroup || | + | |
- | section != other.section || subclass != other.subclass || subgroup != other.subgroup) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(section); | + | |
- | + | ||
- | String strMainClass = String.format("%2s", mainClass).replace(' ', '0'); | + | |
- | sb.append(strMainClass); | + | |
- | + | ||
- | sb.append(subclass); | + | |
- | + | ||
- | String strMainGroup = String.format("%4s", mainGroup); | + | |
- | sb.append(strMainGroup); | + | |
- | + | ||
- | sb.append(SEPARATOR); | + | |
- | + | ||
- | sb.append(subgroup); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Ipc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Ipc extends Classification { | + | |
- | + | ||
- | public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | } | + | |
- | + | ||
- | public Ipc(String symbol) { | + | |
- | super(symbol); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | char section = 'A'; | + | |
- | int mainClass = 4; | + | |
- | char subclass = 'B'; | + | |
- | int mainGroup = 55; | + | |
- | int subgroup = 56; | + | |
- | + | ||
- | Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | System.out.println(ipc); | + | |
- | + | ||
- | String symbol = "G09F 17/30"; | + | |
- | System.out.println(new Ipc(symbol)); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Cpc.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | public class Cpc extends Classification { | + | |
- | private char position; | + | |
- | private char value; | + | |
- | + | ||
- | public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) { | + | |
- | super(section, mainClass, subclass, mainGroup, subgroup); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public Cpc(String symbol, char position, char value) { | + | |
- | super(symbol); | + | |
- | + | ||
- | this.position = position; | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | public char getPosition() { | + | |
- | return position; | + | |
- | } | + | |
- | + | ||
- | public void setPosition(char position) { | + | |
- | this.position = position; | + | |
- | } | + | |
- | + | ||
- | public char getValue() { | + | |
- | return value; | + | |
- | } | + | |
- | + | ||
- | public void setValue(char value) { | + | |
- | this.value = value; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int hashCode() { | + | |
- | final int prime = 31; | + | |
- | int result = super.hashCode(); | + | |
- | + | ||
- | result = prime * result + position; | + | |
- | result = prime * result + value; | + | |
- | + | ||
- | return result; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public boolean equals(Object obj) { | + | |
- | if (this == obj) { | + | |
- | return true; | + | |
- | } | + | |
- | + | ||
- | if (!super.equals(obj)) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | if (getClass() != obj.getClass()) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | Cpc other = (Cpc) obj; | + | |
- | if (position != other.position || value != other.value) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return true; | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Patent.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | import java.util.ArrayList; | + | |
- | import java.util.Arrays; | + | |
- | import java.util.LinkedHashSet; | + | |
- | import java.util.List; | + | |
- | import java.util.Set; | + | |
- | + | ||
- | public class Patent { | + | |
- | //private String applicationNo; | + | |
- | private Date applicationDate; | + | |
- | private LinkedHashSet<Ipc> ipcs; | + | |
- | private LinkedHashSet<Cpc> cpcs; | + | |
- | + | ||
- | private final static String SEPERATOR = ","; | + | |
- | + | ||
- | public Patent(Date applicationDate, LinkedHashSet<Ipc> ipcs, LinkedHashSet<Cpc> cpcs) { | + | |
- | this.applicationDate = applicationDate; | + | |
- | this.ipcs = ipcs; | + | |
- | this.cpcs = cpcs; | + | |
- | } | + | |
- | + | ||
- | public Patent(Date applicationDate, Ipc[] ipcs, Cpc[] cpcs) { | + | |
- | this.applicationDate = applicationDate; | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); | + | |
- | } | + | |
- | + | ||
- | public Patent(String applicationDate, String multiIpcs, String multiCpcs, char position, char value) { | + | |
- | this.applicationDate = new Date(applicationDate); | + | |
- | + | ||
- | String[] ipcSymbols = multiIpcs.split(SEPERATOR); | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(ipcSymbols.length); | + | |
- | for (int i = 0; i < ipcSymbols.length; i++) { | + | |
- | Ipc ipc = new Ipc(ipcSymbols[i].trim()); | + | |
- | ipcs.add(ipc); | + | |
- | } | + | |
- | + | ||
- | String[] cpcSymbols = multiCpcs.split(SEPERATOR); | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(cpcSymbols.length); | + | |
- | for (int i = 0; i < cpcSymbols.length; i++) { | + | |
- | Cpc cpc = new Cpc(cpcSymbols[i].trim(), position, value); | + | |
- | cpcs.add(cpc); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public Date getApplicationDate() { | + | |
- | return applicationDate; | + | |
- | } | + | |
- | + | ||
- | public void setApplicationDate(Date applicationDate) { | + | |
- | this.applicationDate = applicationDate; | + | |
- | } | + | |
- | + | ||
- | public Set<Ipc> getIpcs() { | + | |
- | return ipcs; | + | |
- | } | + | |
- | + | ||
- | public void setIpcs(Ipc[] ipcs) { | + | |
- | this.ipcs = new LinkedHashSet<Ipc>(Arrays.asList(ipcs)); | + | |
- | } | + | |
- | + | ||
- | public Set<Cpc> getCpcs() { | + | |
- | return cpcs; | + | |
- | } | + | |
- | + | ||
- | public void setCpcs(Cpc[] cpcs) { | + | |
- | this.cpcs = new LinkedHashSet<Cpc>(Arrays.asList(cpcs)); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | sb.append(applicationDate + "\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | List<Ipc> listIpcs = new ArrayList<Ipc>(ipcs); | + | |
- | for (int i = 0; i < listIpcs.size() - 1; i++) { | + | |
- | sb.append(listIpcs.get(i) + SEPERATOR); | + | |
- | } | + | |
- | sb.append(listIpcs.get(listIpcs.size() - 1) + "]\t"); | + | |
- | + | ||
- | sb.append("["); | + | |
- | List<Cpc> listCpcs = new ArrayList<Cpc>(cpcs); | + | |
- | for (int i = 0; i < listCpcs.size() - 1; i++) { | + | |
- | sb.append(listCpcs.get(i) + SEPERATOR); | + | |
- | } | + | |
- | sb.append(listCpcs.get(listCpcs.size() - 1) + "]"); | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | <file java Corpus.java> | + | |
- | package cn.edu.bjut.chapter6; | + | |
- | + | ||
- | import java.util.HashMap; | + | |
- | import java.util.HashSet; | + | |
- | import java.util.Map; | + | |
- | import java.util.Set; | + | |
- | + | ||
- | public class Corpus { | + | |
- | private Map<String, Patent> data; | + | |
- | + | ||
- | public Corpus() { | + | |
- | this.data = new HashMap<String, Patent>(); | + | |
- | } | + | |
- | + | ||
- | public Corpus(Map<String, Patent> data) { | + | |
- | this.data = data; | + | |
- | } | + | |
- | + | ||
- | public void add(String applicationNo, Patent patent) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return; | + | |
- | } | + | |
- | + | ||
- | data.put(applicationNo.trim().toUpperCase(), patent); | + | |
- | } | + | |
- | + | ||
- | public int size() { | + | |
- | return data.size(); | + | |
- | } | + | |
- | + | ||
- | public boolean contains(String applicationNo) { | + | |
- | if (applicationNo == null || applicationNo.trim().equals("")) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | return data.containsKey(applicationNo.trim().toUpperCase()); | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Ipc ipc) { | + | |
- | if (ipc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data.values()) { | + | |
- | Set<Ipc> ipcs = patent.getIpcs(); | + | |
- | if (ipcs.contains(ipc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Cpc cpc) { | + | |
- | if (cpc == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data.values()) { | + | |
- | Set<Cpc> cpcs = patent.getCpcs(); | + | |
- | if (cpcs.contains(cpc)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public boolean contains(Date applicationDate) { | + | |
- | if (applicationDate == null) { | + | |
- | return false; | + | |
- | } | + | |
- | + | ||
- | for (Patent patent: data.values()) { | + | |
- | if (patent.getApplicationDate().equals(applicationDate)) { | + | |
- | return true; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
- | } | + | |
- | + | ||
- | public Corpus find(String applicationNo) { | + | |
- | if (!contains(applicationNo)) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | Map<String, Patent> subData = new HashMap<String, Patent>(); | + | |
- | + | ||
- | String applicationNoAfter = applicationNo.trim().toUpperCase(); | + | |
- | subData.put(applicationNoAfter, data.get(applicationNoAfter)); | + | |
- | + | ||
- | return new Corpus(subData); | + | |
- | } | + | |
- | + | ||
- | public Corpus find(Ipc ipc) { | + | |
- | if (!contains(ipc)) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | Map<String, Patent> subData = new HashMap<String, Patent>(); | + | |
- | + | ||
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Set<Ipc> ipcs = entry.getValue().getIpcs(); | + | |
- | if (ipcs.contains(ipc)) { | + | |
- | subData.put(entry.getKey(), entry.getValue()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return new Corpus(subData); | + | |
- | } | + | |
- | + | ||
- | public Corpus find(Cpc cpc) { | + | |
- | if (!contains(cpc)) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | Map<String, Patent> subData = new HashMap<String, Patent>(); | + | |
- | + | ||
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Set<Cpc> cpcs = entry.getValue().getCpcs(); | + | |
- | if (cpcs.contains(cpc)) { | + | |
- | subData.put(entry.getKey(), entry.getValue()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return new Corpus(subData); | + | |
- | } | + | |
- | + | ||
- | public Corpus find(Date applicationDate) { | + | |
- | if (!contains(applicationDate)) { | + | |
- | return null; | + | |
- | } | + | |
- | + | ||
- | Map<String, Patent> subData = new HashMap<String, Patent>(); | + | |
- | + | ||
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Date date = entry.getValue().getApplicationDate(); | + | |
- | if (date.equals(applicationDate)) { | + | |
- | subData.put(entry.getKey(), entry.getValue()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return new Corpus(subData); | + | |
- | } | + | |
- | + | ||
- | public void remove(String applicationNo) { | + | |
- | if (!contains(applicationNo)) { | + | |
- | return; | + | |
- | } | + | |
- | + | ||
- | data.remove(applicationNo.trim().toUpperCase()); | + | |
- | } | + | |
- | + | ||
- | public void remove(Ipc ipc) { | + | |
- | if (!contains(ipc)) { | + | |
- | return; | + | |
- | } | + | |
- | + | ||
- | Set<String> keys = new HashSet<String>(); | + | |
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Set<Ipc> ipcs = entry.getValue().getIpcs(); | + | |
- | if (ipcs.contains(ipc)) { | + | |
- | keys.add(entry.getKey()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | for (String key: keys) { | + | |
- | data.remove(key); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public void remove(Cpc cpc) { | + | |
- | if (!contains(cpc)) { | + | |
- | return; | + | |
- | } | + | |
- | + | ||
- | Set<String> keys = new HashSet<String>(); | + | |
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Set<Cpc> cpcs = entry.getValue().getCpcs(); | + | |
- | if (cpcs.contains(cpc)) { | + | |
- | keys.add(entry.getKey()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | for (String key: keys) { | + | |
- | data.remove(key); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public void remove(Date applicationDate) { | + | |
- | if (!contains(applicationDate)) { | + | |
- | return; | + | |
- | } | + | |
- | + | ||
- | Set<String> keys = new HashSet<String>(); | + | |
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | Date date = entry.getValue().getApplicationDate(); | + | |
- | if (date.equals(applicationDate)) { | + | |
- | keys.add(entry.getKey()); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | for (String key: keys) { | + | |
- | data.remove(key); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public String toString() { | + | |
- | StringBuilder sb = new StringBuilder(); | + | |
- | + | ||
- | for (Map.Entry<String, Patent> entry: data.entrySet()) { | + | |
- | sb.append(entry.getKey() + "\t" + entry.getValue() + "\n"); | + | |
- | } | + | |
- | + | ||
- | return sb.toString(); | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | Corpus corpus = new Corpus(); | + | |
- | + | ||
- | corpus.add("14725838", new Patent("2015-05-29", "A23B 5/04, A23B 5/045", "A23B 5/04, A23B 5/45, A23L 15/20", 'F', 'I')); | + | |
- | corpus.add("14814205", new Patent("2015-07-30", "A61H 33/00, C01B 33/107, E04B 1/84", "A61H 33/6063, A61H 33/6042", 'F', 'I')); | + | |
- | corpus.add("15189723", new Patent("2016-06-22", "D04B 1/00", "B65H2301/44514, C01B 33/1071, F16F 15/2, H01L 27/14641", 'L', 'A')); | + | |
- | corpus.add("14711011", new Patent("2015-05-13", "B32B 17/10, B32B 27/32, F16F 15/02", "G06F 3/485", 'L', 'A')); | + | |
- | corpus.add("14515267", new Patent("2014-10-15", "F16F 15/02, G09G 5/34, G06F 3/0485", "A01B 15/06, A01B 15/06", 'L', 'I')); | + | |
- | + | ||
- | System.out.println(corpus); | + | |
- | + | ||
- | corpus.remove(new Ipc("F16F 15/02")); | + | |
- | System.out.println(corpus); | + | |
- | + | ||
- | corpus.remove(new Cpc("A23B 5/04", 'F', 'I')); | + | |
- | System.out.println(corpus); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
[[zh:courses:java2025:index|返回Java课程页]] | [[zh:courses:java2025:index|返回Java课程页]] | ||
- | ~~DISCUSSION~~ | + | ~~DISCUSSION:closed~~ |