Gunasekhar
Posts: 7
Nickname: dguna
Registered: Nov, 2004
|
|
Re: Adding an object as an array
|
Posted: Dec 13, 2004 10:06 PM
|
|
Hi May be you can also try this code for adding & displaying:
import java.io.*; import java.util.*; public class BookStore { static InputStreamReader isr = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(isr); static Vector arrayBook = new Vector(); public static void main (String [] arg) throws Exception { final int iSize = 200; int choice = 5; //Book book = new Book (iISBN, sTitle, iQuantity); //arrayBook.add(book); do { System.out.println("Select your menu option: "); System.out.println("1. Add Book"); System.out.println("2. Remove Book"); System.out.println("3. Search Book"); System.out.println("4. Show All"); System.out.println("5. Exit"); System.out.println("Choice[1,2,3,4,5]: "); choice = Integer.parseInt(br.readLine()); switch(choice) { case 1: addBook(); break; case 2: //removeBook(); break; case 3: //searchBook(); break; case 4: showBooks(); break; case 5: break; default: System.out.println("Invalid Entry!! Please try again ..."); } }while(choice!=5); }//close main public static void addBook() throws Exception { int iISBN = 0, iQuantity = 0; String iTitle = ""; //getting the ISBN number boolean bISBN = false; while(!bISBN) { System.out.print("Enter the ISBN number: "); String sISBN = br.readLine(); sISBN = sISBN.trim(); try { int iOtherISBN = Integer.parseInt(sISBN); if(iOtherISBN >= 0 && iOtherISBN <= 199) { iISBN = iOtherISBN; bISBN = true; }//end if }//end try catch (NumberFormatException nfe) { System.out.println("Number not within 0-199, back to the menu."); bISBN = false; }//end catch }//end while //getting the Title boolean bTitle = false; while(!bTitle) { System.out.print("Enter your Title: "); String sOtherTitle = br.readLine(); sOtherTitle = sOtherTitle.trim(); if(sOtherTitle.length() !=0) { iTitle = sOtherTitle; bTitle = true; }//end if else { System.out.println("Your title is blank, go back to the menu"); bTitle = false; }//end else }//end while //getting the quantity boolean bQty = false; while(!bQty) { System.out.print("Quantity of books: "); String sQty = br.readLine(); sQty = sQty.trim(); try { int iOtherQuantity = Integer.parseInt(sQty); if (iOtherQuantity >=0) { iQuantity = iOtherQuantity; bQty = true; }//end if else { System.out.println("Your number is negavtive, go back to menu."); bQty = false; }//end else }//end try catch (NumberFormatException nfe) { System.out.println("You did not enter a number, back to the menu you go."); bQty = false; }//end catch }//end while //if all entries are available... if(bISBN && bTitle & bQty) { int flag = 1; for(int i=0;i<arrayBook.size();i++) { Book b = (Book)arrayBook.elementAt(i); if(b.sTitle.equals(iTitle)) { flag = 0; break; } } if(flag == 1) { Book b = new Book(iISBN,iTitle,iQuantity); arrayBook.add(b); } else { System.out.println("Book exists in there, cannot add, try again."); } }
}//close addBook
public static void showBooks() { for(int i=0;i < arrayBook.size(); i++) { Book b = (Book)arrayBook.elementAt(i); System.out.println("Details of Book" + (i+1) + "..."); System.out.println("iISBN: " + b.iISBN); System.out.println("Title: " + b.sTitle); System.out.println("Quantity: " + b.iQuantity); } } }//close class class Book { String sTitle; int iISBN; int iQuantity; Book(int iISBN, String sTitle, int iQuantity) { this.iISBN = iISBN; this.sTitle = sTitle; this.iQuantity = iQuantity; } }
|
|