Advertisement
NB52053

JAVA LAB 3

Jun 12th, 2017
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. class Book {
  2.     public String bookTitle = "Jaava";
  3.     public String author = "idk";
  4.     public String ISBN = "idk";
  5.     public int numCopies = 0;
  6.  
  7.     public Book(String name, String auth, String isbn, int x) {
  8.         bookTitle = name;
  9.         author = auth;
  10.         ISBN = isbn;
  11.         numCopies = x;
  12.     }
  13. }
  14.  
  15. -----------------------------------------------------------------------------------------------
  16.  
  17. import java.util.Scanner;
  18.  
  19. class BookStore {
  20.     private Book[] b = new Book[10];
  21.     int len = 0;
  22.  
  23.     public BookStore() {
  24.  
  25.         b = new Book[10];
  26.         b[0] = new Book("Java", "JAuth", "JIsbn", 3);
  27.         b[1] = new Book("C", "c-auth", "c-isbn", 4);
  28.         b[2] = new Book("Python", "PyAuth", "PyIsbn", 5);
  29.  
  30.                                                  len = 3;
  31.  
  32.     }
  33.  
  34.     public void display() {
  35.  
  36.         System.out.println("\n");
  37.  
  38.         // find the total num of books in array and print one by one
  39.  
  40.         for (int i = 0; i < 10 && b[i] != null; i++) {
  41.             System.out.printf("%d %s\t%s\t%s\t%d\n", i+1, b[i].bookTitle,
  42.                               b[i].author, b[i].ISBN, b[i].numCopies);
  43.         }
  44.  
  45.         // if there are no books
  46.         if (len == 0)  System.out.println("There are no books to display.");
  47.  
  48.         System.out.println("\n\n");
  49.     }
  50.  
  51.     public void order(String isbn, int nums) {
  52.         int index = 0;
  53.         for (; index < len && b[index] != null; index++) {
  54.             // if the book exists in the book shop
  55.             if (b[index].ISBN.equals(isbn) ) {
  56.                 b[index].numCopies += nums;
  57.  
  58.                 // GET OUT OF METHOD
  59.                 return;
  60.             }
  61.         }
  62.  
  63.         // if the method came here, it means the book does not exist
  64.          Scanner scan = new Scanner(System.in);
  65.  
  66.          System.out.println("New Book Name: ");
  67.          String name = scan.nextLine();
  68.  
  69.          System.out.println("New Book's Author: ");
  70.          String auth = scan.nextLine();
  71.  
  72.  
  73.         b[index] = new Book(name, auth, isbn, nums);
  74.                                               len++;
  75.     }
  76.  
  77.     public void sell(String name, int nums) {
  78.         int index = 0;
  79.         for (; index < len && b[index] != null; index++) {
  80.  
  81.             // if the book exists in the book shop
  82.             if (b[index].bookTitle.equals(name) ) {
  83.  
  84.                 if (b[index].numCopies >= nums) {
  85.  
  86.                     b[index].numCopies -= nums;
  87.  
  88.                     System.out.printf("You have just sold %d copies of %s\n\n", nums, name);
  89.                     System.out.printf("You now have %d of %s left.\n\n", b[index].numCopies, b[index].bookTitle);
  90.  
  91.                     // GET OUT OF METHOD
  92.                     return;
  93.                 }
  94.             }
  95.         }
  96.         System.out.println("The book is not available now... Do check back another time!");
  97.     }
  98.  
  99. }
  100. -------------------------------------------------------------------------------------------------------------
  101.  
  102. import java.util.Scanner;
  103.  
  104. class BookStoreApp {
  105.     public static void main(String[] args) {
  106.         Scanner scan = new Scanner(System.in);
  107.  
  108.  
  109.         BookStore books = new BookStore();
  110.  
  111.         System.out.println("\n---------- Welcome to your Book Store App ----------\n\n");
  112.  
  113.         while (true) {
  114.  
  115.             System.out.println("Enter <1> for Displaying all books.");
  116.             System.out.println("Enter <2> to order a new book.");
  117.             System.out.println("Enter <3> for selling a book.");
  118.             System.out.println("Enter <4> for Exiting Out.\n\n");
  119.             System.out.printf("Enter your choice: ");
  120.  
  121.             int choice;
  122.             choice = scan.nextInt();
  123.  
  124.             String nameOrIsbn;
  125.             int nums;
  126.  
  127.             switch (choice)
  128.             {
  129.                 case 1:
  130.                     books.display(); break;
  131.                 case 2:
  132.                     System.out.printf("Enter Book's ISBN: ");  nameOrIsbn = scan.next();
  133.                     System.out.printf("How many? ");  nums = scan.nextInt();
  134.                     books.order(nameOrIsbn, nums);
  135.                     break;
  136.                 case 3:
  137.                     System.out.printf("Enter Book's Name: ");  nameOrIsbn = scan.next();
  138.                     System.out.printf("How many? ");  nums = scan.nextInt();
  139.                     books.sell(nameOrIsbn, nums);
  140.                     break;
  141.                 case 4:
  142.                     System.out.println("Bye Bye!\nSee you later!");  scan.close();
  143.                     System.exit(1);
  144.                     break;
  145.  
  146.                 default:
  147.                     System.out.println("Only numbers between 1 and 4 (inclusive) are accepted.\n\n");
  148.             }
  149.         }
  150.  
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement