ublic class Book
{
String author, area, isbn;
int length;
public Book(String isbn, String author, String area, int length)
{
this.isbn = isbn;
this.author = author;
this.area = area;
this.length = length;
}
public boolean isLong()
{
if(length > 600)
return true;
else
return false;
}
public String getAuthor()
{
return author;
}
public String getArea()
{
return area;
}
public int getLength()
{
return length;
}
public String toString()
{
String s;
return s = "[BOOK ISBN: " +isbn + ", AUTHOR: " + author + "AREA: " + area + "PAGES: " + length + "]";
}
public String mathCS()
{
if (area.equals("MATH"))
return "MATH";
if (area.equals("CS"))
return "CS";
else
return "Other";
}
}
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class BookCollection
{
private ArrayList<Book> bookList = new ArrayList<Book>();
public void BookCollecion() throws IOException
{
Scanner scan;
Scanner fileScan = new Scanner(new File("Books.txt"));
String aLine, isbn, author, area;
int length;
Book aBook;
while(fileScan.hasNext())
{
aLine = fileScan.nextLine();
scan = new Scanner(aLine);
isbn = scan.next();
author = scan.next();
area = scan.next();
length = scan.nextInt();
aBook = new Book(isbn, author, area, length);
bookList.add(aBook);
}
}
public void displayLongBooks()
{
System.out.println("LONG BOOKS");
for(int i = 0; i<bookList.size(); i++)
{
if(bookList.get(i).isLong())
{
bookList.get(i).toString();
}
}
}
public void displayAverageLength()
{
double avg = 0;
for(int i = 0; i < bookList.size(); i++)
{
avg += bookList.get(i).getLength();
}
System.out.println("AVERAGE LENGTH\n");
System.out.println(avg/bookList.size());
}
public void displayMathCSBooks()
{
System.out.println("");
System.out.println("MATH and COMPUTER SCIENCE BOOKS");
for(int i = 0; i < bookList.size(); i++)
{
if(bookList.get(i).mathCS().equals("MATH") || bookList.get(i).mathCS().equals("CS"))
bookList.get(i).toString();
}
}
public void displayBooksFromAuthor(String author){
int a = 0;
System.out.println(author + "'s BOOKS");
for(int i = 0; i < bookList.size(); i++)
{
if (bookList.get(i).getAuthor().equals(author)){
System.out.println (bookList.get(i));
a++;}
}
if (a == 0)
System.out.println("There are no books authored by " + author + ".");
}
public void displayBooksFromArea (String area)
//displays all books from given area (in analogous way)
{
for(int i = 0; i < bookList.size(); i++)
{
if (bookList.get(i).getArea().equals(area))
bookList.get(i);
}
}
public void displayOtherBooks()
{
System.out.println("");
System.out.println("OTHER BOOKS");
for(int i = 0; i < bookList.size(); i++)
{
if(bookList.get(i).mathCS().equals("Other"))
bookList.get(i).toString();
}
}
public String toString()
{
String book = "ALL BOOKS\n";
for(int i = 0; i<bookList.size(); i++)
book += bookList.get(i) + "\n";
return book;
}
}
import java.io.*;
import java.util.Scanner;
public class TestBookCollection {
public static void main(String args[]) throws IOException
{
Scanner input = new Scanner(System.in);
BookCollection test = new BookCollection();
test.toString();
test.displayLongBooks();
System.out.println();
System.out.print("Enter author name: ");
String author = input.nextLine();
System.out.println();
test.displayBooksFromAuthor(author);
System.out.println();
System.out.print("Enter another author name: ");
author = input.nextLine();
System.out.println();
test.displayBooksFromAuthor(author);
//test.displayBooksFromArea(area);
test.displayAverageLength();
test.displayMathCSBooks();
test.displayOtherBooks();
}
}