JAVA program to maintain a BOOK SHOP

Does any one get a doubt how a librarian maintains the list of books available in the library?Today we shall get in detail about this and compile a program to maintain a book shop.First of all we need to know what are the inputs,methods do be coded in the program.
1. INPUTS:
Title of the book.Author of the book are taken from the user who requires the book.
2. Variables Need to be stored:
Author,Title,Publisher,Cost,number of books available.These all are the attributes of the book which is stored.
3. Methods:
In this program we need three methods.One for the input of the list of all the books present in the library.Two,search method to check whether the required book is available or not.Third,display function to display the search output.
Explanation:We shall now go in detail about these methods.
1. In the first method we shall enter details of the books available in the library…like title of the book,author of the book,publisher of the book,price of the book,number of books present in the library.
2. The second method asks the user to enter the title,author of the book they required.
3. The third method is used to display the output of the search result.

We shall now write the code from the above explanation:

import java.util.*;
class bookshopmethods
{
     Scanner in = new Scanner(System.in);
     int n=2;
     String[] title = new String[n];
     String[] author = new String[n];
     String[] publisher = new String[n];
     int[] cost = new int[n];
     int[] count = new int[n];
     void setdata()
      {
        for(int i=0;i < n;i++)
         {
            System.out.println("Enter the title of the " + (i+1) +" book:");
            title[i] = in.nextLine();
            System.out.println("Enter the author of the " + (i+1) +" book:");
            author[i] = in.nextLine();
            System.out.println("Enter the publisher of the " + (i+1) +" book:");
            publisher[i] = in.nextLine();
            System.out.println("Enter the cost of the " + (i+1) + " book:");
            cost[i]=in.nextInt();
            System.out.println("Enter the number of books present in the Book shop:");
            count[i]=in.nextInt();
            in.nextLine();
         }//end of for loop
      }//end of setdata function
      void search()
      {
        int i;
        System.out.println("***********Books present in the Book shop are*************"); 
        for(i=0;i<n;i++)
         {
           System.out.println((i+1) + ". Title of the book:" +title[i]);
          }
        System.out.println("Enter the title of the book needed:");
        String titkey;
        titkey = in.nextLine(); 
        int flag=0;
        for(i=0;i<n;i++)
        {
           
           if(titkey.equals(title[i]))
             {
               flag++;
               break;
             }// end of if condition
         }// end of for loop
         if(flag != 0)
         {
              System.out.println("******Details of the book you needed is*****");
              System.out.println("Title of the book:"+title[i]);
              System.out.println("Author of the book:"+author[i]);
              System.out.println("Publisher of the book:"+publisher[i]);
              System.out.println("Cost of the book:"+cost[i]);
              System.out.println("Number of books presernt in the Book shop:"+count[i]);
         }//end of if condition
        else
             System.out.println("Book not present in the store");
   }//end of search function
}//end of class
class bookshop
{
  public static void main(String arg[])
    {
          int ch;
          bookshopmethods ob = new bookshopmethods();
          Scanner in = new Scanner(System.in);
          while(true)
            {
              System.out.println("********************MENU IS****************");
              System.out.println("\t\t1.To enter the data\n\t\t2.To search a book\n\t\t3.Exit"); 

             System.out.println("Enter your choice from the above menu:");
              ch = in.nextInt();
              switch(ch)
               {
                  case 1: ob.setdata();
                              break;
                  case 2: ob.search();
                           break;
                  case 3: System.exit(0);
               }//end of switch case
             }// end of while loop
 }//end of main function
}// end of class

Output:
book_shop_1
book_shop_2
You can download the source code:download

Advertisements

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in Java and tagged , , , , , , , , . Bookmark the permalink.

One Response to JAVA program to maintain a BOOK SHOP

  1. syed mateen says:

    thank u very much sir, this is very useful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s