-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoodItem.java
More file actions
149 lines (120 loc) · 5.63 KB
/
FoodItem.java
File metadata and controls
149 lines (120 loc) · 5.63 KB
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
146
147
148
package com.example.benjamin.blabfridgeapp;
/**
* Created by Benjamin on 2017-03-22.
* Copied from AndroidApp area of GitHub
*/
import java.util.Arrays;
import java.util.ArrayList;
class FoodItem{
public static final String matchRegexOpcodeDelimiter = "\\?";
public static final String opcodeDelimiter = "?";
public static final int TAGCODE_LENGTH = 10;
private String itemName;
private char[] tagCode;
private ComparableDate expiryDate;
private float lifetime; //the expiry date is set to [lifetime] days from now when the item is put in the fridge
private ArrayList<Float> warningTimes = new ArrayList(); //warningTimes will be the length of warningExpiryToLifetimeRatio.length
public static final float[] warningExpiryToLifetimeRatio = {1, (float)0.5, (float)0.14, (float)0.07, (float).047};
public FoodItem(char[] tagCode, String name){
this(tagCode, name, 1); //default lifetime of 1 day
}
public FoodItem(char[] tagCode, String name, float lifetime){
this(tagCode, name, lifetime, null);
}
public FoodItem(char[] tagCode, String name, float lifetime, ComparableDate expiryDate){
this.tagCode = tagCode.clone();
itemName = name;
this.lifetime = lifetime;
for (int i = 0; i < warningExpiryToLifetimeRatio.length; ++i) {
warningTimes.add(24 * lifetime * warningExpiryToLifetimeRatio[i]);
}
this.expiryDate = expiryDate;
}
public FoodItem(FoodItem anotherFoodItem){
this.itemName = new String(anotherFoodItem.itemName);
for (int i = 0; i < TAGCODE_LENGTH; ++i) {
this.tagCode[i] = anotherFoodItem.tagCode[i];
}
this.lifetime = anotherFoodItem.lifetime;
this.warningTimes = new ArrayList<Float>(anotherFoodItem.warningTimes);
//do not copy expiry information, renewExpiryDate() MUST be called
}
public FoodItem(char[] tagCode){ //DO NOT USE FOODITEMS CREATED WITH THIS METHOD, THIS IS ONY FOR EQUALS
this.tagCode = tagCode.clone();
}
public static FoodItem getFoodItemFromByteArray(char[] tagCode, byte[] bytes){
String splittableString = new String(bytes);
// System.out.println("Splitting " + t);
String[] strings = splittableString.split(matchRegexOpcodeDelimiter);
if(strings.length == 5){
return new FoodItem(tagCode, strings[1], Integer.parseInt(strings[2]), new ComparableDate(Integer.parseInt(strings[3])));
}
return new FoodItem(tagCode, strings[1], Integer.parseInt(strings[2])); //using packet format, the first is the opcode (ignored), second is name, third is lifetime
}
public byte[] to1Packet(){
byte[] buf = new byte[100];
buf[0] = '1';
buf[1] = opcodeDelimiter.getBytes()[0];
byte[] nameAsBytes = itemName.getBytes();
System.arraycopy(nameAsBytes, 0, buf, 2, nameAsBytes.length);
buf[2 + nameAsBytes.length] = opcodeDelimiter.getBytes()[0];
byte[] lifetimeAsBytes = Integer.toString(Math.round(lifetime)).getBytes();
System.arraycopy(lifetimeAsBytes, 0, buf, 2 + 1 + nameAsBytes.length, lifetimeAsBytes.length);
buf[2+1+lifetimeAsBytes.length + nameAsBytes.length] = opcodeDelimiter.getBytes()[0];
return buf;
}
public char[] getTagCode(){
return tagCode;
}
public float expiresInDays(){
return (expiryDate.daysUntil());
}
public float expiresInHours(){
return (expiryDate.hoursUntil());
}
public float getExpiryToLifetimeRatio(){
return (expiresInDays()/lifetime);
}
public void renewExpiryDate(){ //this is considered a secondary constructor, the only reason it isn't in the constructor is so that expiryDate can be renewed at the 'time of entry'
expiryDate = new ComparableDate(lifetime); //this should be called when the item is put in the fridge.
}
public boolean needsWarning(){
//assumes warning times are generated in order, 1st is the soonest, nth is the closest to expiry date
if(warningTimes.size() > 0){
if(expiresInHours() <= warningTimes.get(0)){
return true;
}
}
return false;
}
public boolean warned(){
if (warningTimes.size() > 0) {
warningTimes.remove(0);
return true;
}
return false;
}
@Override
public boolean equals(Object o){ //this equals method does not compare all fields, it returns true if the tagcodes match, to comply with stupid java's dumbass symmetry shit. It violates so many design rules to comply with one stupid design rule. java is dumb.
if (o instanceof FoodItem){
FoodItem i = (FoodItem) o;
if(new String(this.tagCode).equals(new String(i.getTagCode()))) return true;
}
// } else if (o instanceof String){ //this is a bit of a hack so that the linkedList can be searched by just a tagCode. Done because a hashTable cannot have duplicates
// ReaderClass.println("Checking tag code in fooditem equals method : " + (String) o + "compared with" + new String(tagCode)); //DEBUG
// return (this.tagCode.equals(((String) o).toCharArray()));
// }
return false;
}
public String toString(){
String retString = "[Name : " + itemName + ", tagCode : " + new String(tagCode) + ", expires in : " + expiryDate.daysUntil() + " days]";
return retString;
}
public String toHumanReadableString(){
String retString = itemName + " expires in : " + expiryDate.daysUntil() + " days";
return retString;
}
public String getName(){
return itemName;
}
}