-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputParsing.java
87 lines (75 loc) · 3.09 KB
/
InputParsing.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
/* Author: Arash Khazaei
* Description: The InputParsing class has member data and member functions that can be used to parse user input
*/
public class InputParsing {
public static final int DOLLAR_AMOUNT = 0;
public static final int WORKER_AMOUNT = 1;
public static final int CATEGORY = 2;
/* Extracts the correct category from a given input
* @param categoryInput - the user input of the category
* @return - One of either four options: "FOOD", "PHARMA", "ELECTRONICS", "OTHER"
*/
public static String extractCategory (String categoryInput) {
String formattedCategory = categoryInput.toLowerCase();
if (formattedCategory == "food")
return Project.FOOD;
if(formattedCategory == "drugs" || formattedCategory == "pharmaceutical")
return Project.PHARMA;
if (formattedCategory == "electronics")
return Project.ELECTRONICS;
return Project.OTHER;
}
/* Extracts the correct number of workers from a given input
* @param inputWorkers - the user input of the number of workers
* @return - a string representation of a single integer
*/
public static String extractNumWorkers (String inputWorkers){
String formattedWorker = inputWorkers.toLowerCase().replace("people","");
formattedWorker = formattedWorker.replace("person","");
formattedWorker = formattedWorker.trim();
//Throws an exception if a valid integer was not inputted
int numWorkers;
try {
numWorkers = Integer.parseInt(formattedWorker);
}
catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Number of Workers cannot be : " + inputWorkers);
}
if (numWorkers < 0){
throw new IllegalArgumentException("Number of Workers cannot be : " + inputWorkers);
}
return formattedWorker;
}
/* Extracts the correct dollar amount of a given userinput
* @param inputAmount - the user input of the initial dollar amount
* @return - a string representation of the dollar amount
*/
public static String extractDollarAmount (String inputAmount){
String formattedAmount = inputAmount.replace("$","");
//Throws an exception if a valid double was not inputted
double moneyAmount;
try {
moneyAmount = Float.parseFloat(formattedAmount);
}
catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Initial amount cannot be : " + inputAmount);
}
if (moneyAmount < 0){
throw new IllegalArgumentException("Initial amount cannot be : " + inputAmount);
}
return formattedAmount;
}
/* Extracts all the inputs given by the user
* @param inputWorkers - the user input of the initial dollar amount
* @param inputTeamSize - the user input of the number of workers
* @param inputCategory - the user input of the category
* @return - an array containing all the formatted user inputs
*/
public static String [] extractInputs (String inputAmount, String inputTeamSize, String inputCategory) {
String [] finalOutput = new String[3];
finalOutput[DOLLAR_AMOUNT] = extractDollarAmount(inputAmount);
finalOutput[WORKER_AMOUNT] = extractNumWorkers(inputTeamSize);
finalOutput[CATEGORY] = extractCategory(inputCategory);
return finalOutput;
}
}