Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.util.*; //scanner and ArrayList
  2.  
  3. public class ALBook {
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. //variables declared at the top
  7. Book book=null;
  8. Book bk = null;
  9. int isbn=0;
  10. String title="";
  11. String yn= "";
  12. ArrayList al = new ArrayList( );
  13.  
  14. System.out.println("The size of the array list is "+ al.size( ));
  15. do{
  16. try{
  17.  
  18. System.out.print("Enter the ISBN: ");
  19. isbn = sc.nextInt( );
  20. sc = new Scanner(System.in);
  21. System.out.print("Enter the Title: ");
  22. title = sc.nextLine( );
  23. book = new Book (isbn, title);
  24. al.add(book);
  25. sc = new Scanner(System.in);
  26. System.out.println("another? y/n");
  27. yn = sc.nextLine( );
  28.  
  29. }//closes try
  30. catch(InputMismatchException ime){
  31. System.out.println("You did not enter a number");
  32. System.out.println("This program will end now");
  33. }
  34. catch(BookException be){
  35. System.out.println(be.getMessage( ));
  36. }
  37.  
  38. }while((yn.equalsIgnoreCase("Y")));
  39.  
  40. System.out.println("- - -Printing all books - - -");
  41. for (int i=0; i<al.size(); i++){
  42. bk = (Book) al.get(i);
  43. System.out.println(bk.toString( ));
  44. System.out.println(al.get(i));
  45. }
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement