Skip to content

Instantly share code, notes, and snippets.

@LunaticWolf
Created June 25, 2018 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LunaticWolf/a4b35951cef5ca1c73168e298b06ef21 to your computer and use it in GitHub Desktop.
Save LunaticWolf/a4b35951cef5ca1c73168e298b06ef21 to your computer and use it in GitHub Desktop.
Book Store
import java.util.*;
class Book {
private String bookTitle;
private String bookAuthor;
private String bookISBN;
private int bookCopies;
Book() {
this("null", "null", "null", 0);
}
Book(String bookTitle, String bookAuthor, String bookISBN, int bookCopies) {
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookISBN = bookISBN;
this.bookCopies = bookCopies;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookISBN() {
return bookISBN;
}
public void setBookISBN(String bookISBN) {
this.bookISBN = bookISBN;
}
public int getBookCopies() {
return bookCopies;
}
public void setBookCopies(int bookCopies) {
this.bookCopies = bookCopies;
}
public void display() {
System.out.println("Book title: " + getBookTitle());
System.out.println("Book Author: " + getBookAuthor());
System.out.println("Book ISBN value: " + getBookISBN());
System.out.println("Number of Copies : " + getBookCopies());
}
}
class BooksStore {
//max no. of books are 10;
private Book[] books = new Book[10];
// -----------------------------------------------------------------------------------------------------
public void sell(String Title, int copies) {
for (int i = 0; i <= books.length; i++) {
if (books[i] != null && books[i].getBookTitle().equals(Title)) {
if(books[i].getBookCopies() >= copies)
books[i].setBookCopies(books[i].getBookCopies() - copies);
else{
System.err.println("No. of copies are more than present");
return;
}
return;
} else {
System.out.println("\n\nBook Not Found!!");
break;
}
}
}
// -------------------------------------------------------------------------------------
public void order(String ISBN, int Copies) {
boolean found = false;
int i;
for (i = 0; i < books.length; i++) {
if (books[i] != null && books[i].getBookISBN().equals(ISBN)) {
books[i].setBookCopies(books[i].getBookCopies() + Copies);
// display();
found = true;
break;
}
else if(books[i] == null){
Scanner scanner = new Scanner(System.in);
System.out.println("\n Enter Book Title - ");
String Title = scanner.next();
System.out.println("\n Enter Author's Name - ");
String Author = scanner.next();
System.out.println("\n Enter ISBN value -");
String ISBN1 = scanner.next();
System.out.println("\n Enter Number of Copies - ");
int Copies1 = scanner.nextInt();
books[i] = new Book(Title, Author, ISBN1, Copies1);
break;
}
if(i == books.length) System.err.println("No books to be added");
}
}
// --------------------------------------------------------------------------------------------
public void display() {
if(books[0] == null){
System.err.println("NO books found");
return;
}
for (int j = 0; j < books.length; j++) {
if(books[j] != null) books[j].display();
}
}
}
public class BookStoreApp {
public static void main(String[] args) {
BooksStore book = new BooksStore();
//book.initialize();
String choice2;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("\nEnter Option: \n" + " Press 1: To Display all the Books \n"
+ "Press 2: To Order more Books\n " + " Press 3 : To Sell Books \n");
int choice = scanner.nextInt();
switch (choice) {
case 1:
book.display();
break;
case 2:
book.order("abc", 100);
break;
case 3:
System.out.println("\nEnter the Book Title: ");
String title = scanner.next();
System.out.println("\nEnter the no. of copies: ");
int copies = scanner.nextInt();
book.sell(title, copies);
System.out.println("\n\nThank You! Please do Visit us Again... ");
break;
case 4:
System.exit(0);
break;
default:
System.out.println("\n ---------------- Enter a valid option -----------------");
}
System.out.println("\n\nWant to continue ??\n Press 'Y' to continue");
choice2 = scanner.next();
} while (choice2.equals("y"));
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment