Reputation: 829
I have 3 csv files as follows:
I need to display:
I am using BufferedReader
as of now:
String csvFileToRead = "csvFiles/authors.csv";
BufferedReader br = null;
String line = "";
String splitBy = ";";
try {
br = new BufferedReader(new FileReader(csvFileToRead));
while ((line = br.readLine()) != null) {
String[] book = line.split(splitBy);
System.out.println("BOOKS [Email Address= " + book[0] + " , firstname="
+ book[1] + " , lastname=" + book[2] + "]");
}
}
I am confused about how to proceed with multiple files. Here are the approaches that i have considered:
Change
String csvFileToRead
to a String array
String[] csvFileToRead = {"data/authors.csv", "data/books.csv", "data/magazines.csv"};
Then pass each index each time to a method returning all rows but getting stuck with DS to use. I think ArrayList
won't suffice since i need separated data.
Should I make 3 different classes with getters and setters for authors, book and magazine?
What is the ideal way to do this? Please suggest.
Upvotes: 0
Views: 5737
Reputation: 72844
One simple solution is to create three classes corresponding to your entities:
public class Author {
private String email;
private String firstName;
private String lastName;
// ...
}
public class Book {
private String title;
private String isbn;
private String authorEmail;
private String description;
// ...
}
public class Magazine {
private String title;
private String isbn;
private String authorEmail;
private Date releaseDate;
// ...
}
In your main code, when reading your CSV files you can fill a HashMap
that maps an ISBN to a book, and a similar one for magazines, which you can use to retrieve a book or magazine by ISBN:
Map<String, Book> isbnBookMap = new HashMap<String, Book>();
Book book = isbnBookMap.get(isbn);
Map<String, Magazime> isbnMagazineMap = new HashMap<String, Magazime>();
Magazime magazine = isbnMagazineMap.get(isbn);
Similarly for getting a book or magazine by title.
As for getting books by author, you would need to map the author mail address to a List
of Books since an author can have multiple books:
Map<String, List<Book>> authorBookMap = new HashMap<String, List<Book>>();
List<Book> books = authorBookMap.get(authorAddress);
Upvotes: 1
Reputation: 2136
I would create 3 List<String[]>
each containing the matrix from one CSV file with a method like:
public List<string[]> csvToList(String csvFileToRead){
BufferedReader br = null;
String line = "";
String splitBy = ";";
try {
br = new BufferedReader(new FileReader(csvFileToRead));
List<string[]> list_csv = new ArrayList<string[]>();
while ((line = br.readLine()) != null) {
String[] book = line.split(splitBy);
authors.add(book);
}
catch(Exception ex) {}
return list_csv;
}
And after that you can use it as :
List<String[]> authors = csvToList("csvFiles/authors.csv");
List<String[]> books = csvToList("csvFiles/books.csv");
List<String[]> magazine = csvToList("csvFiles/magazine.csv");
You can now print all what you want, for example the first case : Based on the ISBN display all books and magazines :
public void printISBN(string ISBN){
for(String[] s_tab in books)
if(s_tab[1].equals(ISBN))
System.out.println(s_tab[0]);
for(String[] s_tab in magazine)
if(s_tab[1].equals(ISBN))
System.out.println(s_tab[0]);
}
Hope I helped.
Edit
You could also create 3 classes for each files and do the same with Lists of thoses classes : List<Authors> List<Books> List<Magazine>
but it is not necessary if you just have to answer those 3 questions.
Upvotes: 1