package cn.edu.bjut.chapter3; public class Ipc { private char section; private int mainClass; private char subclass; private int mainGroup; private int subgroup; private final char SEPARATOR = '/'; public Ipc(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 Ipc(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 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(); } public static void main(String[] args) { char section = 'A'; int mainClass = 4; char subclass = 'B'; int mainGroup = 55; int subgroup = 56; Ipc ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); System.out.println(ipc); String symbol = "G09F 17/30"; System.out.println(new Ipc(symbol)); } }