@matfah/

Cookie FR 2010

Java

No description

fork
loading
Files
  • Main.java
  • CookieOrder.java
  • MasterOrder.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
import java.util.*;
import java.lang.reflect.*;

public class Main {
  
  private static Field f;
  
  public static void main(String[] args) {
    
    try {
      f = MasterOrder.class.getDeclaredField("orders");
      
      System.out.println("Testing getTotalBoxes()");
      
      
      f.setAccessible(true);
      
      MasterOrder order = new MasterOrder();
      List<CookieOrder> list = (List<CookieOrder>)f.get(order);
      
      if(order.getTotalBoxes() != 0){
        System.out.println("   FAILED test #1 - no boxes in list");
        return;
      }
      else 
        System.out.println("   Passed test #1");
        
      int sum = 0;
      for(int num = (int)(Math.random()*5)+5; num > 0; num--) {
        int count = (int)(Math.random()*5)+1;
        order.addOrder(new CookieOrder("A",  count));
        sum += count;
      }
      
      if(order.getTotalBoxes() != sum) {
        System.out.println("   FAILED test #2 - several boxes in list");
        return;
      }
      else 
        System.out.println("   Passed test #2");
        
      
      System.out.println("Testing removeVariety()");
      
      order = new MasterOrder();
      order.addOrder(new CookieOrder("Chocolate Chip", 1));
      order.addOrder(new CookieOrder("Shortbread", 5));
      order.addOrder(new CookieOrder("Macaroon", 2));
      order.addOrder(new CookieOrder("Chocolate Chip", 3));
      list = (List<CookieOrder>)f.get(order);
      int result = order.removeVariety("Chocolate Chip");
      MasterOrder answer = new MasterOrder();
      answer.addOrder(new CookieOrder("Shortbread", 5));
      answer.addOrder(new CookieOrder("Macaroon", 2));
      
      if(result == 4 && ordersEqual(order, answer)) {
        System.out.println("   Passed test #1");
      }
      else {
        System.out.println("   FAILED test #1 - first example");
        return;
      }
      
      order = new MasterOrder();
      order.addOrder(new CookieOrder("Chocolate Chip", 1));
      order.addOrder(new CookieOrder("Shortbread", 5));
      order.addOrder(new CookieOrder("Macaroon", 2));
      order.addOrder(new CookieOrder("Chocolate Chip", 3));
      list = (List<CookieOrder>)f.get(order);
      result = order.removeVariety("Brownie");
      answer = new MasterOrder();
      answer.addOrder(new CookieOrder("Chocolate Chip", 1));
      answer.addOrder(new CookieOrder("Shortbread", 5));
      answer.addOrder(new CookieOrder("Macaroon", 2));
      answer.addOrder(new CookieOrder("Chocolate Chip", 3));
      
      if(result == 0 && ordersEqual(order, answer)) {
        System.out.println("   Passed test #2");
      }
      else {
        System.out.println("   FAILED test #2 - first example");
        return;
      }
      
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    
  }
  
  public static boolean ordersEqual(MasterOrder a, MasterOrder b) throws Exception {
    List<CookieOrder> aList = (List<CookieOrder>)f.get(a);
    List<CookieOrder> bList = (List<CookieOrder>)f.get(b);
    
    if(aList.size() != bList.size()) return false;
    
    for(int i = 0; i < aList.size(); i++) {
      if(!aList.get(i).getVariety().equals(bList.get(i).getVariety()))
        return false;
       if(aList.get(i).getNumBoxes() != bList.get(i).getNumBoxes())
        return false;
    }
    return true;
  }
}