Advertisement
NB52053

BookStoreApp

Jul 23rd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. package com.company;
  2. //package bookstoreapp;
  3.  
  4. public class Book {
  5.     String bookTitle;
  6.     String Author;
  7.     String ISBN;
  8.     int numOfCopies;
  9.  
  10.     public Book(String bookTitle,String Author,String ISBN,int numOfCopies){
  11.  
  12.         this.bookTitle = bookTitle;
  13.         this.Author = Author;
  14.         this.ISBN = ISBN;
  15.         this.numOfCopies = numOfCopies;
  16.     }
  17.     public void display(){
  18.         System.out.println("Book Tilte : "+bookTitle);
  19.         System.out.println("Auhtor : "+Author);
  20.         System.out.println("ISBN: "+ISBN);
  21.         System.out.println("Quantity : "+numOfCopies);
  22.     }
  23. }
  24.  
  25. package com.company;
  26.  
  27. //package bookstoreapp;
  28.  
  29.         import javax.swing.JOptionPane;
  30.  
  31. public class BookStore {
  32.  
  33.     Book[] books = new Book[10];
  34.  
  35.     {
  36.         books[0] = new Book("Java","Herbert Schieldt","5500",10);
  37.         books[1] = new Book("ANSII C","Balagurusami","6541",5);
  38.         books[2] = new Book("Assembly Language","Alister Cook","1245",15);
  39.     }
  40.  
  41.     public boolean sell(String bookTitle, int numOfCopies) {
  42.  
  43.         if (books == null) {
  44.             System.out.println("Book is not found in the store");
  45.             return false;
  46.         }
  47.         for (int i = 0; i < books.length; i++) {
  48.  
  49.             if (books[i] != null) {
  50.                 if (books[i].bookTitle.equals(bookTitle)) {
  51.                     if (books[i].numOfCopies < numOfCopies) {
  52.                         System.out.println("Not enough book is available in the store");
  53.                     } else {
  54.                         books[i].numOfCopies = books[i].numOfCopies - numOfCopies;
  55.                         JOptionPane.showMessageDialog(null,"Your book has been sold");
  56.                     }
  57.  
  58.                     return true;
  59.                 }
  60.             }
  61.  
  62.         }
  63.         System.out.println("Book is not found in the store");
  64.         return false;
  65.  
  66.     }
  67.  
  68.     public void order(String ISBN, int numOfCopies) {
  69.  
  70.         int totalValidNumber = 0;
  71.         for (int i = 0; i < books.length; i++) {
  72.             if (books[i] != null) {
  73.                 if (books[i].ISBN.equals(ISBN)) {
  74.                     books[i].numOfCopies = books[i].numOfCopies + numOfCopies;
  75.                     return;
  76.                 }
  77.                 totalValidNumber = i + 1;
  78.             }
  79.  
  80.         }
  81.  
  82.         System.out.println(totalValidNumber);
  83.         if (totalValidNumber < 10) {
  84.             String bookName = null;
  85.             String bookAuthor = null;
  86.             bookName = JOptionPane.showInputDialog(null, "Enter Book Name:  ");
  87.             bookAuthor = JOptionPane.showInputDialog(null, "Enter Author Name:  ");
  88.             books[totalValidNumber] = new Book(bookName, bookAuthor,ISBN, numOfCopies);
  89.         }
  90.  
  91.     }
  92.  
  93.     public void display() {
  94.  
  95.         for (int i = 0; i < books.length; i++) {
  96.             if (books[i] != null) {
  97.  
  98.                 books[i].display();
  99.             }
  100.  
  101.         }
  102.  
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. package com.company;
  109. import java.util.Scanner;
  110.  
  111. class BookStoreApp {
  112.     public static void main(String[] args) {
  113.         Scanner scan = new Scanner(System.in);
  114.  
  115.  
  116.         BookStore books = new BookStore();
  117.  
  118.         System.out.println("\n---------- Welcome to your Book Store App ----------\n\n");
  119.  
  120.         while (true) {
  121.  
  122.             System.out.println("Enter <1> for Displaying all books.");
  123.             System.out.println("Enter <2> to order a new book.");
  124.             System.out.println("Enter <3> for selling a book.");
  125.             System.out.println("Enter <4> for Exiting Out.\n\n");
  126.             System.out.printf("Enter your choice: ");
  127.  
  128.             int choice;
  129.             choice = scan.nextInt();
  130.  
  131.             String nameOrIsbn;
  132.             int nums;
  133.  
  134.             switch (choice)
  135.             {
  136.                 case 1:
  137.                     books.display(); break;
  138.                 case 2:
  139.                     System.out.printf("Enter Book's ISBN: ");  nameOrIsbn = scan.next();
  140.                     System.out.printf("How many? ");  nums = scan.nextInt();
  141.                     books.order(nameOrIsbn, nums);
  142.                     break;
  143.                 case 3:
  144.                     System.out.printf("Enter Book's Name: ");  nameOrIsbn = scan.next();
  145.                     System.out.printf("How many? ");  nums = scan.nextInt();
  146.                     books.sell(nameOrIsbn, nums);
  147.                     break;
  148.                 case 4:
  149.                     System.out.println("Bye Bye!\nSee you later!");  scan.close();
  150.                     System.exit(1);
  151.                     break;
  152.  
  153.                 default:
  154.                     System.out.println("Only numbers between 1 and 4 (inclusive) are accepted.\n\n");
  155.             }
  156.         }
  157.  
  158.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement