ObjectOrientedMain类
/** * */package com.jason.kwic.objectOriented;import java.io.IOException;/** * @author jasonzhang * */public class ObjectOrientedMain { public static void main(String[] args) throws IOException { Output output = new Output(); Alphabetizer sorter = new Alphabetizer(output); Shift shift = new Shift(sorter); Input input = new Input(shift); input.input("d:\\temp\\mykwic_in.txt"); shift.shift(); sorter.sort(); }}
Output类
/** * */package com.jason.kwic.objectOriented;import java.util.ArrayList;/** * @author jasonzhang * */public class Output { public void output(ArrayListlines) { System.out.println("output is"); for (String line : lines) { System.out.println(line); } }}
Alphabetizer类
/** * */package com.jason.kwic.objectOriented;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;/** * @author jasonzhang * */public class Alphabetizer { private ArrayListlines = new ArrayList (); private Output output; public Alphabetizer(Output output) { this.output = output; } public void addLine(String line) { this.lines.add(line); } public void sort() { Collections.sort(this.lines, new AlphaabetizerComparator()); this.output.output(this.lines); } private class AlphaabetizerComparator implements Comparator { @Override public int compare(String o1, String o2) { if (o1 == null || o2 == null) { throw new NullPointerException(); } int compareValue = 0; char o1FirstCharacter = o1.charAt(0); char o2FirstCharacter = o2.charAt(0); if(this.isLetter(o1FirstCharacter) && this.isLetter(o2FirstCharacter)) { //如果是小写的字母的值,则转成对应的大写的字母的值 o1FirstCharacter = this.toUpperCase(o1FirstCharacter); o2FirstCharacter = this.toUpperCase(o2FirstCharacter); compareValue = o1FirstCharacter - o2FirstCharacter; } else { throw new RuntimeException("必须是字母"); } return compareValue; } private boolean isLetter(char c) { return (c >= 65 && c <= 90) || (c >= 97 && c <= 122); } private char toUpperCase(char c) { if (Character.isLowerCase(c)) { return Character.toUpperCase(c); } return c; } }}
Shift类
/** * */package com.jason.kwic.objectOriented;import java.util.ArrayList;import java.util.StringTokenizer;/** * @author jasonzhang * */public class Shift { private ArrayListlines = new ArrayList (); //private ArrayList shifedLines = new ArrayList (); private Alphabetizer sorter; public Shift(Alphabetizer sorter) { this.sorter = sorter; } public void addLine(String line) { this.lines.add(line); } public void shift() { for (String line : this.lines) { this.shiftLine(line); } } private void shiftLine(String line) { ArrayList wordList = new ArrayList (); StringTokenizer st = new StringTokenizer(line, " "); while (st.hasMoreTokens()) { wordList.add(st.nextToken()); } int wordNums = wordList.size(); StringBuilder tempLine = new StringBuilder(); //System.out.println("shifed is"); for (int i=0; i
Input类
/** * */package com.jason.kwic.objectOriented;import java.io.File;import java.io.IOException;import java.util.Scanner;/** * @author jasonzhang * */public class Input { private Shift shift; public Input(Shift shift) { this.shift = shift; } public void input(String filePath) throws IOException { File infile = new File(filePath); Scanner sc = new Scanner(infile); String tempLine = null; System.out.println("input is"); while (sc.hasNextLine()) { tempLine = sc.nextLine(); System.out.println(tempLine); this.shift.addLine(tempLine); } }}