-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBascketParser.java
65 lines (49 loc) · 1.88 KB
/
BascketParser.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
import exceptions.WrongFormatException;
import model.BascketProduct;
import model.ShoppingBascket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
public class BascketParser {
public ShoppingBascket parse(Reader input) throws IOException, WrongFormatException {
BufferedReader bufReader = new BufferedReader(input);
ShoppingBascket bascket = new ShoppingBascket();
String line = bufReader.readLine();
do{
if(verifyEntry(line))
bascket.addProduct(new BascketProduct(getVolume(line), getName(line), getPrice(line)));
else
throw new WrongFormatException("Bascket entry not correctly formatted: "+line+"\nThe correct format is: volume name at price");
}while((line = bufReader.readLine()) != null);
return bascket;
}
public int getVolume(String s) {
return Integer.valueOf(s.split(" ")[0]);
}
public String getName(String s) {
StringBuilder name = new StringBuilder();
String[] values = s.split(" ");
for(int i = 1; i<values.length - 3; i++){
name.append(values[i]);
name.append(" ");
}
name.append(values[values.length -3]);
return name.toString();
}
public double getPrice(String s) {
String[] values = s.split(" ");
return Double.valueOf(values[values.length - 1]);
}
public boolean verifyEntry(String s) {
String[] values = s.split(" ");
if(values.length < 4) return false;
if(!values[values.length - 2].equals("at")) return false;
for(char c : values[0].toCharArray())
if(!Character.isDigit(c))
return false;
for(char c : values[values.length - 1].toCharArray())
if(!Character.isDigit(c) && c != '.')
return false;
return true;
}
}