@matfah/

Customer FR 2006

Java

No description

fork
loading
Files
  • Main.java
  • Customer.java
  • Merge.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.*;

public class Main {
  public static void main(String[] args) {
    testCompareCustomer();
    System.out.println("compareCustomer() passed all tests!");
    
    for(int i = 0; i < 20; i++)
      testPrefixMerge();
    System.out.println("prefixMerge() passed all tests!");
      
    System.out.println("All Tests Passed!");
  }
  
  public static void testCompareCustomer() {
    
    String[] names = {"Alan", "Bill", "Charles"};
    int[] ids = {0, 5, 20};
    
    for(int i = 0; i < names.length; i++) {
      for(int j = 0; j < names.length; j++) {
        for(int k = 0; k < ids.length; k++) {
          for(int m = 0; m < ids.length; m++) {
            CustomCustomer one = new CustomCustomer(names[i], ids[k]);
            CustomCustomer two = new CustomCustomer(names[j], ids[m]);
            
            int result = 0;
            try {
              result = one.compareCustomer(two);
            }
            catch(Exception e) {
              System.out.println("Error calling compareCustomer()");
              System.exit(0);
            }
            
            int goal = names[i].compareTo(names[j]);
            
            if(one.calledGetName == 0 || two.calledGetName == 0) {
              System.out.println("compareCustomer() does not call getName() for both Customer objects");
              System.exit(0);
            }

            if(goal == 0 && (one.calledGetID == 0 || two.calledGetID == 0)) {
              System.out.println("compareCustomer() does not call getID() for both Customer objects");
              System.exit(0);
            }
            
            if(goal == 0) goal = ids[k] - ids[m];
            
            if(result == 0 && goal == 0) {
              
            }
            else if(result*goal == 0 || result*Math.abs(goal) != goal*Math.abs(result)) {
              System.out.println("Result of calling compareCustomer() is wrong");
              System.exit(0);
            }
          }
        }
      }
    }
    
  }
  
  public static void testPrefixMerge() {
    
    String[] names = {"Amy", "Brian", "Cathy", "David", "Elliot", "Frank", "Grant", "Howard"};
    int[] ids = {0, 5, 6, 11, 23, 42, 91, 125, 199};
    
    ArrayList<CustomCustomer> customersA = new ArrayList<CustomCustomer>();
    int namesPos = (int)(Math.random()*2)+1;
    int idsPos = (int)(Math.random()*2)+1;
    while(namesPos < names.length && idsPos < ids.length) {
      customersA.add(new CustomCustomer(names[namesPos], ids[idsPos]));
      
      namesPos += (int)(Math.random()*2)+1;
      idsPos += (int)(Math.random()*2)+1;
    }
    
    ArrayList<CustomCustomer> customersB = new ArrayList<CustomCustomer>();
    namesPos = (int)(Math.random()*2)+1;
    idsPos = (int)(Math.random()*2)+1;
    while(namesPos < names.length && idsPos < ids.length) {
      customersB.add(new CustomCustomer(names[namesPos], ids[idsPos]));
      
      namesPos += (int)(Math.random()*2)+1;
      idsPos += (int)(Math.random()*2)+1;
    }
    
    Comparator<CustomCustomer> comparator = new Comparator<CustomCustomer>() {
      public int compare(CustomCustomer a, CustomCustomer b) {
        int comp = a.name.compareTo(b.name);
        if(comp == 0) return a.idNum - b.idNum;
        return comp;
      }
    };
    
    TreeSet<CustomCustomer> allTogether = new TreeSet<CustomCustomer>(comparator);
    allTogether.addAll(customersA);
    allTogether.addAll(customersB);

    
    Customer[] listOne = new Customer[customersA.size()];
    for(int i = 0; i < listOne.length; i++)
      listOne[i] = customersA.get(i);
      
    Customer[] listTwo = new Customer[customersB.size()];
    for(int i = 0; i < listTwo.length; i++)
      listTwo[i] = customersB.get(i);
      
    int resultLength = (int)(Math.random() * Math.min(listOne.length-1, listTwo.length-1)) + 1;
    
    Customer[] result = new Customer[resultLength];
    Customer[] goalResult = new Customer[resultLength];
    
    int count = 0;
    for(Customer c : allTogether) {
      goalResult[count] = c;
      count++;
      
      if(count >= goalResult.length)
        break;
    }
    
    
    String listOneStr = "";
    for(Customer c: listOne) {
      listOneStr += c;
    }

    String listTwoStr = "";
    for(Customer c: listTwo)  {
      listTwoStr += c;
    }

    try {
      Merge.prefixMerge(listOne, listTwo, result);
    }
    catch(Exception e) {
      System.out.println("Error calling prefixMerge()");
      System.exit(0);
    }
    
    String listOneStrNew = "";
    for(Customer c: listOne) 
      listOneStrNew += c;
    
    String listTwoStrNew = "";
    for(Customer c: listTwo) 
      listTwoStrNew += c;
      
    if(!listOneStrNew.equals(listOneStr) || !listTwoStrNew.equals(listTwoStr)) {
      System.out.println("prefixMerge() is modifying listOne or listTwo - it shouldn't do that!");
      System.exit(0);
    }
    
    for(int i = 0; i < result.length; i++) {
      if(result[i] == null) {
        System.out.println("prefixMerge() is leaving null values in the result array!");
        System.exit(0);
      }
      
      CustomCustomer goalCustomer = null;
      CustomCustomer resultCustomer = null;
      
      try {
        goalCustomer = (CustomCustomer)goalResult[i];
        resultCustomer = (CustomCustomer)result[i];
      }
      catch(Exception e) {
        System.out.println("Although the directions don't technically forbid you from creating new instances of the Customer class in prefixMerge(), you really should think of a better way to finish this method!");
        System.exit(0);
      }
      
      if(!goalCustomer.name.equals(resultCustomer.name) || goalCustomer.idNum != resultCustomer.idNum) {
        System.out.println("prefixMerge() is not merging Customers in the right order!");
        System.exit(0);
      }
    }
    
    int amount = 0;
    for(Customer c: listOne) {
        amount += ((CustomCustomer)c).compareCustomerCount;
    }
    for(Customer c: listTwo) {
      amount += ((CustomCustomer)c).compareCustomerCount;
    }
    
    if(amount < result.length) {
      System.out.println("prefixMerge() is not using compareCustomer() correctly");
      System.exit(0);
    }
    
  }
}

class CustomCustomer extends Customer {
  
  String name;
  int idNum;
  
  int calledGetName = 0;
  int calledGetID = 0;
  int compareCustomerCount = 0;
  
  public CustomCustomer(String name, int idNum) { 
    super(name, idNum);
    this.name = name;
    this.idNum = idNum;
  }
  
  public String getName() {
    calledGetName++;
    return name;
  }
  
  public int getID() {
    calledGetID++;
    return idNum;
  }
  
  public int compareCustomer(Customer other) {
    compareCustomerCount++;
    return super.compareCustomer(other);
  }

}