-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCsvDemo.java
92 lines (74 loc) · 3.38 KB
/
SimpleCsvDemo.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
88
89
90
91
92
package org.example;
import org.example.data.CustomerData;
import org.example.data.Customers;
import org.example.util.CsvReaderWriter;
import java.io.*;
import java.util.Scanner;
import static org.example.util.CustomCsvUtil.parseCustomersFromCsv;
import static org.example.util.CustomCsvUtil.writeCustomers;
/**
* A simple demo class showcasing the working of the {@link CsvReaderWriter} class.
* It reads the data from a CSV file, transforms each row into an instance of the
* {@link CustomerData} class and prints the list of instances.
*/
public class SimpleCsvDemo {
public static void main(String[] args) throws Exception {
File customerDataFile = null;
Scanner scanner = new Scanner(System.in);
boolean validPath = false;
/*
* Step 1. Get the input file path from the user
*/
while (!validPath) {
// Prompt the user to enter a file path
System.out.println("Please enter the file path:");
String customerFilePath = scanner.nextLine();
// Create a File object with the provided path
customerDataFile = new File(customerFilePath.trim());
// Check if the file exists and is a file
if (customerDataFile.exists() && customerDataFile.isFile()) {
validPath = true;
System.out.println("File path: " + customerDataFile.getAbsolutePath());
} else {
System.out.println("The provided path is not a valid file. Please try again.");
}
}
/*
* Step 2: Parse the input file path from the user and get the CustomerData instances
*/
Customers parsedCustomers = parseCustomersFromCsv(customerDataFile);
System.out.println(parsedCustomers.getHeaders());
parsedCustomers.getCustomerCollection().forEach(System.out::println);
/*
* Step 3: Get the output file path from the user
*/
File outputCsvFile = null;
validPath = false;
while (!validPath) {
// Prompt the user to enter a file path
System.out.println("Please enter the file path to save the CSV file:");
String outputFilePath = scanner.nextLine();
// Create a File object with the provided path
outputCsvFile = new File(outputFilePath.trim());
// Check if the file exists and is a file
if (outputCsvFile.exists() && outputCsvFile.isFile()) {
System.out.println("The provided path already exists. Please give a different path.");
} else {
File parentDir = outputCsvFile.getParentFile();
if (parentDir != null && parentDir.exists()) {
// Check if the parent directory is writable
if (!parentDir.canWrite()) {
System.out.println("The parent directory of the given path is not writable. Please give a different path.");
} else {
validPath = true;
System.out.println("Output file path: " + outputCsvFile.getAbsolutePath());
}
}
}
}
/*
* Step 4: Write the customer data to the path given by the user.
*/
writeCustomers(outputCsvFile, parsedCustomers.getHeaders(), parsedCustomers.getCustomerCollection());
}
}