Q:

java OOP EXAM question Wharehouse, Items

0

Implement the following class in Java:

Item - id: int - itemCount: int - name: String - UPC: int + Item() + Item (String name, int UPC) + getID(): int + getName(): String + getUPC(): int + setName(String type): void + setUPC(int UPC): void  This class implements items that can be stored in a warehouse. Every item has an ID that is assigned by the class in an orderly fashion (1, 2, 3, 4, …).  Item’s name can contain any string assigned by the user (e.g. Dell computer, MS mouse, etc.)  UPC is a unique identification number (for example: 4011200296908). Attributes: id An auto incremental ID where the first item has the id=1 itemCount A static variable that holds the number of created objects Name Name of the item UPC A unique identification number Methods: Item() A default constructor Item (String name, int UPC) A constructor that takes the name and UPC of an item. It should assign an id to each item, and increments the itemCount everytime getID(): int An accessor for the attribute id getName(): String An accessor for the attribute name getUPC(): int An accessor for the attribute UPC setName(String type): void A setter for attribute name setUPC(int UPC): void A setter for attribute UPC

 

 

Implement the following class in Java: (13 Marks) Warehouse - items: Item[] - nOfItems: int + Warehouse(int maxSize) + addItem(String name, int UPC): void + deleteItem(int UPC): void + searchItem(int UPC): int + sort(): void + printItemsInfo(): void + getNumberOfItems(): int + isFull(): boolean + isEmpty(): boolean Attributes: items An array of the object Item (from the previous Question) nOfItems The number of the items in the array. Methods: Warehouse(int maxSize) A constructor that accepts the max number of items in the warehouse + addItem(String name, int UPC): void To add a new item. If the item’s UPC exists in the system, the method should not add the item + deleteItem(int UPC): void To delete an item from the warehouse using its given UPC + searchItem(int UPC): int To search for an item using its UPC. The method should return the index of the item in the array, and -1 if not found. + sort(): void To sort the items in an ascending order (smallest to the largest) using their UPC + printItemsInfo(): void To print all items information in the system. For each, it should print: id, name, and UPC. Then it should print the number of items in the warehouse. + getNumberOfItems(): int To return the number of items in the warehouse + isFull(): boolean Return true if the array of items is full + isEmpty(): boolean Return true if the array of items is empty

 

Using the previous implemented classes, implement a main program that does the following tasks (assume that the max size of items=100): (5 Marks) 1) Add the following items to the warehouse Name UPC HP computer 122 Desk 100 Chair 15 Tablet 200 Pen 100 2) Sort the items based on their UPC number in an ascending order 3) Print all items information 4) Delete the item which has (UPC=100) 5) Print all items information

 

Using the previously mentioned main method that you have implemented, write in the box the output expected from your main method.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class Main
{
	public static void main(String[] args) {
	    //question1
	    Warehouse wh=new Warehouse(100);
	    wh.addItem("Hp computer",122);
	    wh.addItem("Desk",100);
	    wh.addItem("Chair",15);
	    wh.addItem("Tablet",200);
	    wh.addItem("Pen",100);
	    
	    //question2
	    wh.sort();
	    
	    //question3
	    wh.printItemsInfo();
	    
	    //question4
	    wh.deleteItem(100);
	    
	    //question5
	    wh.printItemsInfo();
	    
	    //question6
	    /*
	    the output is:
	    warehouse items:
	    ------------------
	    id: 1, name: Hp computer, UPC:122
	    id: 2, name: Desk, UPC:100
	    id: 3, name: Chair, UPC:15
	    id: 4, name: Tablet, UPC:200
	    
	    item 100 deleted
	    warehouse items:
	    ------------------
	    id: 1, name: Hp computer, UPC:122
	    id: null, name: null, UPC:null
	    id: 3, name: Chair, UPC:15
	    id: 4, name: Tablet, UPC:200
	    */
	}
}

class Item{
    private int id;
    private static int itemCount=0;
    private String name;
    private int UPC;
    
    public Item()
    {
        Item.itemCount++;
        this.id=itemCount;
    }
    public Item(String name, int UPC)
    {
        this.name=name;
        this.UPC=UPC;
        Item.itemCount++;
        this.id=itemCount;
    }
    public int getID()
    {
        return id;
    }
    public String getName()
    {
        return name;
    }
    public int getUPC()
    {
        return UPC;
    }
    public void setName(String type)
    {
        name=type;
    }
    public void setUPC(int UPC)
    {
        this.UPC=UPC;
    }
}
class Warehouse{
    private Item[] items;
    private int nOfItems;
    public Warehouse(int maxSize)
    {
        items=new Item[maxSize];
        nOfItems=0;
    }
    public void addItem(String name, int UPC)
    {
        for(int i=0;i<nOfItems;i++)
        {
            if(items[i].getUPC()==UPC)
            {
                System.out.println("this item is exists already");
                return;
            }
        }
        Item it=new Item(name,UPC);
        items[nOfItems]=it;
        nOfItems++;
        
    }
    public void deleteItem(int UPC)
    {
    for(int i=0;i<nOfItems;i++)
        {
            if(items[i].getUPC()==UPC)
            {
                items[i]=null;
                break;
            }
        }
    }
    
    public int searchItem(int UPC)
    {
    for(int i=0;i<items.length;i++)
        {
            if(items[i].getUPC()==UPC)
            {
                return i;
            }
        }
        return -1;
    }
    public void sort()
    {
        
    }
    public void printItemsInfo()
    {
    for(int i=0;i<nOfItems;i++)
        {
            System.out.println("Id: "+items[i].getID()+", name: "+items[i].getName()+", UPC: "+items[i].getUPC()+", number of items in the warehouse: "+nOfItems);
        }
    }
    public int getNumberOfItems()
    {
        return nOfItems;
    }
    public boolean isFull()
    {
       if(nOfItems==items.length)
       return true;
       else return false;
    }
     public boolean isEmpty()
     {
       if(nOfItems==0)
       return true;
       else return false;
     }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now