import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class LoadingData
{
List <Book> Alist = new ArrayList<>();
public void loading(String fileName, Class<?> theClass)
{
Constructor<Book>[] ctors = (Constructor<Book>[]) theClass.getConstructors();
Constructor<Book> ctor = ctors[0];
try (BufferedReader br = new BufferedReader(new FileReader(fileName)))
{
String currentLine;
boolean first = true;
while ((currentLine = br.readLine()) != null)
{
Object[] details = currentLine.split(",");
if (first)
{
first = false;
continue;
}
Book instance = ctor.newInstance((Object[]) details);
Alist.add(0,instance);
}
}
catch (IllegalAccessException |
IllegalArgumentException |
InstantiationException |
InvocationTargetException |
IOException ioe) {
}
}
public void insertionSort(List<Object> array)
{
int i, j;
for (i=1; i < array.size();i++ )
{
String current = (String) array.get(i);
j=1;
while(j>0 && current.compareTo((String) array.get(-1))<0)
{
array.set(j, array.get(j-1));
j--;
}
array.set(j, current);
}
System.out.println(array);
}
public static void main(String[] args)
{
LoadingData Data = new LoadingData();
Data.loading("C:\\Users\\User\\Documents\\NetBeansProjects\\sorting\\src\\sorting\\books.csv", Book.class);
System.out.println("Before sort");
System.out.println(Data.Alist);
System.out.println("After sort");
insertionSort(Data.Alist);
}
}
public class Book
{
String bookID;
String title;
String authors;
String average_rating;
String isbn;
String isbn13;
String language_code;
String num_pages;
String ratings_count;
String text_reviews_count;
String publication_date;
String publisher;
public String getBookID() {
return bookID;
}
public void setBookID(String bookID) {
this.bookID = bookID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthors() {
return authors;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public String getAverage_rating() {
return average_rating;
}
public void setAverage_rating(String average_rating) {
this.average_rating = average_rating;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getIsbn13() {
return isbn13;
}
public void setIsbn13(String isbn13) {
this.isbn13 = isbn13;
}
public String getLanguage_code() {
return language_code;
}
public void setLanguage_code(String language_code) {
this.language_code = language_code;
}
public String getNum_pages() {
return num_pages;
}
public void setNum_pages(String num_pages) {
this.num_pages = num_pages;
}
public String getRatings_count() {
return ratings_count;
}
public void setRatings_count(String ratings_count) {
this.ratings_count = ratings_count;
}
public String getText_reviews_count() {
return text_reviews_count;
}
public void setText_reviews_count(String text_reviews_count) {
this.text_reviews_count = text_reviews_count;
}
public String getPublication_date() {
return publication_date;
}
public void setPublication_date(String publication_date) {
this.publication_date = publication_date;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Book (String bookID, String title, String authors, String average_rating, String isbn, String isbn13, String language_code, String num_pages, String ratings_count, String text_reviews_count, String publication_date, String publisher )
{
this.bookID = bookID;
this.title = title;
this.authors = authors;
this.average_rating = average_rating;
this.isbn = isbn;
this.isbn13 = isbn13;
this.language_code = language_code;
this.num_pages = num_pages;
this.ratings_count = ratings_count;
this.text_reviews_count = text_reviews_count;
this.publication_date = publication_date;
this.publisher = publisher;
}
@Override
public String toString()
{
return String.format(" (%s) %s %s %s %s %s %s %s %s %s %s %s \n", bookID, title, authors,average_rating, isbn, isbn13, language_code, num_pages, ratings_count, text_reviews_count, publication_date, publisher );
}
}
What I have tried:
i tried to sort this this list of object in insertion sort and it keeps telling me that i have to change methods. can someone help me run this code and tell me what im still doing wrong here