Assignment Description You will write three Class objects an

Assignment Description You will write three Class objects (and subsequently submit three java files): Item ItemCatalog ItemCatalogDriver Class Design: Item The Item class is intended to be an abstract and simplified representation of an item for sale. Data to store Name Description Price (let\'s make it simple and say \"each\") Quantity

Solution

import java.util.ArrayList;

import java.util.Iterator;

public class ItemCatalogDriver {

   public static void main(String[] args) {

       Item i1 = new Item(\"Apple\", \"fruit\", 15.05, 3);

       Item i2 = new Item(\"KitKat\", \"Chocolate\", 5.00, 6);

       Item i3 = new Item(\"Benz\", \"Car\", 99995.05, 10);

       Item i4 = new Item(\"AirIndia\", \"Plane\", 998.56, 2);

       // to dispaly object

       System.out.println(\"Display before purchase\");

       i4.display();

       // to purchase quantity

       i4.purchase(3);

       i4.purchase(1);

       System.out.println(\"Display after purchase\");

       i4.display();

       // to restock quantity

       i4.reStock(-1);

       i4.reStock(10);

       System.out.println(\"Display after ReStock\");

       i4.display();

       ItemCatolog ic = new ItemCatolog();

       // to add pre populated items

       ic.additem(i1);

       ic.additem(i2);

       ic.additem(i3);

       // to dispaly itemCatolog before add

       System.out.println(\"before adding\");

       ic.displayItems();

       ic.additem(i4);

       // to display itemcatolog after add

       System.out.println(\"After adding\");

       ic.displayItems();

      

       System.out.println(\"Purchasing item\");

       ic.list[2].display();

       ic.list[2].purchase(2);

       ic.list[2].display();

      

       System.out.println(\"After purchase\");

       ic.displayItems();

   }

}

class Item {

   String name;

   String description;

   double price;

   int quantity;

   public void display() {

       System.out.println(

               name + \".\" + description + \". \" + price + \" each. Quantity: \" + quantity + \" available for sale.\");

   }

   public void purchase(int q) {

       if (quantity < q)

           System.out.println(\"Cannot purchase \" + q + \" quantity\");

       else

           this.quantity = this.quantity - q;

   }

   public void reStock(int q) {

       if (q < 0)

           System.out.println(\"Cannot restock to \" + q + \" quantity\");

       else

           this.quantity = q;

   }

   public Item(String name, String description, double price, int quantity) {

       super();

       this.name = name;

       this.description = description;

       this.price = price;

       this.quantity = quantity;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public String getDescription() {

       return description;

   }

   public void setDescription(String description) {

       this.description = description;

   }

   public double getPrice() {

       return price;

   }

   public void setPrice(double price) {

       this.price = price;

   }

   public int getQuantity() {

       return quantity;

   }

   public void setQuantity(int quantity) {

       this.quantity = quantity;

   }

}

class ItemCatolog {

   Item[] list = new Item[100];

   int i = 0;

   public void displayItems() {

       for (int j = 0; j < i; j++)

           list[j].display();

   }

   public void additem(Item item) {

       list[i] = item;

       i++;

   }

}

 Assignment Description You will write three Class objects (and subsequently submit three java files): Item ItemCatalog ItemCatalogDriver Class Design: Item The
 Assignment Description You will write three Class objects (and subsequently submit three java files): Item ItemCatalog ItemCatalogDriver Class Design: Item The
 Assignment Description You will write three Class objects (and subsequently submit three java files): Item ItemCatalog ItemCatalogDriver Class Design: Item The
 Assignment Description You will write three Class objects (and subsequently submit three java files): Item ItemCatalog ItemCatalogDriver Class Design: Item The

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site