Question: For this assignment you are asked to create the following classes: abstract class: "item" every i……

USE JAVA
For this assignment you are asked to create the following classes: abstract class: item every item has a unique item_id (a positive integer, auto-incremented for each new item), a price (double), and quantity (integer). Every item can be DisplayedO, an abstract method, and PurchasedO (that is removed from the inventory). concrete class: book a book is-an item. It adds the attributes author, title and year. concrete class: gift a gift is-an item. It adds the attributes label, and manufacturer, Write an application that maintains a list of items entered from the user by means of an interactive
media%2F109%2F109fc566-8f02-4d70-9b9d-32

For this assignment you are asked to create the following classes: abstract class: “item” every item has a unique item_id (a positive integer, auto-incremented for each new item), a price (double), and quantity (integer). Every item can be “DisplayedO”, an abstract method, and “PurchasedO” (that is removed from the inventory). concrete class: “book” a book is-an item. It adds the attributes “author”, title” and “year”. concrete class: “gift” a gift is-an item. It adds the attributes “label”, and “manufacturer”, Write an application that maintains a list of items entered from the user by means of an interactive

Expert Answer

answers

Entire Java Code for the problem above:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created by pchandul on 01-Nov-18.
 */

abstract class item
{
    int id;
    double price;
    int quantity;
    private static int count = 0;
    public item(double price, int qty) {
        this.id = ++count;
        this.price = price;
        this.quantity = qty;
    }

    public abstract void displayItem();
    public abstract void purchaseItem();

}

class book extends item {
    String author;
    String title;
    String year;

    public book(double price, int qty, String author, String title, String year) {
        super(price, qty);
        this.author = author;
        this.title = title;
        this.year = year;
    }

    public static Comparator<book> authorNameComparator = new Comparator<book>() {

        public int compare(book b1, book b2) {
            String author1 = b1.author.toUpperCase();
            String author2 = b2.author.toUpperCase();

            //ascending order
            return author1.compareTo(author2);
        }
    };

    public int getId() {
        return this.id;
    }

    public void displayItem() {
        System.out.println("Book title: " + title + "nAuthor: " + author + "nYear: " + year + "nId: " + id + "nPrice: " + price + "$nQuantity: " + quantity );
        System.out.println("--------------------------------------------");
    }

    public void purchaseItem() {
        System.out.println("Enter the quantity to be purchased");
        Scanner sc = new Scanner(System.in);
        int qty = sc.nextInt();
        if(this.quantity < qty) {
            System.out.println("Sorry we have only " + this.quantity + " of these books left in our inventory");
        } else {
            this.quantity -= qty;
            System.out.println(qty + " of these books purchased successfully!!!");
        }
    }
}

class gift extends item
{
    String label;
    String manufacturer;

    public gift(double price, int qty, String label, String manufacturer) {
        super(price,qty);
        this.label = label;
        this.manufacturer = manufacturer;
    }

    public static Comparator<gift> giftLabelComparator = new Comparator<gift>() {

        public int compare(gift b1, gift b2) {
            String label1 = b1.label.toUpperCase();
            String label2 = b2.label.toUpperCase();

            //ascending order
            return label1.compareTo(label2);
        }
    };

    public int getId() {
        return this.id;
    }

    public void displayItem() {
        System.out.println("Gift label: " + label + "nManufacturer: " + manufacturer + "nId: " + id + "nPrice: " + price + "$nQuantity: " + quantity );
        System.out.println("--------------------------------------------");
    }

    public void purchaseItem() {
        System.out.println("Enter the quantity to be purchased");
        Scanner sc = new Scanner(System.in);
        int qty = sc.nextInt();
        if(this.quantity < qty) {
            System.out.println("Sorry we have only " + this.quantity + " of these gifts left in our inventory");
        } else {
            this.quantity -= qty;
            System.out.println(qty + " of these gifts purchased successfully!!!");
        }
    }
}

public class ItemShopping {
    public static ArrayList<book> bookList;
    public static ArrayList<gift> giftList;

    public static void addBook() {
        int price, qty;
        String title, author, year;
        Scanner sc  = new Scanner(System.in);

        System.out.println("Enter the book title:");
        title = sc.nextLine();

        System.out.println("Enter the book author:");
        author = sc.nextLine();

        System.out.println("Enter the book year:");
        year = sc.nextLine();

        System.out.println("Enter the book price:");
        price = sc.nextInt();

        System.out.println("Enter the book quantity:");
        qty = sc.nextInt();
        bookList.add(new book(price, qty, title, author, year));
    }

    public static void addGift() {
        int price, qty;
        String label,manu;
        Scanner sc  = new Scanner(System.in);

        System.out.println("Enter the gift label:");
        label = sc.nextLine();

        System.out.println("Enter the gift manufacturer:");
        manu = sc.nextLine();

        System.out.println("Enter the gift price:");
        price = sc.nextInt();

        System.out.println("Enter the gift quantity:");
        qty = sc.nextInt();
        giftList.add(new gift(price, qty, label, manu));
    }

    public static void displayAllBooks() {
        for(int i =0; i<bookList.size(); i++) {
            bookList.get(i).displayItem();
        }
    }

    public static void displayAllGifts() {
        for(int i =0; i<giftList.size(); i++) {
            giftList.get(i).displayItem();
        }
    }

    public static void deleteItemById(int id) {
        boolean found  = false;
        for(int i = 0; i<bookList.size(); i++) {
            if(id == bookList.get(i).getId()) {
                found = true;
                bookList.remove(i);
            }
        }
        if(!found) {
            for(int i = 0; i<giftList.size(); i++) {
                if(id == giftList.get(i).getId()) {
                    found = true;
                    giftList.remove(i);
                }
            }
        }
        if(!found) {
            System.out.println("There is no such item with id " + id + " in our inventory");
        }
    }

    public static  void purchaseItemById(int id) {
        boolean found = false;
        for(int i = 0; i<bookList.size(); i++) {
            if(id == bookList.get(i).getId()) {
                found = true;
                bookList.get(i).purchaseItem();
            }
        }
        if(!found) {
            for(int i = 0; i<giftList.size(); i++) {
                if(id == giftList.get(i).getId()) {
                    found = true;
                    giftList.get(i).purchaseItem();
                }
            }
        }
        if(!found) {
            System.out.println("There is no such item with id " + id + " in our inventory");
        }
    }

    public static void main(String[] args) {
        bookList = new ArrayList<book>();
        giftList = new ArrayList<gift>();

        int choice = 0;
        int addCh = 0;
        Scanner sc = new Scanner(System.in);
        while(choice!=9) {
            System.out.println("1. Add item to inventory");
            System.out.println("2. Display all items");
            System.out.println("3. Delete item by id");
            System.out.println("4. Purchase an item");
            System.out.println("5. Display books sorted by Authors");
            System.out.println("6. Display gifts sorted by label");
            System.out.println("9.Exit");
            System.out.println("Please enter your choice");
            choice = sc.nextInt();
            if(choice==1) {
                System.out.println("Press 1 to add a BooknPress 2 to add a Gift");
                addCh = sc.nextInt();
                if(addCh == 1) {
                    addBook();
                } else if (addCh == 2) {
                    addGift();
                } else {
                    System.out.println("Invalid value entered!!");
                }
            } else if(choice == 2) {
                System.out.println("All the items in the inventory:");
                displayAllBooks();
                displayAllGifts();
            } else if(choice == 3) {
                System.out.println("Enter the id of the item to be deleted: ");
                int deleteId = sc.nextInt();
                deleteItemById(deleteId);
            } else if(choice == 4) {
                System.out.println("Enter the id of the item to be purchased: ");
                int purchaseId = sc.nextInt();
                purchaseItemById(purchaseId);
            } else if (choice == 5) {
                Collections.sort(bookList, book.authorNameComparator);
                displayAllBooks();
            } else if (choice == 6) {
                Collections.sort(giftList, gift.giftLabelComparator);
                displayAllGifts();
            } else if(choice !=9) {
                System.out.println("Invalid value entered!! Please try again");
            }
        }
    }
}


