Question: I am unable to get this code to work in IntelliJ and I do not know how to fix it! :( Please fix and show
I am unable to get this code to work in IntelliJ and I do not know how to fix it! :( Please fix and show that the what you did actually will run properly. package com.company; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; class DriverClass { public static String Information = ""; public static void writeToFile(ArrayList ar) throws IOException { File output = new File(Information); BufferedWriter bfw = new BufferedWriter(new FileWriter(output)); for (Books books : ar) { bfw.write(books.getTitle() + ' '); bfw.write(books.getAuthor() + " " + books.getIsbn() + " " + books.getAverageRating() + " " + books.getNoOfReviews()); bfw.write(' '); } bfw.close(); } static public void loadFromFile(String str, ArrayList ar) throws FileNotFoundException { Information =str; Scanner scanner = new Scanner(new FileInputStream(new File(str))); while(scanner.hasNextLine()) { String title = scanner.nextLine(); String restOfDetails = scanner.nextLine(); System.out.println(restOfDetails); String[] array = restOfDetails.split(" "); String author = array[0]; String isbn = array[1]; double rating = new Double(array[2]); int numberOfReviews = new Integer(array[3]); Books book = new Books(title, author, isbn, rating, numberOfReviews); ar.add(book); //System.out.println(book); } scanner.close(); } public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); BufferedReader br = null; Books books; ArrayList ar = new ArrayList<>(); // Adding sample Data to the array list // Books b0 = new Books("First Book", "author,one", "123-1234512345", 4.5, 1); // // b0.setAverageRating(4.5); // // Books b1 = new Books("Second Book", "author,two", "456-1234512345", 4.7, 1); // // b1.setAverageRating(4.7); // // Books b2 = new Books("Third Book", "author,three", "789-1234512345", 3.4, 1); // // b2.setAverageRating(3.4); // // ar.add(b0); // // ar.add(b1); // // ar.add(b2); System.out.println("Choose a menu option from the list below: "); System.out.println(" " + "Choose a menu option from the list below: " + " 1: Load book data from a file " + " 2: Add one new book " + " 3: Rate one book " + " 4: Get recommendations " + " 5: Print the full book list " + " 6: Exit the software"); try { int option = scan.nextInt(); while (true) { switch (option) { case 1: System.out.println("Enter the file name(for eg., Books)"); String bookName = scan.next(); loadBookData(bookName, ar); // if (bookName.equals("Books")) { // // loadBookData(bookName, ar); // // } else { // // System.out.println("File for" + bookName + " found!!!!!!!!"); // // } break; case 2: System.out.println("Enter the book title:"); scan.nextLine(); String bookTitle = scan.nextLine(); System.out.println("Enter the author(lastName,firstName with no spaces)"); String author = scan.next(); System.out.println("Enter the 13 digit ISBN: "); String isbnNo = scan.next(); addNewBook(bookTitle, author, isbnNo, ar); break; case 3: System.out.println("Enter the ISBN: "); String isbn = scan.next(); boolean found = false; for (Books book : ar) { if (book.getIsbn().equals(isbn)) { System.out.println("Enter the rating: "); double rating = scan.nextDouble(); rateBook(ar, isbn, rating, book); found = true; break; } } if (found) { System.out.println("The record for " + isbn + " was updated"); } else { System.out.println("The record for " + isbn + " was not found"); } break; case 4: Collections.sort(ar, new SortClass()); for (Books book : ar) { System.out.println(String.format("%1$-18f %2$-10d %3$-10s %4$-10s %5$-10s", book.getAverageRating(), book.getNoOfReviews(), book.getIsbn(), book.getAuthor(), book.getTitle())); } break; case 5: for (Books book : ar) { System.out.println(String.format("%1$-18f %2$-10d %3$-10s %4$-10s %5$-10s", book.getAverageRating(), book.getNoOfReviews(), book.getIsbn(), book.getAuthor(), book.getTitle())); } break; case 6: //When exiting the program write the arraylist of books to the file writeToFile(ar); return; default: System.out.println("please select a proper option!!!"); break; } System.out.println("Enter an option:"); option = scan.nextInt(); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) br.close(); System.out.println("Exiting the program..."); } } static void loadBookData(String bookName, ArrayList ar) throws IOException { // br = new BufferedReader(new FileReader(bookName + ".txt")); // // StringBuilder sb = new StringBuilder(); // // String line = br.readLine(); // // while (line != null) { // // sb.append(line); // // sb.append(System.lineSeparator()); // // line = br.readLine(); // // } // // String everything = sb.toString(); // // System.out.println(everything); loadFromFile(bookName, ar); } static void addNewBook(String title, String author, String isbn, ArrayList ar) { Books books = new Books(); books.setAuthor(author); books.setTitle(title); books.setIsbn(isbn); ar.add(books); } static void rateBook(ArrayList ar, String isbn, double rating, Books book) { int reviews=0; double averageRating=0.0; for (Books books : ar) { if (books.getIsbn().equals(isbn)) { double old = books.getAverageRating() * books.getNoOfReviews(); books.setNoOfReviews(books.getNoOfReviews() + 1); double newRating = (old + rating) / books.getNoOfReviews(); books.setAverageRating(newRating); } } } } class SortClass implements Comparator { @Override public int compare(Books o1, Books o2) { return o1.getRating() == o2.getRating() ? 0 : o1.getRating() < o2.getRating() ? 1 : -1; } } public class Books { private String title; private String author; private String isbn; private double rating; private int noOfReviews; private double averageRating; public Books() { super(); } @Override public String toString() { return "Books [title=" + title + ", author=" + author + ", isbn=" + isbn + ", rating=" + averageRating + ", noOfReviews=" + noOfReviews + "]"; } public Books(String title, String author, String isbn, double rating, int noOfReviews) { super(); this.title = title; this.author = author; this.isbn = isbn; this.rating = rating; this.averageRating = rating; this.noOfReviews = noOfReviews; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public double getRating() { return rating; } public void setRating(double rating) { this.rating = rating; } public int getNoOfReviews() { return noOfReviews; } public void setNoOfReviews(int noOfReviews) { this.noOfReviews = noOfReviews; } public double getAverageRating() { return averageRating; } public void setAverageRating(double averageRating) { this.averageRating = averageRating; } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
