Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ArrayListInterface <E> {
- // There are 2 versions of add for arrayList
- public void add (E a); // This method adds at the end of list
- public void add (int index, E a); // This is overloaded method
- // adds at location index
- // Remove method
- public E remove (int index); // remove from index
- // get and set
- public E get (int index); // Returns the object at index
- public void set(int index, E a); // Update the value at index
- public int getSize(); // Returns the number of elements in list
- public int indexOf(E a); // Returns the index of the object
- }
- public class ArrayList <E> implements ArrayListInterface<E> {
- private int size; // How many elements are there in the AL
- private int capacity; // How big is the AL
- private E[] myArray; // This is the reference to actual data
- // Constructors
- // Default constructor, creates an array list of size 2
- public ArrayList() {
- this.capacity = 20;
- this.size = 0; // Initially there is no data
- myArray = (E[]) new Object[this.capacity]; // Create the
- // array with default size
- }
- // Overloaded constructor; creates an AL of user defined size
- public ArrayList(int capacity) {
- this.capacity = capacity;
- this.size = 0; // Initially there is no data
- myArray = (E[]) new Object[this.capacity]; // Create the
- // array with default size
- }
- @Override
- public void add(E a) {
- // This method adds an element to the end of the AL
- if(size < capacity) {
- // This means there is space at the end of the AL
- myArray[size] = a; // size gives the index of first
- // empty location
- size++; // Update the total number of elements in AL
- }
- else {
- // We do not have space to add the element
- System.out.println(" Not enough space! Call reallocate");
- this.reallocate(); // Call reallocate
- this.add(a); // Once we have bigger AL, then add data
- }
- }
- private void reallocate() {
- // This method doubles the capacity of the AL
- this.capacity *= 2;
- E[] temp = (E[]) new Object[this.capacity];
- // Now, we have an empty array double the size
- // Copy over the elements from original array to temp
- for(int i = 0; i < myArray.length;i++) {
- temp[i] = myArray[i];
- }
- // Now update the reference to reflect the change
- this.myArray = temp;
- }
- @Override
- public void add(int index, E a) {
- // This method inserts the data at given index
- // First, check if the index is valid or not
- if(index < 0 || index > size) {
- System.out.println("Invalid index!");
- return;
- }
- else if(index == size) {
- // This indicates that the element needs to be added
- // at the end of the AL
- // Now, we have a method for that!
- this.add(a); // Call the add() method to handle
- }
- else {
- // We will insert the data by shifting elements
- // Is there enough space to shift?
- if(this.capacity == this.size) {
- // AL is already full
- System.out.println("Not enough space! Call reallocate");
- this.reallocate();
- }
- // We have space to shift elements
- for(int i = size; i > index; i--) {
- this.myArray[i] = this.myArray[i -1];
- }
- // Don't forget to insert
- this.myArray[index] = a;
- // Update the total number of elements
- this.size++;
- }
- }
- @Override
- public E remove(int index) {
- // This method deletes an element from the given index
- // Make sure that the index is valid
- if(index < 0 || index >= size) {
- System.out.println("Invalid index!");
- return null;
- }
- // We will save the element to be deleted in a temp
- E temp = myArray[index];
- // Then we need to shift elements to the left
- for(int i = index; i < size -1 ; i++) {
- this.myArray[i] = this.myArray[i+1];
- }
- // Don't forget to update size
- size--;
- return temp;
- }
- @Override
- public E get(int index) {
- if(index < 0 || index >= size) {
- System.out.println("Invalid index!");
- return null;
- }
- return myArray[index];
- }
- @Override
- public void set(int index, E a) {
- if(index < 0 || index >= size) {
- System.out.println("Invalid index!");
- return;
- }
- myArray[index] = a;
- }
- @Override
- public int getSize() {
- return this.size;
- }
- @Override
- public int indexOf(E a) {
- // TODO Auto-generated method stub
- return 0;
- }
- // Method to print the contents of the AL
- // We will override the toString method
- @Override
- public String toString() {
- String s = "";
- for(int i = 0; i < size; i++) {
- s = s + myArray[i];
- }
- return s;
- }
- }
- import java.util.Scanner;
- public class driver {
- public static void main(String[] args) {
- // Here, we create object of AL, and test the methods
- ArrayList<LibraryBooks> myList = new ArrayList<>();
- Scanner myObj = new Scanner(System.in);
- int option = 0;
- System.out.println("Select a number option: ");
- System.out.println("1. List all books");
- System.out.println("2. Display all books sorted by publishing year");
- System.out.println("3. Sort the books according to length in pages");
- System.out.println("4. Sort the books according to review ratings");
- System.out.println("5. Ask user for subject and display all books belonging to that specific subject");
- System.out.println("6. Search for a specific book name and display all info about said book");
- System.out.println("7. Add a book to the list of books");
- System.out.println("8. Exit");
- option = myObj.nextInt();
- while(option != 8) {
- if(option == 1) {
- System.out.println(myList.toString());
- }
- if(option == 2) {
- }
- if(option == 4) {
- }
- if(option == 5) {
- }
- if(option == 7) {
- System.out.println("Please provide details of the new book...");
- LibraryBooks newBook = getDetailsNewBook();
- myList.add(newBook);
- }
- System.out.println("Select an option: ");
- option = myObj.nextInt();
- }
- myObj.close();
- }
- private static LibraryBooks getDetailsNewBook() {
- // TODO Auto-generated method stub
- Scanner input = new Scanner(System.in);
- System.out.println("Enter Title of new Book: ");
- String title = input.nextLine();
- System.out.println("Enter Subject of new book: ");
- String uSub = input.nextLine();
- System.out.println("Enter the year for book: ");
- int uYear = input.nextInt();
- System.out.println("Enter the amount of pages: ");
- int uPage = input.nextInt();
- System.out.println("Enter the review rating: ");
- double uRate = input.nextDouble();
- LibraryBooks lb1 = new LibraryBooks(title, uSub, uYear, uPage, uRate);
- return lb1;
- }
- }
- import java.util.Scanner;
- import java.util.Comparator;
- public class LibraryBooks {
- private String title;
- private String subject;
- private int year;
- private int page;
- private double rating;
- private int titleVal = 21;
- //Default Constructor
- public LibraryBooks () {
- this.title = null;
- this.subject = null;
- this.year = 0;
- this.page = 0;
- this.rating = 0.0;
- }
- //User Inputed Constructor
- public LibraryBooks(String uTitle, String uSub, int uYear, int uPage, double uRate) {
- this.title = uTitle;
- this.subject = uSub;
- this.year = uYear;
- this.page = uPage;
- this.rating = uRate;
- }
- public void setTitle(String newTitle) {
- this.title = newTitle;
- }
- public void setSubject(String newSubject) {
- this.subject = newSubject;
- }
- public void setYear(int newYear) {
- this.year = newYear;
- }
- public void setPage(int newPage) {
- this.page = newPage;
- }
- public void setRating(double newRate) {
- this.rating = newRate;
- }
- public String getTitle() {
- return title;
- }
- public String getSubject() {
- return subject;
- }
- public int getYear() {
- return year;
- }
- public int getPage() {
- return page;
- }
- public double getRating() {
- return rating;
- }
- @Override
- public String toString() {
- return "Title: "+title+ "\nSubject: " + subject + "\nYear Published: " + year + "\nNumber of Pages: " + page + "\nReview Rating: " + rating +"\n\n";
- }
- public static Comparator<LibraryBooks> LibraryPublish = new Comparator<LibraryBooks>() {
- public int compare(LibraryBooks b1, LibraryBooks b2) {
- int publish1 = b1.getYear();
- int publish2 = b2.getYear();
- return publish1 - publish2;
- }
- };
- public static Comparator<LibraryBooks> LibraryPages = new Comparator<LibraryBooks>() {
- public int compare(LibraryBooks b1, LibraryBooks b2) {
- int Page1 = b1.getPage();
- int Page2 = b2.getPage();
- return Page1 - Page2;
- }
- };
- public static Comparator<LibraryBooks> LibraryRating = new Comparator<LibraryBooks>() {
- public int compare(LibraryBooks b1, LibraryBooks b2) {
- if (b1.getRating() < b2.getRating()) return -1;
- if (b1.getRating() > b2.getRating()) return 1;
- return 0;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement