@matfah/

Trio FR 2014

Java

No description

fork
loading
Files
  • Main.java
  • MenuItem.java
  • Trio.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4911551471829063","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEubGFuZy5yZWZsZWN0Lio7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgCiAgcHJpdmF0ZSBzdGF0aWMgU3RyaW5nW10gc2FuZHdoaWNoTmFtZXMgPSB7Ildob3BwZXIiLCAiQmlnIE1hYyIsICJSb3lhbCBXaXRoIENoZWVzZSIsICJWZWdnaWUgQnVyZGVyIn07CiAgcHJpdmF0ZSBzdGF0aWMgU3RyaW5nW10gc2FsYWROYW1lcyA9IHsiR2FyZGVuIiwgIkdyZWVrIiwgIkNoaWNrZW4iLCAiQ2Flc2FyIn07CiAgcHJpdmF0ZSBzdGF0aWMgU3RyaW5nW10gZHJpbmtOYW1lcyA9IHsiV2F0ZXIiLCAiQ29rZSIsICJQZXBzaSIsICJMZW1vbmFkZSJ9OwogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIAogICAgZm9yKGludCBpID0gMDsgaSA8IDEwMDAwOyBpKyspCiAgICAgIHRlc3RUcmlvKCk7CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkFsbCB0ZXN0cyBwYXNzZWQhIik7CiAgfQogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCB0ZXN0VHJpbygpIHsKICAgIGNoZWNrQWxsSW5zdGFuY2VWYXJpYWJsZXNBcmVQcml2YXRlKFRyaW8uY2xhc3MpOwogICAgCiAgICBDb25zdHJ1Y3RvcjxUcmlvPiBjb25zdHIgPSBudWxsOwogICAgdHJ5IHsKICAgICAgY29uc3RyID0gVHJpby5jbGFzcy5nZXREZWNsYXJlZENvbnN0cnVjdG9yKFNhbmR3aGljaC5jbGFzcywgU2FsYWQuY2xhc3Ms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U3lzdGVtLm91dC5wcmludGxuKCJOb3QgYWxsIGZpZWxkcyBvZiAiICsgYy5nZXROYW1lKCkgKyAiIGFyZSBwcml2YXRlISIpOwogICAgICAgIFN5c3RlbS5leGl0KDApOwogICAgICB9CiAgICB9CiAgfQp9CgpjbGFzcyBGb29kSXRlbSBpbXBsZW1lbnRzIE1lbnVJdGVtIHsKICAKICBwcml2YXRlIFN0cmluZyBuYW1lOwogIHByaXZhdGUgZG91YmxlIHByaWNlOwogIAogIHB1YmxpYyBGb29kSXRlbShTdHJpbmcgbiwgZG91YmxlIHApIHsKICAgIG5hbWUgPSBuOwogICAgcHJpY2UgPSBwOwogIH0KICAKICBwdWJsaWMgZG91YmxlIGdldFByaWNlKCkgewogICAgcmV0dXJuIHByaWNlOwogIH0KICAKICBwdWJsaWMgU3RyaW5nIGdldE5hbWUoKSB7CiAgICByZXR1cm4gbmFtZTsKICB9Cn0KCmNsYXNzIFNhbmR3aGljaCBleHRlbmRzIEZvb2RJdGVtIHsKICBwdWJsaWMgU2FuZHdoaWNoKFN0cmluZyBuLCBkb3VibGUgcCkgewogICAgc3VwZXIobiwgcCk7CiAgfQp9CgpjbGFzcyBTYWxhZCBleHRlbmRzIEZvb2RJdGVtIHsKICBwdWJsaWMgU2FsYWQoU3RyaW5nIG4sIGRvdWJsZSBwKSB7CiAgICBzdXBlcihuLCBwKTsKICB9Cn0KCmNsYXNzIERyaW5rIGV4dGVuZHMgRm9vZEl0ZW0gewogIHB1YmxpYyBEcmluayhTdHJpbmcgbiwgZG91YmxlIHApIHsKICAgIHN1cGVyKG4sIHApOwogIH0KfQ=="},"asBuffer":null},"loaded":true}}
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
import java.lang.reflect.*;

