-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtility.java
More file actions
220 lines (195 loc) · 5.51 KB
/
Utility.java
File metadata and controls
220 lines (195 loc) · 5.51 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.bridgelabz.utility;
/*
* created by: Bridge Labz
* Date 4/05/2016
*
* Perpose: Putting Commonly used function in single file.
1 function to take word,integer and double as input
2: function to read and write file.
3 function to match regex from string and replace with user details
**/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import com.bridgelabz.model.Stock;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.bridgelabz.model.UserDetails;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.io.File;
import java.io.FileWriter;
import com.bridgelabz.utility.SortingFunction;
public class Utility{
BufferedReader bufferedReader,br;
private final String REGEX_NAME = "<<name>>";
private final String REGEX_FULLNAME = "<<full name>> ";
private final String REGEX_MOBILE_NO = "xxxxxxxxxx";
private final String REGEX_DATE = "01/01/2016";
//constructor to initalize bufferedReader
public Utility(){
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}
//take input word
public String inputWord(){
try{
return bufferedReader.readLine();
}
catch(IOException exception){
System.out.println(exception.getMessage());
}
return "";
}
//Take Integer Input
public int inputInteger(){
try{
try{
return Integer.parseInt(bufferedReader.readLine());
}
catch(NumberFormatException exception){
System.out.println(exception.getMessage());
}
}catch(IOException exception){
System.out.println(exception.getMessage());
}
return 0;
}
//Take Double Input
public double inputDouble(){
try{
try{
return Double.parseDouble(bufferedReader.readLine());
}
catch(NumberFormatException exception){
System.out.println(exception.getMessage());
}
}catch(IOException exception){
System.out.println(exception.getMessage());
}
return 0.0;
}
//Take number of random number to genrate as input and return array of random number
public int[] getRandomArray(int number){
Random randomGenerator = new Random();
int array[]=new int[number];
for(int i=0;i<number;i++){
array[i]=randomGenerator.nextInt();
}
return array;
}
//Read the file and take stock detils as input and return arraylist of stock details
public ArrayList<Stock> getStockDetails(String fileName){
ArrayList<Stock> stockList=new ArrayList<Stock>();
try{
br=new BufferedReader(new FileReader(fileName));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
SortingFunction sortingFunction=new SortingFunction();
while(line!=null){
String words[]=sortingFunction.wordsArrayFromStatement(line);
try{
stockList.add(new Stock(words[0],Integer.parseInt(words[1]),Integer.parseInt(words[2])));
}
catch(NumberFormatException exception){
}
catch(ArrayIndexOutOfBoundsException exception){
}
line=br.readLine();
}
return stockList;
}
catch(Exception exception){
return null;
}
finally{
try{
br.close();
}
catch(IOException exception){
}
}
}
//Function take String in dd/MM/yyyy format and return Date Object
public Date stringToDate(String date){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
return sdf.parse(date);
}
catch(ParseException parseException){
return null;
}
}
//format date object in this format 01/12/2016
public String getFormatedDate(Date date){
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
return sdf.format(date);
}
//Regex pattern matcher match the string and replace the regex pattern with user details.
public String convertString(UserDetails userDetails,String message){
Pattern p = Pattern.compile(REGEX_NAME);
Matcher m = p.matcher(message);
message = m.replaceAll(userDetails.getfName());
p = Pattern.compile(REGEX_FULLNAME);
m = p.matcher(message);
message = m.replaceAll(userDetails.getfName()+" "+userDetails.getlName());
p = Pattern.compile(REGEX_MOBILE_NO);
m = p.matcher(message);
message = m.replaceAll(userDetails.mobileNo());
p = Pattern.compile(REGEX_DATE);
m = p.matcher(message);
message = m.replaceAll(userDetails.date());
return message;
}
//Take file name as input and return string of file text
public String getFileText(String fileName){
try{
br=new BufferedReader(new FileReader(fileName));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
while(line!=null){
sb.append(line);
sb.append(System.lineSeparator());
line=br.readLine();
}
return sb.toString();
}
catch(Exception exception){
return null;
}
finally{
try{
br.close();
}
catch(IOException exception){
}
}
}
//this function take two parameter
// 1 what data to write.
// 2 file name
public void writeToFile(String data,String fileName)throws Exception{
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write(data);
writer.flush();
writer.close();
}
public void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
}