Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Book {
- public String bookTitle = "Jaava";
- public String author = "idk";
- public String ISBN = "idk";
- public int numCopies = 0;
- public Book(String name, String auth, String isbn, int x) {
- bookTitle = name;
- author = auth;
- ISBN = isbn;
- numCopies = x;
- }
- }
- -----------------------------------------------------------------------------------------------
- import java.util.Scanner;
- class BookStore {
- private Book[] b = new Book[10];
- int len = 0;
- public BookStore() {
- b = new Book[10];
- b[0] = new Book("Java", "JAuth", "JIsbn", 3);
- b[1] = new Book("C", "c-auth", "c-isbn", 4);
- b[2] = new Book("Python", "PyAuth", "PyIsbn", 5);
- len = 3;
- }
- public void display() {
- System.out.println("\n");
- // find the total num of books in array and print one by one
- for (int i = 0; i < 10 && b[i] != null; i++) {
- System.out.printf("%d %s\t%s\t%s\t%d\n", i+1, b[i].bookTitle,
- b[i].author, b[i].ISBN, b[i].numCopies);
- }
- // if there are no books
- if (len == 0) System.out.println("There are no books to display.");
- System.out.println("\n\n");
- }
- public void order(String isbn, int nums) {
- int index = 0;
- for (; index < len && b[index] != null; index++) {
- // if the book exists in the book shop
- if (b[index].ISBN.equals(isbn) ) {
- b[index].numCopies += nums;
- // GET OUT OF METHOD
- return;
- }
- }
- // if the method came here, it means the book does not exist
- Scanner scan = new Scanner(System.in);
- System.out.println("New Book Name: ");
- String name = scan.nextLine();
- System.out.println("New Book's Author: ");
- String auth = scan.nextLine();
- b[index] = new Book(name, auth, isbn, nums);
- len++;
- }
- public void sell(String name, int nums) {
- int index = 0;
- for (; index < len && b[index] != null; index++) {
- // if the book exists in the book shop
- if (b[index].bookTitle.equals(name) ) {
- if (b[index].numCopies >= nums) {
- b[index].numCopies -= nums;
- System.out.printf("You have just sold %d copies of %s\n\n", nums, name);
- System.out.printf("You now have %d of %s left.\n\n", b[index].numCopies, b[index].bookTitle);
- // GET OUT OF METHOD
- return;
- }
- }
- }
- System.out.println("The book is not available now... Do check back another time!");
- }
- }
- -------------------------------------------------------------------------------------------------------------
- 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