public class Main {
  
  private static String[] sandwhichNames = {"Whopper", "Big Mac", "Royal With Cheese", "Veggie Burder"};
  private static String[] saladNames = {"Garden", "Greek", "Chicken", "Caesar"};
  private static String[] drinkNames = {"Water", "Coke", "Pepsi", "Lemonade"};
  
  public static void main(String[] args) {
    
    for(int i = 0; i < 10000; i++)
      testTrio();
    System.out.println("All tests passed!");
  }
  
  public static void testTrio() {
    checkAllInstanceVariablesArePrivate(Trio.class);
    
    Constructor<Trio> constr = null;
    try {
      constr = Trio.class.getDeclaredConstructor(Sandwhich.class, Salad.class, Drink.class);
    }
    catch(Exception e) {
      System.out.println("Your Trio class does not have a 3 parameter constructor that takes a Sandwhich, Sald, and Drink in that order!");
      System.exit(0);
    }
    
    if(!MenuItem.class.isAssignableFrom(Trio.class)) {
      System.out.println("Your Trio class does not implement MenuItem!");
      System.exit(0);
    }
    
    Trio trio = null;
    
    String sandName = sandwhichNames[(int)(Math.random()*sandwhichNames.length)];
    String saladName = saladNames[(int)(Math.random()*saladNames.length)];
    String drinkName = drinkNames[(int)(Math.random()*drinkNames.length)];
    
    double sandPrice = Math.random()*10;
    double salPrice = Math.random()*10;
    double drinkPrice = Math.random()*10;
    
    Sandwhich sand = new Sandwhich(sandName, sandPrice);
    Salad sal = new Salad(saladName, salPrice);
    Drink drk = new Drink(drinkName, drinkPrice);
    try {
      trio = constr.newInstance(sand, sal, drk);
    }
    catch(Exception e) {
      System.out.println("Error trying to construct a Trio object!");
      System.exit(0);
    }
    
    String name = null;
    
    try {
      name = trio.getName();
    }
    catch(Exception e) {
      System.out.println("Error calling Trio object's getName() method!");
      System.exit(0);
    }
    
    String goal = sandName + "/" + saladName + "/" + drinkName + " Trio";
    
    if(!goal.equals(name)) {
      System.out.println("Trio's getName() returned\n\t" + name + "\n\nShould have been\n\t" + goal);
      System.exit(0);
    }
    
    double price = 0.0;
    
    try {
      price = trio.getPrice();
    }
    catch(Exception e) {
      System.out.println("Error calling Trio object's getPrice() method!");
      System.exit(0);
    }
    
    double min = Math.min(sandPrice, Math.min(salPrice, drinkPrice));
    double goalPrice = sandPrice + salPrice + drinkPrice - min;
    
    if(Math.abs(goalPrice - price) > 0.000001) {
      System.out.println("Result of calling getPrice() with sandwhich costing " + sandPrice + ", salad costing " + salPrice + ", and drink costing " + drinkPrice + " should be " + goalPrice + ", but Trio's getPrice() returns " + price + " instead.");
      System.exit(0);
    }
  }
  
  public static void checkAllInstanceVariablesArePrivate(Class c) {
    Field[] fields = c.getDeclaredFields();
    for(Field f: fields) {
      if((f.getModifiers() & Modifier.PRIVATE) != Modifier.PRIVATE)
      {
        System.out.println("Not all fields of " + c.getName() + " are private!");
        System.exit(0);
      }
    }
  }
}

class FoodItem implements MenuItem {
  
  private String name;
  private double price;
  
  public FoodItem(String n, double p) {
    name = n;
    price = p;
  }
  
  public double getPrice() {
    return price;
  }
  
  public String getName() {
    return name;
  }
}

class Sandwhich extends FoodItem {
  public Sandwhich(String n, double p) {
    super(n, p);
  }
}

class Salad extends FoodItem {
  public Salad(String n, double p) {
    super(n, p);
  }
}

class Drink extends FoodItem {
  public Drink(String n, double p) {
    super(n, p);
  }
}