Skip to content

Instantly share code, notes, and snippets.

@yaswanthrajyadiki
Created August 19, 2015 03:47
Show Gist options
  • Save yaswanthrajyadiki/78f3f5cba0ea5a2e36e5 to your computer and use it in GitHub Desktop.
Save yaswanthrajyadiki/78f3f5cba0ea5a2e36e5 to your computer and use it in GitHub Desktop.
Library Catalog
/**
* Write a description of class Card here.
* @author (Yaswanth)
* @version (11-08-2015)
*/
public class Card {
/**
* titleOfBook in card catalog.
*/
private String titleOfBook;
/**
* authorOfBook in card catalog.
*/
private String authorOfBook;
/**
* subjectOfBook in card catalog.
*/
private String subjectOfBook;
/**
* Constructor for card class.
* @param titleOfBook1 gets from the menu class
* @param authorOfBook1 gets from the menu class
* @param subjectOfBook1 gets from the menu class
*/
Card(final String titleOfBook1,
final String authorOfBook1, final String subjectOfBook1) {
this.titleOfBook = titleOfBook1;
this.authorOfBook = authorOfBook1;
this.subjectOfBook = subjectOfBook1;
}
/**
* gerTitleOfBook method.
* @return titleOfBook of card catalog
*/
final String getTitleOfBook() {
return this.titleOfBook;
}
/**
* getAuthorOfBook method.
* @return authorOfBook of card Catalog
*/
final String getAuthorOfBook() {
return this.authorOfBook;
}
/**
* getSubjectOfBook method.
* @return subjectOfBook of card catalog
*/
final String getSubject() {
return this.subjectOfBook;
}
}
/**
* Write a description of class CardCatalog here.
* @author (Yaswanth)
* @version (11-08-2015)
*/
public class CardCatalog {
/**
* Index of arrays.
*/
private int index;
/**
* flag is used in insertion of cards.
*/
private int flag = 0;
/**
* size is used in the getauthor method.
*/
private int size;
/**
* Title cards array for titlearray.
*/
private Card[] titleCards;
/**
* Author cards array for authorarray.
*/
private Card[] authorCards;
/**
* Subject cards array for sbject array.
*/
private Card[] subjectCards;
/**
* Constructor for CardCatalog.
* @param siz is array size of cards
*/
CardCatalog(final int siz) {
index = 0;
//this.size = siz;
titleCards = new Card[size];
authorCards = new Card[size];
subjectCards = new Card[size];
}
/**
* addACard method is intialised to pass cards into card array.
* @param card object of card
*/
final void addACard(final Card card) {
if (index == 0) {
titleCards[index] = card;
authorCards[index] = card;
subjectCards[index] = card;
} else {
for (int i = 0; i < index; i++) {
flag = 0;
if (card.getTitleOfBook().compareTo(titleCards[i].getTitleOfBook()) < 0) {
flag = 1;
for (int j = index; j > i; j--) {
titleCards[j] = titleCards[j - 1];
}
titleCards[i] = card;
break;
}
}
if (flag == 0) {
titleCards[index] = card;
}
for (int i = 0; i < index; i++) {
flag = 0;
if (card.getAuthorOfBook().compareTo(authorCards[i].getAuthorOfBook()) < 0) {
flag = 1;
for (int j = index; j > i; j--) {
authorCards[j] = authorCards[ j - 1];
}
authorCards[i] = card;
break;
}
}
if (flag == 0) {
authorCards[index] = card;
}
for (int i = 0; i < index; i++) {
flag = 0;
if (card.getSubject().compareTo(subjectCards[i].getSubject()) < 0) {
flag = 1;
for (int j = index; j > i; j--) {
subjectCards[j] = subjectCards[j - 1];
}
subjectCards[i] = card;
break;
}
}
if (flag == 0) {
subjectCards[index] = card;
}
}
index++;
}
/**
* Remove a title.
* @param title using title of book
*/
final void removeATitle(final String title) {
for (int i = 0; i < index; i++) {
if (title.compareTo(titleCards[i].getTitleOfBook()) == 0) {
for (int ind = i; ind < index - 1; ind++) {
titleCards[ind] = titleCards[ind + 1];
}
}
}
for (int i = 0; i < index; i++) {
if (title.compareTo(authorCards[i].getTitleOfBook()) == 0) {
for (int ind = i; ind < index - 1; ind++) {
authorCards[ind] = authorCards[ind + 1];
}
}
}
for (int i = 0; i < index; i++) {
if (title.compareTo(subjectCards[i].getTitleOfBook()) == 0) {
for (int ind = i; ind < index - 1; ind++) {
subjectCards[ind] = subjectCards[ind + 1];
}
}
}
index--;
}
/**
* Searching first book using title.
* @param titleOfBook using title of Book
* @return first card which has title parameter
*/
final Card getATitle(final String titleOfBook) {
System.out.println("-----Get title of Book-----");
for (int i = 0; i < index; i++) {
if (titleOfBook.compareTo(titleCards[i].getTitleOfBook()) == 0) {
return titleCards[i];
}
}
return null;
}
/**
* Searching books using authorname.
* @param authorOfBook using author of Book
* @return authorCards array which has parmetre author
*/
final Card[] getAnAuthor(final String authorOfBook) {
System.out.println("-----Get author books-----");
Card[] author = new Card[size];
for (int i = 0, k = 0; i < index; i++) {
if (authorOfBook.compareTo(authorCards[i].getAuthorOfBook()) == 0) {
author[k] = authorCards[i];
k++;
}
}
return author;
}
/**
* Searching book using subject getting all the books related to subject.
* @param subjectOfBook which is passed
* @return subjectArray which has parametre subject
*/
final Card[] getSubject(final String subjectOfBook) {
System.out.println("-----Get subject books-----");
Card[] subject = new Card[size];
for (int i = 0, l = 0; i < index; i++) {
if (subjectOfBook.compareTo(subjectCards[i].getSubject()) == 0) {
subject[l] = subjectCards[i];
l++;
}
}
return subject;
}
/**
* Printing all books in sorting order.
*/
final void printTheCatalog() {
//printing according to title
for (int i = 0; i < index; i++) {
System.out.println("---Sort by Title---");
for (i = 0; i < index; i++) {
System.out.print("Title:" + titleCards[i].getTitleOfBook() + " ");
System.out.print("Author:" + titleCards[i].getAuthorOfBook() + " ");
System.out.println("Subject:" + titleCards[i].getSubject());
}
System.out.println("---Sort by Author---");
for (i = 0; i < index; i++) {
System.out.print("Title:" + authorCards[i].getTitleOfBook() + " ");
System.out.print("Author:" + authorCards[i].getAuthorOfBook() + " ");
System.out.println("Subject:" + authorCards[i].getSubject());
}
System.out.println("---Sort by Subject---");
for (i = 0; i < index; i++) {
System.out.print("Title:" + subjectCards[i].getTitleOfBook() + " ");
System.out.print("Author:" + subjectCards[i].getAuthorOfBook() + " ");
System.out.println("Subject:" + subjectCards[i].getSubject());
}
}
}
}
/**
* Write a description of class Menu here.
* @author (yaswanth)
* @version (12-08-2015)
*/
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
Card c1 = new Card("LetUsC" , "Kanetkar" , "Intro to C");
CardCatalog cc1 = new CardCatalog(10);
Card c2 = new Card("ThinkJava" , "AllenB.Downey" , "Intro to C");
Card c3 = new Card("ATwoStates" , "Chetan" , "Lovestory");
Card c4 = new Card("LetUsC++" , "Kanetkar" , "Intro to C++");
cc1.addACard(c1);
cc1.addACard(c2);
cc1.addACard(c3);
cc1.addACard(c4);
cc1.removeATitle("ATwoStates");
cc1.printTheCatalog();
Card c = cc1.getATitle("LetUsC");
System.out.print("Title: "+c.getTitleOfBook()+", ");
System.out.print("Author: "+c.getAuthorOfBook()+", ");
System.out.println("Subject: "+c.getSubject());
//Card[] ca = cc1.getAnAuthor("Kanetkar");
Card[] ca1 = cc1.getAnAuthor("AllenB.Downey");
for(int i=0; ca1[i]!=null; i++) {
System.out.print("Title: "+ca1[i].getTitleOfBook()+", ");
System.out.print("Author: "+ca1[i].getAuthorOfBook()+", ");
System.out.println("Subject: "+ca1[i].getSubject());
}
Card[] cs = cc1.getSubject("Intro to C");
for(int i=0; cs[i]!=null; i++) {
System.out.print("Title: "+cs[i].getTitleOfBook()+", ");
System.out.print("Author: "+cs[i].getAuthorOfBook()+", ");
System.out.println("Subject: "+cs[i].getSubject());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment