Advertisement
CaptainSpaceCat

SolvedShift

Sep 9th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. public class SolvedShift implements Comparable{
  2.  
  3.   public String message;
  4.   public String[] keywords = new String[0];
  5.   public int offset;
  6.  
  7.   public SolvedShift(String m, int o, String k) {
  8.     message = m;
  9.     offset = o;
  10.     addKeyword(k);
  11.   }
  12.  
  13.   public int compareTo(Object o) {
  14.     SolvedShift temp = (SolvedShift)o;
  15.     if (temp.keywords.length > keywords.length) {
  16.       return 1;
  17.     } else if (temp.keywords.length > keywords.length) {
  18.       return -1;
  19.     } else {
  20.       if (temp.getWordLength() > getWordLength()) {
  21.         return 1;
  22.       } else {
  23.         return -1;
  24.       }
  25.     }
  26.   }
  27.  
  28.   public int getWordLength() {
  29.     int sum = 0;
  30.     for (String str: keywords) {
  31.       sum += str.length();
  32.     }
  33.     return sum;
  34.   }
  35.  
  36.   public void addKeyword(String word) {
  37.     String[] k = new String[keywords.length + 1];
  38.     for (int i = 0; i < keywords.length; i++) {
  39.       k[i] = keywords[i];
  40.     }
  41.     k[k.length - 1] = word;
  42.     keywords = k;
  43.   }
  44.  
  45.   public void removeKeyword(int index) {
  46.     String[] result = new String[keywords.length - 1];
  47.    
  48.     int offset = 0;
  49.     for (int i = 0; i < keywords.length; i++) {
  50.       if (i == index) {
  51.         offset++;
  52.       } else {
  53.         result[i-offset] = keywords[i];
  54.       }
  55.     }
  56.    
  57.     keywords = result;
  58.   }
  59.  
  60.   public void cleanKeywords() {
  61.     for (int i = keywords.length - 1; i >= 0; i--) {
  62.       for (int n = i - 1; n >= 0; n--) {
  63.         if (keywords[n] == keywords[i]) {
  64.           removeKeyword(i);
  65.           break;
  66.         }
  67.       }
  68.     }
  69.   }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement