In: Computer Science
Please include all classes including driver.
IN JAVA
Suppose that there is a big farm in California that supplies the majority of the Grocery stores in the twin cities with vegetables and fruits. The stores submit their orders and receive the shipment a week later. Based on the size of the orders, the farm management decides on the size of the truck to load.
Create a Produce class that have an instance variable of type String for the name, appropriate constructors, appropriate accessor and mutator methods, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String, the price (this is the price per box) as a double, the quantity as an integer, and invoke the appropriate constructor from the base class to set the name. Also, they should override toString method to display the name of the produce, the price, and its type. For instance, Mango is a Fruit and Cauliflower is a Vegetable.
Finally, create a class called TruckOfProduce that will keep track of the boxes of Vegetables and Fruits added in to the truck. This class should use an array of Produce to store both vegetables and fruits. Also, it should have the following:
• Constructor that accepts an integer to initialize the array of Produce
• addProduce method that adds either fruit or vegetable to the array
• search method that accepts a name string, which can be either the name of a fruit or vegetable, and returns true if the name exists. Otherwise, it returns false.
• remove method that accepts a produce object and returns true if the produce is found and removed successfully. Otherwise, it returns false.
• computeTotal method that will return the total cost of all the produce in the truck.
• toString method that returns all the produce from in the truck.
• ensureCapacity method this is a void method. When this method is called it will double the size of the array and copy all the content of the smaller array in to this bigger array.
For instance, one could initialize the array to hold 1000 boxes of vegetables and fruits. What will happen if you try to add one more box (1001)? Of course, you will get an ArrayIndexOutOfBound error. So, thing about ensureCapacity as the method that will solve ArrayIndexOutBound error.
Then wrap it up with the driver class that tests all methods and displays the name of the produce, the price, and its type.
Below is the java classes and the main driver class :
class Produce
{
//instance variable of the product name
protected String name;
//public constructors
public Produce(String name)
{
this.name = name;
}
public Produce()
{
this.name = "";
}
//Accesors and mutators for the name
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public String toString()
{
return this.name;
}
}
//The Fruit class
class Fruit extends Produce
{
protected double price;
protected int quantity;
public Fruit(String name, double price, int
quantity)
{
super.setName(name);
this.price = price;
this.quantity = quantity;
}
public String toString()
{
//This stores the string
representation of the product
String str = "";
str += "Name : " + this.name +
"\n";
str += "Price : " + this.price +
"\n";
str += "Type : " + "Fruit";
return str;
}
//Accesors and mutators
public void setPrice(double price)
{
this.price = price;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public double getPrice()
{
return this.price;
}
public int getQuantity()
{
return this.quantity;
}
}
//The Vegetable class
class Vegetable extends Produce
{
protected double price;
protected int quantity;
public Vegetable(String name, double price, int
quantity)
{
super.setName(name);
this.price = price;
this.quantity = quantity;
}
public String toString()
{
//This stores the string
representation of the product
String str = "";
str += "Name : " + this.name +
"\n";
str += "Price : " + this.price +
"\n";
str += "Type : " + "Vegetable";
return str;
}
//Accesors and mutators
public void setPrice(double price)
{
this.price = price;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public double getPrice()
{
return this.price;
}
public int getQuantity()
{
return this.quantity;
}
}
class TruckOfProduce
{
//Array of products
protected Produce products[];
//Stores the number of products currently present
in the array
protected int num;
//the current capacity of the array
protected int capacity;
public TruckOfProduce(int size)
{
this.capacity = size;
this.num = 0;
//initializing the produce
array
this.products = new
Produce[this.capacity];
}
//Addes a fruit or vegetable to the array
public void addProduce(Produce produce)
{
// Checking if the array has
exeeded its limit
if (this.num >=
this.capacity)
this.ensureCapacity();
//adding the produce
this.products[this.num++] =
produce;
}
//Search for a string or vegetable
public boolean search(String str)
{
int i;
for (i = 0; i < this.num;
i++)
{
//Cheking if the
string matchs using the String.equals() method
if
(this.products[i].getName().equals(str))
return true;
}
//control will come here if the
str does not match so returning false
return false;
}
public String toString()
{
String str = "";
for (int i = 0; i < this.num;
i++)
{
str += "Produce
" + (i + 1) + " : \n";
str +=
this.products[i].toString() + "\n\n";
}
return str;
}
protected void ensureCapacity()
{
//increase the capacity by 2
this.capacity *= 2;
Produce newArray[] = new Produce[this.capacity];
//moving the elements form the
old array to the new one
for (int i = 0; i < this.num;
i++)
{
newArray[i] =
this.products[i];
}
this.products = newArray;
}
public boolean remove(Produce p)
{
int i, j;
for (i = 0; i < this.num;
i++)
{
if
(this.products[i] == p)
break;
}
//if the object was not found, i
must have gone the this.num
if (i == this.num)
return
false;
else
{
for (j = i; j
< this.num - 1; j++)
this.products[j] = this.products[j + 1];
this.products[this.num - 1] = null;
this.num--;
}
return true;
}
public double computeTotal()
{
double total = 0.0;
for (int i = 0; i < this.num;
i++)
{
double price =
0;
int q = 0;
//Checking the
class type to down cast
if
(this.products[i].getClass() == Fruit.class)
{
price =
((Fruit)this.products[i]).getPrice();
q =
((Fruit)this.products[i]).getQuantity();
}
else
{
price =
((Vegetable)this.products[i]).getPrice();
q =
((Vegetable)this.products[i]).getQuantity();
}
total += price *
q;
}
return total;
}
}
//The main driver class
class Main
{
public static void main(String args[])
{
Produce f1 = new Fruit("Mango", 20,
3);
Produce f2 = new Fruit("Bananna",
10, 6);
Produce v1 = new Fruit("Tomato",
15, 2);
Produce v2 = new Fruit("Carrot",
10, 5);
TruckOfProduce truck = new TruckOfProduce(2);
truck.addProduce(f1);
truck.addProduce(f2);
truck.addProduce(v1);
truck.addProduce(v2);
System.out.println("Contents of
the truck are : \n");
System.out.println(truck);
if (truck.remove(v1))
{
System.out.println("Contents of the truck after removing " +
v1.getName() + " : \n");
System.out.println(truck);
}
System.out.println("Total cost of all the produce in the truck : " + truck.computeTotal() + "\n");
if (truck.search("Mango"))
System.out.println("Mango found in the truck");
else
System.out.println("Mango not found in the truck");
if
(truck.search("Strawberry"))
System.out.println("\nStrawberry found in the truck");
else
System.out.println("\nStrawberry not found in the truck");
}
}
OUTPUT :
C:\Users\HeathCliff\Desktop>javac my.java
C:\Users\HeathCliff\Desktop>java Main
Contents of the truck are :
Produce 1 :
Name : Mango
Price : 20.0
Type : Fruit
Produce 2 :
Name : Bananna
Price : 10.0
Type : Fruit
Produce 3 :
Name : Tomato
Price : 15.0
Type : Fruit
Produce 4 :
Name : Carrot
Price : 10.0
Type : Fruit
Contents of the truck after removing Tomato :
Produce 1 :
Name : Mango
Price : 20.0
Type : Fruit
Produce 2 :
Name : Bananna
Price : 10.0
Type : Fruit
Produce 3 :
Name : Carrot
Price : 10.0
Type : Fruit
Total cost of all the produce in the truck : 170.0
Mango found in the truck
Strawberry not found in the truck