Skip to content

Commit 2102d50

Browse files
authored
Create ExportsCSVReading.java
1 parent bf874d7 commit 2102d50

File tree

1 file changed

+376
-0
lines changed

1 file changed

+376
-0
lines changed
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
//Reading and printing of CSV File consiting data related to Countries Export and its value
2+
//GUI (Graphical User Interface based program)
3+
//NOTE : Take care of the file and package name and make sure the csv file location is set
4+
/*
5+
* Coder : Phantom-fs
6+
*/
7+
8+
package ExportsCSV;
9+
10+
11+
//This is an external library, use steps in CSV README.md file to know how to add it
12+
import org.apache.commons.csv.*;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
import java.io.*;
16+
import java.util.*;
17+
18+
public class ExportsCSVReading
19+
{
20+
String nl = System.lineSeparator();
21+
static int count = 0;
22+
23+
//the method to create CSV parser and read the CSV file
24+
public static @Nullable CSVParser readCSV()
25+
{
26+
try
27+
{
28+
//location of the CSV file, change it according to your usage
29+
FileReader fileReader = new FileReader("B:\\3- Java Programs\\IdeaProjects\\CSVData\\CSV Files\\exports\\exportdata.csv");
30+
31+
//inline variable, returning the parser to whichever method called it
32+
return new CSVParser(fileReader, CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
33+
}
34+
catch (IOException e)
35+
{
36+
throw new RuntimeException(e);
37+
}
38+
catch (Exception e)
39+
{
40+
System.out.println("Error");
41+
}
42+
return null;
43+
}
44+
45+
public void printFullCSV ()
46+
{
47+
try
48+
{
49+
Styling.lineTitle("Countries, their exports and export value : ");
50+
51+
//the CSVParser object is returned from the readCSV() method, thus not recreating multiple times
52+
//Objects.requireNonNull() to make sure that the csvParser object is not null
53+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
54+
{
55+
Styling.color("Country - " + csvRecord.get("Country"), "red");
56+
Styling.color("Exports - " + csvRecord.get("Exports"), "purple");
57+
Styling.color("Value (dollars) - " + csvRecord.get("Value (dollars)"), "blue");
58+
Styling.line();
59+
}
60+
}
61+
catch (Exception e)
62+
{
63+
System.out.println("Error");
64+
}
65+
}
66+
67+
public void printCountryAndExports ()
68+
{
69+
try
70+
{
71+
Styling.lineTitle("Countries and their exports : ");
72+
73+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
74+
{
75+
Styling.color("Country - " + csvRecord.get("Country"), "red");
76+
Styling.color("Exports - " + csvRecord.get("Exports"), "purple");
77+
Styling.line();
78+
}
79+
}
80+
catch (Exception e)
81+
{
82+
System.out.println("Error");
83+
}
84+
}
85+
86+
public void printCountryAndValue ()
87+
{
88+
try
89+
{
90+
Styling.lineTitle("Countries and their export value : ");
91+
92+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
93+
{
94+
Styling.color("Country - " + csvRecord.get("Country"), "red");
95+
Styling.color("Value (dollars) - " + csvRecord.get("Value (dollars)"), "blue");
96+
Styling.line();
97+
}
98+
}
99+
catch (Exception e)
100+
{
101+
System.out.println("Error");
102+
}
103+
}
104+
105+
public void printCountries ()
106+
{
107+
try
108+
{
109+
Styling.lineTitle("Countries : ");
110+
111+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
112+
{
113+
Styling.colorTB(csvRecord.get("Country"), "red");
114+
115+
//after every 5 countries, a new line is printed
116+
if (csvRecord.getRecordNumber() % 5 == 0)
117+
System.out.println();
118+
}
119+
Styling.line();
120+
}
121+
catch (Exception e)
122+
{
123+
System.out.println("Error");
124+
}
125+
}
126+
127+
public void printExportOfInterest (String export)
128+
{
129+
count = 0;
130+
try
131+
{
132+
Styling.lineTitle("Countries that export " + export + " : ");
133+
134+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
135+
{
136+
if (csvRecord.get("Exports").contains(export))
137+
{
138+
Styling.colorTB(csvRecord.get("Country"), "red");
139+
count++;
140+
141+
//newline after every 5 countries
142+
if (count % 5 == 0)
143+
System.out.println();
144+
}
145+
}
146+
Styling.line();
147+
}
148+
catch (Exception e)
149+
{
150+
System.out.println("Error");
151+
}
152+
}
153+
154+
public void exportsByCountry (String country)
155+
{
156+
try
157+
{
158+
Styling.lineTitle("Exports of " + country + " : ");
159+
160+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
161+
{
162+
if (csvRecord.get("Country").equals(country))
163+
{
164+
Styling.color(csvRecord.get("Exports"), "purple");
165+
Styling.line();
166+
}
167+
}
168+
}
169+
catch (Exception e)
170+
{
171+
System.out.println("Error");
172+
}
173+
}
174+
175+
public void exportsValueByCountry (String country)
176+
{
177+
try
178+
{
179+
Styling.lineTitle("Export Value of " + country + " : ");
180+
181+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
182+
{
183+
if (csvRecord.get("Country").equals(country))
184+
{
185+
Styling.color(csvRecord.get("Value (dollars)"), "purple");
186+
Styling.line();
187+
}
188+
}
189+
}
190+
catch (Exception e)
191+
{
192+
System.out.println("Error");
193+
}
194+
}
195+
196+
public void exportsByValue (String value)
197+
{
198+
count = 0;
199+
try
200+
{
201+
Styling.lineTitle("Countries whose export value is more than $" + value + " : ");
202+
203+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
204+
{
205+
String valueStr = csvRecord.get("Value (dollars)");
206+
207+
if(valueStr.length() > 0)
208+
{
209+
//removing the commas and $ sign from the value string
210+
valueStr = valueStr.replaceAll(",", "").replace("$", "").replace(" ", "");
211+
212+
//converting the string to long
213+
long valueStrInt = Long.parseLong(valueStr);
214+
215+
//converting the string to long
216+
long valueUser = Long.parseLong(value);
217+
218+
if (valueStrInt > valueUser)
219+
{
220+
Styling.colorTB(csvRecord.get("Country"), "red");
221+
count++;
222+
223+
//newline after every 5 countries
224+
if (count % 5 == 0)
225+
System.out.println();
226+
}
227+
}
228+
}
229+
Styling.line();
230+
}
231+
catch (NumberFormatException e)
232+
{
233+
System.out.println("Error");
234+
}
235+
}
236+
237+
public void printSomeImportantExports ()
238+
{
239+
count = 0;
240+
try
241+
{
242+
String[] impExports = new String[21];
243+
244+
impExports[0] = "crude oil";
245+
impExports[1] = "petroleum";
246+
impExports[2] = "gold";
247+
impExports[3] = "diamonds";
248+
impExports[4] = "copper";
249+
impExports[5] = "iron ore";
250+
impExports[6] = "silver";
251+
impExports[7] = "tin";
252+
impExports[8] = "zinc";
253+
impExports[9] = "lead";
254+
impExports[10] = "nickel";
255+
impExports[11] = "aluminum";
256+
impExports[12] = "uranium";
257+
impExports[13] = "manganese";
258+
impExports[14] = "platinum";
259+
impExports[15] = "cotton";
260+
impExports[16] = "tea";
261+
impExports[17] = "coffee";
262+
impExports[18] = "sugar";
263+
impExports[19] = "cocoa";
264+
impExports[20] = "rubber";
265+
266+
for (String impExport : impExports)
267+
{
268+
Styling.lineTitle("Countries that export " + impExport + " : ");
269+
270+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
271+
{
272+
if (csvRecord.get("Exports").contains(impExport))
273+
{
274+
Styling.colorTB(csvRecord.get("Country"), "purple");
275+
count++;
276+
277+
//newline after every 5 countries
278+
if (count % 5 == 0)
279+
System.out.print(nl);
280+
}
281+
}
282+
}
283+
}
284+
catch (Exception e)
285+
{
286+
System.out.println("Error");
287+
}
288+
}
289+
290+
public void someCalculations(int sizeEx)
291+
{
292+
try
293+
{
294+
//number of countries exporting more than n items
295+
int count = 0;
296+
297+
//total value of exports
298+
long total = 0;
299+
300+
int count2 = 0;
301+
302+
//array of countries that satisfy the condition
303+
String[] countries = new String[250];
304+
305+
for (CSVRecord csvRecord : Objects.requireNonNull(readCSV()))
306+
{
307+
String valueStr = csvRecord.get("Value (dollars)");
308+
String valueEx = csvRecord.get("Exports");
309+
310+
if (valueStr.length() > 0 && valueEx.length() > 0)
311+
{
312+
//removing the commas and $ sign from the value string
313+
valueStr = valueStr.replaceAll(",", "").replace("$", "").replace(" ", "");
314+
315+
//converting the string to long
316+
long valueStrInt = Long.parseLong(valueStr);
317+
318+
//adding the value to the total
319+
total += valueStrInt;
320+
321+
322+
//splitting the export string and putting it in an array
323+
String[] exports = valueEx.split(",");
324+
325+
//checking, if the number of exports is more than the size, as a country export is saved in this string, with each entry being an export item,
326+
//thus if the number of exports in the string is more than the size provide, then the country satisfies the condition, i.e. incrementing the count
327+
if (exports.length > sizeEx)
328+
{
329+
countries[count] = csvRecord.get("Country");
330+
count++;
331+
}
332+
}
333+
}
334+
335+
Styling.lineTitle("Some calculations on complete data: ");
336+
337+
Styling.color("Number of countries exporting more than " +sizeEx+ " items : " + count +nl, "red");
338+
Styling.color("Total value of exports : $" + total + nl, "purple");
339+
340+
//printing the countries exporting more than n items
341+
Styling.color("Name of Countries exporting more than " + sizeEx + " items : "+nl, "red");
342+
343+
for (int i = 0; i < 250; i++)
344+
{
345+
if(countries[i] != null)
346+
{
347+
Styling.colorTB(countries[i], "purple");
348+
count2++;
349+
350+
//newline after every 5 countries
351+
if (count2 % 5 == 0)
352+
System.out.print(nl);
353+
}
354+
}
355+
}
356+
catch (Exception e)
357+
{
358+
System.out.println("Error");
359+
}
360+
}
361+
362+
public static String removeNonDigits(final String str)
363+
{
364+
if (str == null || str.isEmpty())
365+
return str;
366+
367+
StringBuilder sb = new StringBuilder();
368+
for (int i = 0; i < str.length(); i++)
369+
{
370+
char c = str.charAt(i);
371+
if (c >= '0' && c <= '9')
372+
sb.append(c);
373+
}
374+
return sb.toString();
375+
}
376+
}

0 commit comments

Comments
 (0)