Sample output:

1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
1
Press 1 to add a Book
Press 2 to add a Gift
1
Enter the book title:
Pride and prejudice
Enter the book author:
Jane
Enter the book year:
2001
Enter the book price:
150
Enter the book quantity:
20
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
1
Press 1 to add a Book
Press 2 to add a Gift
1
Enter the book title:
All king’s men
Enter the book author:
Adam
Enter the book year:
2004
Enter the book price:
100
Enter the book quantity:
10
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
2
All the items in the inventory:
Book title: Jane
Author: Pride and prejudice
Year: 2001
Id: 1
Price: 150.0$
Quantity: 20
——————————————–
Book title: Adam
Author: All king’s men
Year: 2004
Id: 2
Price: 100.0$
Quantity: 10
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
5
Book title: Adam
Author: All king’s men
Year: 2004
Id: 2
Price: 100.0$
Quantity: 10
——————————————–
Book title: Jane
Author: Pride and prejudice
Year: 2001
Id: 1
Price: 150.0$
Quantity: 20
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
2
All the items in the inventory:
Book title: Adam
Author: All king’s men
Year: 2004
Id: 2
Price: 100.0$
Quantity: 10
——————————————–
Book title: Jane
Author: Pride and prejudice
Year: 2001
Id: 1
Price: 150.0$
Quantity: 20
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
1
Press 1 to add a Book
Press 2 to add a Gift
2
Enter the gift label:
Barbie doll
Enter the gift manufacturer:
Barbie Manufacturers Ltd
Enter the gift price:
45
Enter the gift quantity:
50
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
1
Press 1 to add a Book
Press 2 to add a Gift
2
Enter the gift label:
Frame
Enter the gift manufacturer:
Pixel Ltd Co
Enter the gift price:
60
Enter the gift quantity:
20
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
6
Gift label: Barbie doll
Manufacturer: Barbie Manufacturers Ltd
Id: 3
Price: 45.0$
Quantity: 50
——————————————–
Gift label: Frame
Manufacturer: Pixel Ltd Co
Id: 4
Price: 60.0$
Quantity: 20
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
2
All the items in the inventory:
Book title: Adam
Author: All king’s men
Year: 2004
Id: 2
Price: 100.0$
Quantity: 10
——————————————–
Book title: Jane
Author: Pride and prejudice
Year: 2001
Id: 1
Price: 150.0$
Quantity: 20
——————————————–
Gift label: Barbie doll
Manufacturer: Barbie Manufacturers Ltd
Id: 3
Price: 45.0$
Quantity: 50
——————————————–
Gift label: Frame
Manufacturer: Pixel Ltd Co
Id: 4
Price: 60.0$
Quantity: 20
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
3
Enter the id of the item to be deleted:
3
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
2
All the items in the inventory:
Book title: Adam
Author: All king’s men
Year: 2004
Id: 2
Price: 100.0$
Quantity: 10
——————————————–
Book title: Jane
Author: Pride and prejudice
Year: 2001
Id: 1
Price: 150.0$
Quantity: 20
——————————————–
Gift label: Frame
Manufacturer: Pixel Ltd Co
Id: 4
Price: 60.0$
Quantity: 20
——————————————–
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
4
Enter the id of the item to be purchased:
1
Enter the quantity to be purchased
10
10 of these books purchased successfully!!!
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
4
Enter the id of the item to be purchased:
3
There is no such item with id 3 in our inventory
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
4
Enter the id of the item to be purchased:
4
Enter the quantity to be purchased
25
Sorry we have only 20 of these gifts left in our inventory
1. Add item to inventory
2. Display all items
3. Delete item by id
4. Purchase an item
5. Display books sorted by Authors
6. Display gifts sorted by label
9.Exit
Please enter your choice
9

Don't use plagiarized sources. Get Your Custom Essay on
Question: For this assignment you are asked to create the following classes: abstract class: "item" every i……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

Process finished with exit code 0

Top Grade Homework
Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents