Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class BookStore {
- private Book[] b = new Book[10]; int len = 0;
- public BookStore() {
- b[0] = new Book("Head First Java", "O Railey", "JAVAISBN", 3);
- b[1] = new Book("C Programming", "Herbert Schield", "CPRGRM", 4);
- b[2] = new Book("Linear Algebra", "Kenith Bleh", "MATH183", 5);
- len = 3;
- }
- public void display() {
- System.out.println("\n");
- 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 (len == 0) System.out.println("There are no books to display."); System.out.println("\n\n");
- }
- public void order(String isbn, int nums) {
- for ( int index = 0; index < len && b[index] != null; index++) {
- if (b[index].ISBN.equals(isbn) ) { b[index].numCopies += nums; return;
- }
- }
- 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) {
- for (int index = 0; index < len && b[index] != null; index++) {
- 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);
- 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