Skip to content

Commit 6e3a20d

Browse files
Updated some functionality of the coreapi.
1 parent 653f35f commit 6e3a20d

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

api/src/main/java/coreapi/Cafeteria.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Cafeteria implements Serializable
1818
{
1919
private static final long serialVersionUID = 1L;
2020
private LinkedHashMap<Product, Integer> productStock;
21-
private List<Order> orderHistory;
21+
private List<Integer> orderHistory;
2222
private String name;
2323
private int id;
2424
private List<String> types;
@@ -36,7 +36,7 @@ public class Cafeteria implements Serializable
3636
public Cafeteria(int assignedId, String givenName, String Email)
3737
{
3838
productStock = new LinkedHashMap<Product, Integer>();
39-
orderHistory = new ArrayList<Order>();
39+
orderHistory = new ArrayList<Integer>();
4040
id = assignedId;
4141
name = givenName;
4242
types = new ArrayList<String>();
@@ -193,14 +193,14 @@ public void removeProduct(Product prod)
193193
*/
194194
public void registerOrder(Order newOrder)
195195
{
196-
orderHistory.add(newOrder);
196+
orderHistory.add(Integer.valueOf(newOrder.getId()));
197197
}
198198

199199
/**
200200
* Returns the list of orders registered to this cafeteria.
201201
* @return Returns the list of orders registered to this cafeteria.
202202
*/
203-
public List<Order> getOrders()
203+
public List<Integer> getOrdersID()
204204
{
205205
return List.copyOf(orderHistory);
206206
}

api/src/main/java/coreapi/OrderImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,11 @@ public BigDecimal totalCost()
146146
BigDecimal sumCost = BigDecimal.ZERO;
147147
BigDecimal productCost;
148148
BigDecimal productQuantity;
149-
System.out.println(basket.isEmpty());
150149
for (Map.Entry<Product, Integer> entry : basket.entrySet())
151150
{
152151
// We create a copy of the product's price as to not modify its value via this reference.
153152
productCost = entry.getKey().getPrice();
154153
productQuantity = new BigDecimal(entry.getValue().intValue());
155-
System.out.println(entry.getKey().getName()+"-"+productCost.doubleValue()+"--"+productQuantity.intValue());
156154
productCost = productCost.multiply(productQuantity);
157155

158156
sumCost = sumCost.add(productCost);

api/src/main/java/coreapi/OrderService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ public BigDecimal getTotalDailyRegister(Cafeteria coffe, LocalDate date) throws
165165

166166
if(date.compareTo(LocalDate.now()) <= 0)
167167
{
168-
for(Order ord: coffe.getOrders())
168+
for(Integer ordID: coffe.getOrdersID())
169169
{
170-
if(ord.getDate().toLocalDate().equals(date))
170+
Order aux = oData.getOrder(ordID.intValue());
171+
if(aux.getDate().toLocalDate().equals(date))
171172
{
172-
dailyRegister = dailyRegister.add(ord.totalCost());
173-
System.out.println("Order "+ord.getId()+": "+ord.totalCost());
173+
dailyRegister = dailyRegister.add(aux.totalCost());
174174
}
175175
}
176176
}
@@ -194,9 +194,10 @@ public int getNumberOfDailyOrders(Cafeteria coffee, LocalDate date) throws Inval
194194
int numberOfOrders = 0;
195195
if(date.compareTo(LocalDate.now()) <= 0)
196196
{
197-
for(Order ord: coffee.getOrders())
197+
for(Integer ordID : coffee.getOrdersID())
198198
{
199-
if(ord.getDate().toLocalDate().equals(date))
199+
Order aux = oData.getOrder(ordID.intValue());
200+
if(aux.getDate().toLocalDate().equals(date))
200201
{
201202
numberOfOrders++;
202203
}

api/src/main/java/coreapi/Product.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@ public interface Product {
3636
*/
3737
public String getType();
3838

39+
@Override
3940
public boolean equals(Object o);
41+
42+
@Override
43+
public int hashCode();
4044
}

api/src/main/java/coreapi/ProductImpl.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,33 @@ public String getType()
7272
return type;
7373
}
7474

75+
@Override
7576
public boolean equals(Object o)
7677
{
7778
if (o == this)
7879
{
7980
return true;
8081
}
8182

82-
if (!(o instanceof Product))
83+
if(!(o instanceof Product) && !(o instanceof ProductImpl))
8384
{
8485
return false;
8586
}
8687

8788
Product p = (Product) o;
88-
8989
return p.getId() == this.id;
9090
}
91+
92+
@Override
93+
public int hashCode()
94+
{
95+
int result = 17;
96+
97+
result = 31 * result + id;
98+
result = 31 * result + price.hashCode();
99+
result = 31 * result + name.hashCode();
100+
result = 31 * result + type.hashCode();
101+
102+
return result;
103+
}
91104
}

api/src/main/java/coreapi/User.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class User implements Serializable
2626
private String surname;
2727
private String email;
2828

29-
//This list stores all the orders that the user has made
30-
private List<Order> UserOrderList;
29+
//This list stores all the ids of the orders that the user has made
30+
private List<Integer> UserOrderList;
3131

3232
public User()
3333
{
34-
UserOrderList = new ArrayList<Order>();
34+
UserOrderList = new ArrayList<Integer>();
3535
}
3636
/**
3737
* Creates a new instance of a specific user.
@@ -50,7 +50,7 @@ public User(String Name, String Surname, int ncard, LocalDate birth_date, int Dn
5050
this.surname = Surname;
5151
this.dni = Dni;
5252
this.email = Email;
53-
UserOrderList = new ArrayList<Order>();
53+
UserOrderList = new ArrayList<Integer>();
5454
}
5555

5656
/**
@@ -102,7 +102,7 @@ public String getEmail()
102102
* Returns the list of orders made by the user.
103103
* @return Returns the list of orders made by the user.
104104
*/
105-
public List<Order> getUserOrderList()
105+
public List<Integer> getUserOrderList()
106106
{
107107
return List.copyOf(UserOrderList);
108108
}
@@ -195,12 +195,12 @@ public LocalDate getBirthDate()
195195
}
196196

197197
/**
198-
* Set the new order to the list of the user order.
198+
* Set the new order id to the list of the user order.
199199
* @param o The new order to set to the list.
200200
*/
201201
public void setOrder(Order o)
202202
{
203-
boolean b = UserOrderList.add(o);
203+
boolean b = UserOrderList.add(Integer.valueOf(o.getId()));
204204
}
205205

206206
/**

0 commit comments

Comments
 (0)