-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVParserAssignment.java
More file actions
138 lines (125 loc) · 5.4 KB
/
CSVParserAssignment.java
File metadata and controls
138 lines (125 loc) · 5.4 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
import edu.duke.*;
import org.apache.commons.csv.*;
public class CSVParserAssignment {
public void tester() {
FileResource fr = new FileResource();
CSVParser parser = fr.getCSVParser();
// Part 2
countryInfo(parser, "Germany");
// Part 3
listExportersTwoProducts(parser, "gold", "diamonds");
// Part 4
int numberOfExportersResult = numberOfExporters(parser, "gold");
System.out.println("Number of exporters: " + numberOfExportersResult);
// Part 5
bigExporters(parser, "$999,999,999");
}
public void countryInfo(CSVParser parser, String country) {
boolean found = false;
for (CSVRecord record : parser) {
String currentCountry = record.get("Country");
if (currentCountry.equals(country)) {
found = true;
String exports = record.get("Exports");
String value = record.get("Value (dollars)");
System.out.println(currentCountry + ": " + exports + ": " + value);
break;
}
}
if (!found) {
System.out.println("NOT FOUND");
}
}
public void listExportersTwoProducts(CSVParser parser, String exportItem1, String exportItem2) {
int count = 0;
for (CSVRecord record : parser) {
String exports = record.get("Exports");
if (exports.contains(exportItem1) && exports.contains(exportItem2)) {
count++;
if (count == 3) {
String country = record.get("Country");
System.out.println("Third country exporting both " + exportItem1 + " and " + exportItem2 + ": " + country);
break;
}
}
}
}
public int numberOfExporters(CSVParser parser, String exportItem) {
int count = 0;
for (CSVRecord record : parser) {
String exports = record.get("Exports");
if (exports.contains(exportItem)) {
count++;
}
}
return count;
}
public void bigExporters(CSVParser parser, String amount) {
// Remove dollar sign and commas from the amount string
amount = amount.replace("$", "").replace(",", "");
for (CSVRecord record : parser) {
String value = record.get("Value (dollars)").replace("$", "").replace(",", "");
// Compare numeric values
if (value.length() > amount.length()) {
String country = record.get("Country");
System.out.println(country + " " + record.get("Value (dollars)"));
}
}
}
public String getNthExporter(CSVParser parser, int n, String exportItem1, String exportItem2) {
int count = 0;
for (CSVRecord record : parser) {
String exports = record.get("Exports");
if (exports.contains(exportItem1) && exports.contains(exportItem2)) {
count++;
if (count == n) {
return record.get("Country");
}
}
}
return "Not Found";
}
public int getNumberOfExporters(CSVParser parser, String exportItem) {
int count = 0;
for (CSVRecord record : parser) {
String exports = record.get("Exports");
if (exports.contains(exportItem)) {
count++;
}
}
return count;
}
public String getNthBigExporter(CSVParser parser, int n, String amount) {
// Remove dollar sign and commas from the amount string
amount = amount.replace("$", "").replace(",", "");
int count = 0;
for (CSVRecord record : parser) {
String value = record.get("Value (dollars)").replace("$", "").replace(",", "");
// Compare numeric values
if (value.length() > amount.length()) {
count++;
if (count == n) {
return record.get("Country");
}
}
}
return "Not Found";
}
public static void main(String[] args) {
CSVParserAssignment csvParserAssignment = new CSVParserAssignment();
// Run your program on the file exportdata.csv
FileResource fr = new FileResource("exportdata.csv");
CSVParser parser = fr.getCSVParser();
// Question 1: What is the name of the country that is listed as the second country that exports both cotton and flowers?
String secondCountry = csvParserAssignment.getNthExporter(parser, 2, "cotton", "flowers");
System.out.println("Second country exporting both cotton and flowers: " + secondCountry);
// Question 2: How many countries export cocoa?
parser = fr.getCSVParser(); // Reset parser
int numberOfCocoaExporters = csvParserAssignment.getNumberOfExporters(parser, "cocoa");
System.out.println("Number of countries exporting cocoa: " + numberOfCocoaExporters);
// Question 3: What is the name of the third country (on the third line of the output) listed whose exports are valued at one trillion US dollars or more?
parser = fr.getCSVParser(); // Reset parser
String thirdTrillionDollarCountry = csvParserAssignment.getNthBigExporter(parser, 3, "$999,999,999,999");
System.out.println("Third country with exports valued at one trillion or more: " + thirdTrillionDollarCountry);
}
}