Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Simple inventory System. Sometimes it doesn't update my added book.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple inventory System. Sometimes it doesn't update my added book.

    import java.util.*;
       public class S10105810_Assignment
       {
          public static void main(String[] args)
          {
             int[] code = new int[20];			// array for codes
             String[] title = new String[20];	// array for booktitles
             double[] price = new double[20];	// array for price of books
             int[] qty = new int[20];			// array for quantity of books
             int count = 0;	
             int choice= 1;                   
             int number =0; 
             int search=0;                   // search when there user enter 1
             int idex=-1;                     // return if search cannot be found
             int key;				              // To enter the book that user want to find
    			int serialnum=1;                //  To indicate the s/no of the book that is out of stock. Serial num always at 1 
     
             Scanner input = new Scanner(System.in);
             while(choice != 0)                       // when choice is not 0 to exit. 
             {
                System.out.println("MENU " + "\n" + "====");
                System.out.println("1) Display All Books" + "\n2) Display books that are out of stock" + "\n3)Add new book"+ "\n4)Update Quantity of a book"+ "\n5)Display Lowest Quantity"+ "\n6)Search a book"+ "\n7)Remove a Book"+ "\n0)Exit");
                System.out.print("Enter your option : " );
                choice = input.nextInt();
     
                if(choice==1)
                {
                   System.out.println("Option 1. Display all books");
                   if(count<=6)
                   {
                      if(number>0)
                      {
                       System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
                       for(int i=1;i<count+1;i++)
                       {
                        System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
                       }
    					    if(idex!=-1)
    						 {
    						  RemovedBook(code,title,price,qty,count,number);
    						 }
                      }
                      else
                      {
    						 if(idex==-1)
    						 {
                        count = initialization(code,title,price,qty);
                        System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
                          for(int i=1;i<count+1;i++)
                        {
                         System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
                        }
    						 }
    						 if(idex!=-1)
    						 {
    						  RemovedBook(code,title,price,qty,count,number);
    						 }
                      }
     
                   }
                   if(count>6)
                   {
                      if(number>0)
                      {
                         System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
                         for(int i=1;i<count+1;i++)
                         {
                            System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
                         }
    							if(idex!=-1)
    							{
    							RemovedBook(code,title,price,qty,count,number);
    							}
                      }
                      else
                      {
                         System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
                         for(int i=1;i<count+1;i++)
                         {
                            System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
                         }
    							if(idex!=-1)
    							{
    							RemovedBook(code,title,price,qty,count,number);
    							}
                      }
                   }
     
                }
                if(choice==2)
                {
                  OutOfStock(code,title,price,qty,count,serialnum);
                }
                if(choice==3)
                {
                   count=NewBook(code,title,price,qty,count);
                }
                if(choice==4)
                {
                   number=updateQuantity(code,title,price,qty,count,number);
                }
                if(choice==5)
                {
                   lowestQuantity(code,title,price,qty,count);
                }
     
                if(choice==6)
                {
    				   System.out.println("Option 6. Searching for a book");
                   System.out.print("Enter 1 if you wish to proceed with searching for a particular book(0 to skip): ");
                   search= input.nextInt();
                   while(search==1)
                   {
                      System.out.print("Enter the book you want to search for: ");
                      key= input.nextInt();
                      idex = searchingforBook(code,key,count);
                      if(idex == -1)
                      {
                         System.out.println("No such book Exist");
                      }
                      else
                      {
    						   System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
    							System.out.println(idex + "\t" + code[idex-1] + "\t" + price[idex-1] + "\t" + qty[idex-1] + "\t" +"\t"+ title[idex-1]);
                      }
                      System.out.print("Do you want to continue searching books? 1 to continue, 0 to end: ");
                      search=input.nextInt();
                   }	 	
                }
                if(choice==7)
                {
    				   System.out.println("Option 7. Removing a book");
                   System.out.print("Enter 1 if you wish to proceed with removing a particular book(0 to skip): ");
                   search= input.nextInt();
                   while(search==1)
                   {
                      System.out.print("Enter the book you want to remove: ");
                      key= input.nextInt();
                      idex = removingBook(code,key,count);
                      if(idex == -1)
                      {
                         System.out.println("No such book");
                      }
                      else
                      {
    			            code[idex-1]=0;
    			            qty[idex-1]=0;
    			             title[idex-1]="";
    			             price[idex-1]=0;
    			             System.out.println("Book of Serial no."+(idex)+" is Removed.");
     
                      }
                      System.out.print("If Continue removing books? Enter 1 to continue or 0 to End Removing book: ");
                      search=input.nextInt();
                   }	 	
                }
     
             }				
          }
     
          public static int initialization(int[] code, String[] title, double[] price, int[] qty)
          {
             code[0] = 9877;
             title[0] = "Awaken The Giant Within";
             price[0] = 19.75;
             qty[0] = 6;
             code[1] = 9965;
             title[1] = "Being Happy!";
             price[1] = 17.33;
             qty[1] = 9;
             code[2] = 9126;
             title[2] = "Chicken Soup For The Teenage Soul";
             price[2] = 21.54;
             qty[2] = 0;
             code[3] = 9429;
             title[3] = "Dealing with People You Can't Stand";
             price[3] = 24.03;
             qty[3] = 3;
             code[4] = 9101;
             title[4] = "Emotional Intelligence";
             price[4] = 16.33;
             qty[4] = 11;
             code[5] = 9222;
             title[5] = "Follow Your Heart";
             price[5] = 21.09;
             qty[5] = 15;
             return 6;
          }
     
          public static void OutOfStock (int[] code, String[] title, double[] price, int[] qty, int count, int serialnum)
          {
             System.out.println("Option 2.Display books out of stock");
             System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
             for(int i=1; i<count; i++)
             {
                if(qty[i-1]==0)
                {
                   System.out.println(serialnum + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
                }
             }
          }
          public static int NewBook (int[] code, String[] title, double[] price, int[] qty,int count)
          {
             int num=0;
             Scanner input = new Scanner(System.in);
             System.out.print("Enter 1 to enter book or 0 to exit adding book menu:");
             num = input.nextInt();
             while(num==1)
             {
                System.out.println("Option 3. Add new book");
                System.out.print("Enter a new code: ");
                code[count] = input.nextInt();
                input.nextLine();
                System.out.print("Enter a new title: ");
                title[count] = input.nextLine();
                System.out.print("Enter a new price: ");
                price[count] = input.nextDouble();
                System.out.print("Enter a new quantity: ");
                qty[count] = input.nextInt();
                count++;
                System.out.println("One book added");
     
                System.out.print("Do you want to continue? 1 for Yes, 0 for no");
                num = input.nextInt();
             }
             return count;
          }
          public static int updateQuantity (int[] code, String[] title, double[] price, int[] qty,int count, int num2)
          {
             System.out.println("Option 4. Update quantity");
             int quantity=0;
             Scanner input = new Scanner(System.in);
             System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
             for(int i=1;i<count+1;i++)
             {
              System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
             }
             System.out.print("Enter the Serial Number of the book to update the quantity: ");
             num2=input.nextInt();
             System.out.print("Enter quantity to increase or decrease: ");
             quantity=input.nextInt();
             qty[num2-1] = qty[num2-1] + quantity;
             System.out.println("Quantity updated");
     
             return num2;
          }
          public static void lowestQuantity (int[] code, String[] title, double[] price, int[] qty,int count)
          {
    		   System.out.println("Option 5. Lowest quantity");
             int lowestquantity = qty[0];
             String bookTitle="";
             for (int i=1;i<count+1;i++)
             {
                if (qty[i]<lowestquantity)
                {
                   lowestquantity=qty[i];
                   bookTitle=title[i];
                }
             }
             System.out.println("Lowest quantity book is "+bookTitle+" at "+lowestquantity);
          }
          public static int searchingforBook (int[] code,int searchKey,int count)
          {
             for(int i=1;i<count+1;i++)
             { 
                if(code[i]==searchKey)
                {
                   return i+1;
                }
             }
             return -1;
          }
          public static int removingBook (int[] code,int searchKey,int count)
          {
             for(int i=1;i<count+1;i++)
             { 
                if(code[i]==searchKey)
                {
                   return i+1;
                }
             }
             return -1;
          }
    		public static void RemovedBook(int[] code, String[] title, double[] price, int[] qty,int count,int num2)
    		{
    	   System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
           for(int i=1;i<count+1;i++)
           {
            System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
           }
    		}
     
       }






    Firstly, whenever you add a book, it should always display the serial number as well as the code,title,price and quantity. However, it doesn't show up sometimes when i try to add a book. The book isn't added into the array. I tried to debug, but i can't seem to find the problem. The debugger isn't showing any problems. Moreover, if i try to add a book with price=0 and quantity =0, it doesn't show up too.
    Last edited by JustinK; July 26th, 2011 at 09:16 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    That is WAY too much code for us to wade through. Please read the link in my signature on asking questions the smart way. Code you post should be in the form of an SSCCE, and you should very clearly state what's happening and what you expected to happen. Have you run through this with a debugger, or with a piece of paper and a pencil? Where does the code's behavior differ from what you thought it would do?

    You forgot the highlight tags. I added them for you this time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    it doesn't show up sometimes
    I don't understand how it "sometimes" does not work.
    Computer programs do the same thing every time. Its the input that is different.
    Can you copy and paste here the console from when you execute the program.
    Add comments to show where it is NOT displaying.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    When i try to compile it, it works.

    the only problem i encountered are.... i have to type in option 1 , option 2, , option 3 then, when i update my the quantity (in option4) it will display all the book.

    if i start from option 3, then the option 4 will only display the added book i entered in option 3, it does not display all the books.

    why?

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    When i try to compile it, it works.

    the only problem i encountered are.... i have to type in option 1 , option 2, , option 3 then, when i update my the quantity (in option4) it will display all the book.

    if i start from option 3, then the option 4 will only display the added book i entered in option 3, it does not display all the books.

    why?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    Can you copy and paste here the console from when you execute the program.
    Add comments to show where it is NOT displaying.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    //////////////
    Attached Images Attached Images
    Last edited by kerrina; August 1st, 2011 at 11:14 AM.

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    cccccccccccccccccccc . thanks
    Last edited by kerrina; August 1st, 2011 at 11:15 AM.

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    cccccccccccccccccccccccccccc. thank you
    Last edited by kerrina; August 1st, 2011 at 11:16 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Simple inventory System. Sometimes it doesn't update my added book.

    You need to do some debugging to find out where in your code you are not updating or setting the count variable correctly. Add a println after EVERY place that you change the value of the count variable. One of those changes must be wrong for the value to not be correct.

Similar Threads

  1. Structure of simple POS system
    By jrflynn in forum Java Theory & Questions
    Replies: 13
    Last Post: July 9th, 2012, 06:32 AM
  2. Image is not added when converting eml into htm
    By Vjay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2011, 06:24 AM
  3. Image is not added when converting eml into htm
    By Vjay in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 1st, 2011, 09:23 AM
  4. Simple Banking System
    By ShadowKing98 in forum Java Theory & Questions
    Replies: 7
    Last Post: April 12th, 2011, 04:26 AM
  5. Replies: 8
    Last Post: December 9th, 2009, 03:45 PM