Skip to content

Commit 122804a

Browse files
Implemented full support for the management of prices with the BigDecimal class.
1 parent 519c2f9 commit 122804a

File tree

8 files changed

+84
-81
lines changed

8 files changed

+84
-81
lines changed

src/main/java/coreapi/Menu.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
/**
2+
* @author Fran
3+
* @author Borja
4+
* @version 0.2
5+
*/
16
package coreapi;
27

8+
import java.math.BigDecimal;
39
import java.util.ArrayList;
410
import java.util.Collections;
511
import java.util.LinkedHashMap;
@@ -39,12 +45,20 @@ public Menu(int assignedId, String assignedName)
3945
* Returns the combined cost of all products that make up this menu.
4046
* @return the cost of the entire menu.
4147
*/
42-
public float getPrice()
48+
public BigDecimal getPrice()
4349
{
44-
float sumCost = 0;
50+
BigDecimal sumCost = BigDecimal.ZERO;
51+
BigDecimal productCost = BigDecimal.ZERO;
52+
BigDecimal productQuantity = BigDecimal.ZERO;
53+
4554
for (Map.Entry<Product, Integer> entry : productsMenu.entrySet())
4655
{
47-
sumCost += entry.getKey().getPrice() * entry.getValue().intValue();
56+
// We create a copy of the product's price as to not modify its value via this reference.
57+
productCost = entry.getKey().getPrice();
58+
productQuantity = new BigDecimal(entry.getValue().intValue());
59+
productCost = productCost.multiply(productQuantity);
60+
61+
sumCost = sumCost.add(productCost);
4862
}
4963
return sumCost;
5064
}

src/main/java/coreapi/OrderService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package coreapi;
22

3+
import java.math.BigDecimal;
34
import java.util.Date;
45

56

@@ -165,19 +166,19 @@ public void OrderStatus_Finished(OrderImpl ord)
165166
* PRECONDITION: Receive a date
166167
* POSTCONDITION: Return the total of all orders for the date entered.
167168
*/
168-
public float getDailyRegister(Cafeteria coffe, Date date)
169+
public BigDecimal getDailyRegister(Cafeteria coffe, Date date)
169170
{
170-
float total = 0; //We save the total of the day
171+
BigDecimal dailyRegister = BigDecimal.ZERO;
171172

172173
//We go through the order history of the establishment
173174
for(Order ord: coffe.orderHistory)
174175
{
175176
if(ord.getDate() == date)
176177
{
177-
total += ord.totalCost();
178+
dailyRegister = dailyRegister.add(ord.totalCost());
178179
}
179180
}
180-
return total;
181+
return dailyRegister;
181182
}
182183

183184

src/main/java/coreapi/ProductCatalog.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package coreapi;
1010

11+
import java.math.BigDecimal;
1112

1213
public class ProductCatalog {
1314

@@ -33,16 +34,16 @@ public static ProductCatalog Instance()
3334
*/
3435
private ProductCatalog()
3536
{
36-
products[0] = new ProductImpl(0, (float) 1.2, "Patatas fritas");
37-
products[1] = new ProductImpl(1, (float) 1.7, "Bacon-queso-huevo");
38-
products[2] = new ProductImpl(2, (float) 0.9, "Café con leche");
39-
products[3] = new ProductImpl(3, (float) 0.5, "Doritos");
40-
products[4] = new ProductImpl(4, (float) 1.6, "Monster");
41-
products[5] = new ProductImpl(5, (float) 1.3, "Bocadillo de tortilla");
42-
products[6] = new ProductImpl(6, (float) 0.8, "Botella de agua");
43-
products[7] = new ProductImpl(7, (float) 0.7, "Donut blanco");
44-
products[8] = new ProductImpl(8, (float) 0.75, "Donut de chocolate");
45-
products[9] = new ProductImpl(9, (float) 1.4, "Sándwich de roquefort");
37+
products[0] = new ProductImpl(0, new BigDecimal(1.2), "Patatas fritas");
38+
products[1] = new ProductImpl(1, new BigDecimal(1.7), "Bacon-queso-huevo");
39+
products[2] = new ProductImpl(2, new BigDecimal(0.9), "Café con leche");
40+
products[3] = new ProductImpl(3, new BigDecimal(0.5), "Doritos");
41+
products[4] = new ProductImpl(4, new BigDecimal(1.6), "Monster");
42+
products[5] = new ProductImpl(5, new BigDecimal(1.3), "Bocadillo de tortilla");
43+
products[6] = new ProductImpl(6, new BigDecimal(0.8), "Botella de agua");
44+
products[7] = new ProductImpl(7, new BigDecimal(0.7), "Donut blanco");
45+
products[8] = new ProductImpl(8, new BigDecimal(0.75), "Donut de chocolate");
46+
products[9] = new ProductImpl(9, new BigDecimal(1.4), "Sándwich de roquefort");
4647
}
4748
/**
4849
* Returns the corresponding reference to a certain product, determined by its identifier.

src/test/java/coreapiTest/MenuTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package coreapiTest;
22

3+
import java.math.BigDecimal;
34
import java.util.List;
45

56
import org.junit.After;
@@ -56,8 +57,14 @@ public void PriceCheckMenu()
5657
{
5758
myMenu.addProductToMenu(product1, 2);
5859
myMenu.addProductToMenu(product2, 4);
59-
float expectedPrice = (product1.getPrice() * 2) + (product2.getPrice() * 4);
60-
Assert.assertEquals(expectedPrice, myMenu.getPrice(), 0.0);
60+
61+
BigDecimal product1Cost = product1.getPrice();
62+
BigDecimal product2Cost = product2.getPrice();
63+
product1Cost = product1Cost.multiply(new BigDecimal(2));
64+
product2Cost = product2Cost.multiply(new BigDecimal(4));
65+
66+
BigDecimal expectedCost = product1Cost.add(product2Cost);
67+
Assert.assertEquals(0, expectedCost.compareTo(myMenu.getPrice()));
6168
}
6269

6370
@Test

src/test/java/coreapiTest/OrderImplTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/**
2+
* @author Borja
3+
* @version 0.2
4+
*/
15
package coreapiTest;
26

37
import coreapi.*;
48
import org.junit.Assert;
59

10+
import java.math.BigDecimal;
611
import java.util.List;
712
import java.util.Map;
813

@@ -156,16 +161,22 @@ public void RemoveNonPositiveProductQuantityCheck()
156161
@Test
157162
public void TotalCostEmptyCheck()
158163
{
159-
Assert.assertEquals(0.0, myOrder.totalCost(), 0.0);
164+
Assert.assertEquals(0, myOrder.totalCost().compareTo(BigDecimal.ZERO));
160165
}
161166

162167
@Test
163168
public void TotalCostCheck()
164169
{
165170
myOrder.addProduct(product1.getId(), 2);
166171
myOrder.addProduct(product2.getId(), 4);
167-
float calculatedCost = (product1.getPrice() * 2) + (product2.getPrice() * 4);
168-
Assert.assertEquals(calculatedCost, myOrder.totalCost(), 0.0);
172+
173+
BigDecimal product1Cost = product1.getPrice();
174+
BigDecimal product2Cost = product2.getPrice();
175+
product1Cost = product1Cost.multiply(new BigDecimal(2));
176+
product2Cost = product2Cost.multiply(new BigDecimal(4));
177+
178+
BigDecimal expectedCost = product1Cost.add(product2Cost);
179+
Assert.assertEquals(0, expectedCost.compareTo(myOrder.totalCost()));
169180
}
170181

171182
}

src/test/java/coreapiTest/OrderServiceTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package coreapiTest;
22

33
import coreapi.*;
4+
5+
import java.math.BigDecimal;
46
import java.util.Date;
57

68
import org.junit.After;
@@ -286,9 +288,10 @@ public void OrderStatus_FinishedCheck_OrderService()
286288
*/
287289
public void DailyRegisterCheck_OrderService()
288290
{
289-
float total = ordSer.getDailyRegister(coffe, date);
290-
float correctTotal=(ord1.totalCost() + ord2.totalCost() + ord3.totalCost());
291-
Assert.assertTrue("Incorrect total",total == correctTotal);
291+
BigDecimal expectedRegister = BigDecimal.ZERO;
292+
expectedRegister = expectedRegister.add(ord1.totalCost()).add(ord2.totalCost()).add(ord3.totalCost());
293+
294+
Assert.assertEquals(0, expectedRegister.compareTo(ordSer.getDailyRegister(coffe, date)));
292295

293296
}
294297
}
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,40 @@
1+
/**
2+
* @author Fran
3+
* @author Borja
4+
* @version 0.2
5+
*/
16
package coreapiTest;
27

38
import coreapi.ProductImpl;
49
import coreapi.ProductCatalog;
510

611
import org.junit.Assert;
712

13+
import java.math.BigDecimal;
14+
815
import org.junit.After;
916
import org.junit.Before;
1017
import org.junit.Test;
1118

1219
public class ProductImplTest
1320
{
14-
private ProductImpl Product1;
15-
private ProductImpl Product2;
16-
private ProductImpl Product3;
17-
18-
@Before
19-
public void setUp()
20-
{
21-
Product1 = (ProductImpl) ProductCatalog.Instance().getProduct(0);
22-
Product2 = (ProductImpl) ProductCatalog.Instance().getProduct(1);
23-
Product3 = (ProductImpl) ProductCatalog.Instance().getProduct(2);
24-
}
25-
26-
@After
27-
public void tearDown()
28-
{
29-
Product1 = null;
30-
Product2 = null;
31-
Product3 = null;
32-
}
21+
private ProductImpl myProduct = (ProductImpl) ProductCatalog.Instance().getProduct(0);
3322

3423
@Test
3524
public void IdCheckProductImpl()
3625
{
37-
Assert.assertEquals("DifferentIdAtProductImpl1",0,Product1.getId());
38-
Assert.assertEquals("DifferentIdAtProductImpl2",1,Product2.getId());
39-
Assert.assertEquals("DifferentIdAtProductImpl3",2,Product3.getId());
26+
Assert.assertEquals("DifferentIdAtProductImpl1", 0, myProduct.getId());
4027
}
4128

4229
@Test
4330
public void PriceCheckProductImpl()
4431
{
45-
Assert.assertEquals("DifferentPriceAtProductImpl1",(float) 1.2,Product1.getPrice(), 0.0);
46-
Assert.assertEquals("DifferentPriceAtProductImpl2",(float) 1.7,Product2.getPrice(), 0.0);
47-
Assert.assertEquals("DifferentPriceAtProductImpl3",(float) 0.9,Product3.getPrice(), 0.0);
32+
Assert.assertEquals(0, myProduct.getPrice().compareTo(new BigDecimal(1.2)));
4833
}
4934

5035
@Test
5136
public void NameCheckProductImpl()
5237
{
53-
Assert.assertEquals("DifferentNameAtProductImpl1","Patatas fritas",Product1.getName());
54-
Assert.assertEquals("DifferentNameAtProductImpl2","Bacon-queso-huevo",Product2.getName());
55-
Assert.assertEquals("DifferentNameAtProductImpl3","Café con leche",Product3.getName());
38+
Assert.assertEquals("DifferentNameAtProductImpl1", "Patatas fritas", myProduct.getName());
5639
}
5740
}
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
1+
/**
2+
* @author Fran
3+
* @author Borja
4+
* @version 0.2
5+
*/
16
package coreapiTest;
27

38
import coreapi.*;
49
import org.junit.Assert;
510

11+
import java.math.BigDecimal;
12+
613
import org.junit.After;
714
import org.junit.Before;
815
import org.junit.Test;
916

1017
public class ProductTest
1118
{
12-
private Product Product1;
13-
private Product Product2;
14-
private Product Product3;
15-
16-
@Before
17-
public void setUp()
18-
{
19-
Product1 = ProductCatalog.Instance().getProduct(3);
20-
Product2 = ProductCatalog.Instance().getProduct(4);
21-
Product3 = ProductCatalog.Instance().getProduct(5);
22-
}
23-
24-
@After
25-
public void tearDown()
26-
{
27-
Product1 = null;
28-
Product2 = null;
29-
Product3 = null;
30-
}
19+
private Product myProduct = ProductCatalog.Instance().getProduct(3);
3120

3221
@Test
3322
public void IdCheckProduct()
3423
{
35-
Assert.assertEquals("DifferentIdAtProduct1",3,Product1.getId());
36-
Assert.assertEquals("DifferentIdAtProduct2",4,Product2.getId());
37-
Assert.assertEquals("DifferentIdAtProduct3",5,Product3.getId());
24+
Assert.assertEquals("DifferentIdAtProduct1", 3, myProduct.getId());
3825
}
3926

4027
@Test
4128
public void PriceCheckProduct()
4229
{
43-
Assert.assertEquals("DifferentPriceAtProduct1",(float) 0.5,Product1.getPrice(), 0.0);
44-
Assert.assertEquals("DifferentPriceAtProduct2",(float) 1.6,Product2.getPrice(), 0.0);
45-
Assert.assertEquals("DifferentPriceAtProduct3",(float) 1.3,Product3.getPrice(), 0.0);
30+
Assert.assertEquals(0, myProduct.getPrice().compareTo(new BigDecimal(0.5)));
4631
}
4732

4833
@Test
4934
public void NameCheckProduct()
5035
{
51-
Assert.assertEquals("DifferentNameAtProduct1","Doritos",Product1.getName());
52-
Assert.assertEquals("DifferentNameAtProduct2","Monster",Product2.getName());
53-
Assert.assertEquals("DifferentNameAtProduct3","Bocadillo de tortilla",Product3.getName());
36+
Assert.assertEquals("DifferentNameAtProduct1", "Doritos", myProduct.getName());
5437
}
5538
}

0 commit comments

Comments
 (0)