In: Computer Science
(Java Problem)
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.
Here is the solution for your answer. I have also created main() for better understanding and it is very much user interactive. Also, Comments has been given where you can find some code miscellaneous. Still if you have any doubt feel free to ask.
/*file: TruckOfProduce.java*/
import java.util.*;
class Produce
{
private String name;
public Produce(String s) {
name=s;
}
public String getName(){
return name;
}
public void setName(String s){
name=s;
}
public String toString(){
return ("Name is: "+name);
}
/*following methods are added because base class refernce was unable to figure out these methods in derived class*/
public int getQuantity()
{
return 0;
}
public double getPrice()
{
return 0;
}
public void setQuantity(int quantity)
{
return;
}
public void setPrice(double price)
{
return;
}
}
class Fruits extends Produce
{
private double price;
private int quantity;
public Fruits(String s,double price,int quantity)
{
super(s);
this.price=price;
this.quantity=quantity;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
public void setQuantity(int quantity)
{
this.quantity=quantity;
}
public void setPrice(double price)
{
this.price=price;
}
public String toString()
{
return (super.getName()+" is a fruit. It cost is: "+price+" per box.");
}
}
class Vegetables extends Produce
{
private double price;
private int quantity;
public Vegetables(String s,double price,int quantity)
{
super(s);
this.price=price;
this.quantity=quantity;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
public void setQuantity(int quantity)
{
this.quantity=quantity;
}
public void setPrice(double price)
{
this.price=price;
}
public String toString()
{
return (super.getName()+" is a vegetable. It cost is: "+price+" per box.");
}
}
class TruckOfProduce
{
private Produce arrayOfProduce[];
private int i=0,n;
public TruckOfProduce(int n)
{
arrayOfProduce=new Produce[n];
this.n=n;
}
void addProduce()
{
if(i>=n)
System.out.println("No more items can be added.");
else
{
System.out.println("1.Add a fruit.\n2.Add a vegetable.\nEnter your choice:");
int ch;
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
String s;
int q;
double p;
switch(ch)
{
case 1: System.out.println("Enter Fruit name: ");
s=sc.next();
System.out.println("Enter Quantity(No. of Boxes): ");
q=sc.nextInt();
System.out.println("Enter Price per box:");
p=sc.nextDouble();
arrayOfProduce[i]=new Fruits(s,p,q);
i++;
break;
case 2: System.out.println("Enter Vegetable name: ");
s=sc.next();
System.out.println("Enter Quantity(No. of Boxes): ");
q=sc.nextInt();
System.out.println("Enter Price per box:");
p=sc.nextDouble();
arrayOfProduce[i]=new Vegetables(s,p,q);
i++;
break;
}
}
}
public boolean Search(String s)
{
for(int j=0;j<i;j++)
{
if(s.equalsIgnoreCase(arrayOfProduce[j].getName())==true)
return true;
}
return false;
}
public double computeTotal()
{
double cost=0;
for(int j=0;j<i;j++)
{
//calculate cost
cost+=(arrayOfProduce[j].getPrice()*arrayOfProduce[j].getQuantity());
}
return cost;
}
public boolean removed(Produce p)
{
for(int j=0;j<i;j++)
{
if(p.getName().equalsIgnoreCase(arrayOfProduce[j].getName())==true)
{
/*shifting back the remaining items. I overwrite the element that we want to remove.*/
for(int k=j+1;k<i;k++)
arrayOfProduce[k-1]=arrayOfProduce[k];
i--;
return true;
}
}
return false;
}
public String toString()
{
String s=" ";
for(int j=0;j<i;j++)
s=s+"Item "+(j+1)+": "+arrayOfProduce[j].toString();
return s;
}
public static void main(String []args)
{
TruckOfProduce object=new TruckOfProduce(5);
Scanner s=new Scanner(System.in);
int ch;
do{
System.out.println("Eneter your choice:\n1.Add item\n2.Search\n3.removed\n4.Compute total\n5.display\n6.exit");
ch=s.nextInt();
switch(ch)
{
case 1: object.addProduce();
break;
case 2: System.out.println("Enter item you want to search ");
if(object.Search(s.next())==true)
System.out.println("Element Found");
else
System.out.println("Element not Found");
break;
case 3: System.out.println("Enter item you want to remove");
Produce p=new Produce(s.next());
if(object.removed(p)==true)
System.out.println("Element removed");
else
System.out.println("Element not Found");
break;
case 4: System.out.println("Total amount is: "+object.computeTotal());
break;
case 5: System.out.println(object);
break;
case 6: return;
}
}while(true);
}
}
/*feel free to ask if you have any query.*/