Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- //package bookstoreapp;
- public class Book {
- String bookTitle;
- String Author;
- String ISBN;
- int numOfCopies;
- public Book(String bookTitle,String Author,String ISBN,int numOfCopies){
- this.bookTitle = bookTitle;
- this.Author = Author;
- this.ISBN = ISBN;
- this.numOfCopies = numOfCopies;
- }
- public void display(){
- System.out.println("Book Tilte : "+bookTitle);
- System.out.println("Auhtor : "+Author);
- System.out.println("ISBN: "+ISBN);
- System.out.println("Quantity : "+numOfCopies);
- }
- }
- package com.company;
- //package bookstoreapp;
- import javax.swing.JOptionPane;
- public class BookStore {
- Book[] books = new Book[10];
- {
- books[0] = new Book("Java","Herbert Schieldt","5500",10);
- books[1] = new Book("ANSII C","Balagurusami","6541",5);
- books[2] = new Book("Assembly Language","Alister Cook","1245",15);
- }
- public boolean sell(String bookTitle, int numOfCopies) {
- if (books == null) {
- System.out.println("Book is not found in the store");
- return false;
- }
- for (int i = 0; i < books.length; i++) {
- if (books[i] != null) {
- if (books[i].bookTitle.equals(bookTitle)) {
- if (books[i].numOfCopies < numOfCopies) {
- System.out.println("Not enough book is available in the store");
- } else {
- books[i].numOfCopies = books[i].numOfCopies - numOfCopies;
- JOptionPane.showMessageDialog(null,"Your book has been sold");
- }
- return true;
- }
- }
- }
- System.out.println("Book is not found in the store");
- return false;
- }
- public void order(String ISBN, int numOfCopies) {
- int totalValidNumber = 0;
- for (int i = 0; i < books.length; i++) {
- if (books[i] != null) {
- if (books[i].ISBN.equals(ISBN)) {
- books[i].numOfCopies = books[i].numOfCopies + numOfCopies;
- return;
- }
- totalValidNumber = i + 1;
- }
- }
- System.out.println(totalValidNumber);
- if (totalValidNumber < 10) {
- String bookName = null;
- String bookAuthor = null;
- bookName = JOptionPane.showInputDialog(null, "Enter Book Name: ");
- bookAuthor = JOptionPane.showInputDialog(null, "Enter Author Name: ");
- books[totalValidNumber] = new Book(bookName, bookAuthor,ISBN, numOfCopies);
- }
- }
- public void display() {
- for (int i = 0; i < books.length; i++) {
- if (books[i] != null) {
- books[i].display();
- }
- }
- }
- }
- package com.company;
- import java.util.Scanner;
- class BookStoreApp {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- BookStore books = new BookStore();
- System.out.println("\n---------- Welcome to your Book Store App ----------\n\n");
- while (true) {
- System.out.println("Enter <1> for Displaying all books.");
- System.out.println("Enter <2> to order a new book.");
- System.out.println("Enter <3> for selling a book.");
- System.out.println("Enter <4> for Exiting Out.\n\n");
- System.out.printf("Enter your choice: ");
- int choice;
- choice = scan.nextInt();
- String nameOrIsbn;
- int nums;
- switch (choice)
- {
- case 1:
- books.display(); break;
- case 2:
- System.out.printf("Enter Book's ISBN: "); nameOrIsbn = scan.next();
- System.out.printf("How many? "); nums = scan.nextInt();
- books.order(nameOrIsbn, nums);
- break;
- case 3:
- System.out.printf("Enter Book's Name: "); nameOrIsbn = scan.next();
- System.out.printf("How many? "); nums = scan.nextInt();
- books.sell(nameOrIsbn, nums);
- break;
- case 4:
- System.out.println("Bye Bye!\nSee you later!"); scan.close();
- System.exit(1);
- break;
- default:
- System.out.println("Only numbers between 1 and 4 (inclusive) are accepted.\n\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement