Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Library {
- private Book[] arrOfBooks;
- int numOfDiffBooks = 0;
- final int sizeOfLibrary;
- Scanner input = new Scanner(System.in);
- public Library(int size)
- {
- sizeOfLibrary = size;
- arrOfBooks = new Book[size];
- }
- public boolean putBook(Book someBook)
- {
- for (int i = numOfDiffBooks-1; i>=0;i--)
- {
- if (someBook.equals (arrOfBooks[i]))
- {
- arrOfBooks[i].setCopys(arrOfBooks[i].getCopys()+1);
- return true;
- }
- }
- if (numOfDiffBooks + 1 <= sizeOfLibrary )
- {
- numOfDiffBooks++;
- arrOfBooks[numOfDiffBooks-1] = someBook;
- return true;
- }
- return false;
- }
- public int numberOfDifBooks()
- {
- return numOfDiffBooks;
- }
- public int numOfCopies(Book someBook)
- {
- for (Book Books : arrOfBooks)
- {
- if (someBook.equals(Books))
- return Books.getCopys();
- }
- return 0 ;
- }
- public int numOfBooks()
- {
- int sum = 0;
- for (int i = 0; i<numOfDiffBooks;i++)
- sum+= arrOfBooks[i].getCopys();
- return sum;
- }
- }
- import java.lang.String;
- public class Book {
- private String nameOfBook;
- private int copys;
- public Book(String title)
- {
- nameOfBook = title;
- copys = 1;
- }
- public void setName(String name)
- {
- nameOfBook = name;
- }
- public String getTitle()
- {
- return nameOfBook;
- }
- public boolean equals(Book other)
- {
- if (nameOfBook.equals(other.getTitle()))
- return true;
- else return false;
- }
- public int getCopys()
- {
- return copys;
- }
- public void setCopys(int num)
- {
- copys = num;
- }
- }
- import java.util.Scanner;
- public class LibTest {
- public static void main(String[] args) {
- final int size;
- int tmp,tmpCpy;
- Library lib = null;
- Book someBook = null;
- System.out.println("Enter library size: ");
- Scanner input = new Scanner(System.in);
- size = input.nextInt();
- lib = new Library(size);
- //Menu with options (put book,checking library size, and number of copies for a specific book
- do {
- System.out.println("\nWhat would you like to do next?\n\n1 - Put book\n2 - Show the amount of diffrent books the library can hold\n3 - Show number of copies for a specific book\n4 - Total number of books in the library\n5 - Exit");
- tmp = input.nextInt();
- input.nextLine();
- switch(tmp) {
- case 1:
- System.out.println("Enter book name: ");
- someBook = new Book(input.nextLine());
- if (lib.putBook(someBook)) {
- System.out.println("The book '"+someBook.getTitle()+"' was entered!");
- }
- else {
- System.out.println("Library is full!");
- }
- break;
- case 2:
- System.out.println("The library can hold a maximum of " + lib.sizeOfLibrary + " different books");
- break;
- case 3:
- System.out.println("Enter book name: ");
- someBook= new Book(input.nextLine());
- tmpCpy = lib.numOfCopies(someBook);
- if (tmpCpy>0) {
- System.out.println("The library has " + tmpCpy + " copies of the book: " + someBook.getTitle());
- }
- else {
- System.out.println("Book doesn't exist");
- }
- break;
- case 4:
- System.out.println("The total is: " + lib.numOfBooks());
- break;
- case 5:
- System.out.println("Good bye!");
- input.close();
- return;
- default:
- System.out.println("Not valid, try again.");
- }
- }while(tmp != 0);
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement