@matfah/

Vehicle FR 2006

Java

No description

fork
loading
Files
  • Main.java
  • Item.java
  • TaxableItem.java
  • Vehicle.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
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    for(int i = 0; i < 5; i++)
      testPurchasePrice();
    
    for(int i = 0; i < 5; i++)
      testVehicle();
    
    System.out.println("Passed all test!");
  }
  
  public static void testPurchasePrice() {
    
    double rate = Math.random()/2 + 0.5;
    double price = Math.random()*100 + 50;
    
    MyItem item = new MyItem(rate, price);
    double value = item.purchasePrice();
    double correct = (rate + 1.0) * price;
    
    if(Math.abs(value - correct) >= 0.00001) {
      System.out.println("purhcasePrice() calcultion is not correct");
      System.exit(0);
    }
  }
  
  public static void testVehicle() {
    
    double[] values = {Math.random(), Math.random(), Math.random()};
    
    checkAllInstanceVariablesArePrivate(Vehicle.class);
    
    Constructor<Vehicle> vehConst = null;
    try {
      vehConst = Vehicle.class.getDeclaredConstructor(double.class, double.class, double.class);
    }
    catch(Exception e) {
      System.out.println("Vehicle class does not have a 3 parameter constructor that takes 3 decimal values");
      System.exit(0);
    }
    
    Vehicle vehicle = null;
    try {
      vehicle = vehConst.newInstance(values[0], values[1], values[2]);
    }
    catch(Exception e) {
      System.out.println("Error trying to construct a Vehicle object!");
      System.exit(0);
    }
    
    Field taxRateField = null;
    double taxRate = 0.0;
    try {
      taxRateField = TaxableItem.class.getDeclaredField("taxRate");
      taxRateField.setAccessible(true);
      taxRate = (Double)taxRateField.get(vehicle);
      
      if( Math.abs(taxRate - values[0]) >= 0.00001 &&
          Math.abs(taxRate - values[1]) >= 0.00001 &&
          Math.abs(taxRate - values[2]) >= 0.00001) {
            System.out.println("super() is not called correctly in Vehicle's constructor - taxRate instance variable is not assigned correctly");
            System.exit(0);
      }
          
    }
    catch(Exception e) {
      System.out.println("Did you change the TaxableItem instance variables...?!?!");
      System.exit(0);
    }
    
    double sum = values[0] + values[1] + values[2] - taxRate;
    
    double listPrice = 0.0; 
    
    try {
      listPrice = vehicle.getListPrice();
    }
    catch(Exception e) {
      System.out.println("Error calling getListPrice() from a Vehicle object");
      System.exit(0);
    }
    
    if(Math.abs(listPrice - sum) >= 0.00001) {
      System.out.println("Vehicle's getListPrice() is not completed correctly");
      System.exit(0);
    }
    
    double pPrice;
    try {
      pPrice = vehicle.purchasePrice();
      if(Math.abs(sum * (1 + taxRate) - pPrice) >= 0.00001) {
        System.out.println("Vehicle's purchasePrice() is returning the wrong value");
      }
    }
    catch(Exception e) {
      System.out.println("Error calling purchasePrice() from a Vehicle object");
      System.exit(0);
    }
    
    double newMarkup = Math.random() * 50;
    
    //need to check purchase price and change markup
    try {
      vehicle.changeMarkup(newMarkup);
    }
    catch(Exception e) {
      System.out.println("Error calling changeMarkup() from a Vehicle object");
      System.exit(0);
    }
    
    try {
      listPrice = vehicle.getListPrice();
    }
    catch(Exception e) {
      System.out.println("Error calling getListPrice() from a Vehicle object");
      System.exit(0);
    }
    
    listPrice -= newMarkup;
    if((Math.abs(listPrice - values[0]) > 0.0001 &&
       Math.abs(listPrice - values[1]) > 0.0001 &&
       Math.abs(listPrice - values[2]) > 0.0001) ||
       Math.abs(listPrice - taxRate) < 0.0001) {
        System.out.println("changeMarkup() is not working for a Vehicle object");
        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 MyItem extends TaxableItem { double price; public MyItem(double r, double p) { super(r); price = p; } public double getListPrice() { return price; } }