Skip to content

Commit 2951913

Browse files
authored
Create GUI.java
1 parent 2102d50 commit 2951913

File tree

1 file changed

+373
-0
lines changed
  • (1) Java Programming: Solving Problems with Software/Week 3/Programs/(3)ExportsCSV

1 file changed

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

0 commit comments

Comments
 (0)