Instantiating an array with object of another class
I can't figure out why I'm not getting the String representation
of "item" from my toString. I'm getting an incompatible type error
Please point me in the right direction.
Here's my code:
Code:
public class Shopping
{
private static Cart [] carts;
public static void main ( String[] args)
{
int numOfCarts = 2;
carts = new Cart[ numOfCarts]; // create a Cart array of multiple carts
// add logic to process each cart in the array here
int numOfItems = 5;
String typeSize = "Medium";
String typeSeat = "Regular";
carts[0] = new Cart(numOfItems, typeSize, typeSeat); // creates a cart object with values
String itemName = "apple";
double itemPrice = .50;
int itemQuantity = 5;
Item item = new Item(itemName, itemPrice, itemQuantity); //instantiates item
String s = "";
s = item;
carts[0].addItemToCustomerCart(item);
System.out.println(carts);
}// end method main
@Override //return String representation of Item object
public String toString()
{
String cartString = "";
for ( Cart cart : carts )
{
cartString += cart + "/n";
}
return String.format( " %s", cartString );
}
}//end class Shopping
import java.util.ArrayList;
public class Cart
{
private String sizeType;
private String seatType;
private ArrayList <Item> items;
//no argument constructor creates a cart with a default size (10)
public Cart()
{
} // end no argument constructor
public Cart( int n, String s, String t)
{
int initNbrOfItems = n > 0? n : 10; // validate
items = new ArrayList< Item >( initNbrOfItems ); //create ArrayList
setSizeType(s);
setSeatType(t);
} // end three parameter constructor
public void addItemToCustomerCart( Item I)
{
items.add( I );
for ( Item item : items )
{
System.out.println(item);
}
} //end method addItemToCustomerCart
public void setSizeType(String Size)
{
sizeType = Size;
} //end method setSizeType
public void setSeatType(String Seat)
{
seatType = Seat;
} // end method setSeatType
public String getSizeType()
{
return sizeType;
} //end method getSizeType
public String getSeatType()
{
return seatType;
} //end method getSeatType
@Override //return String representation of each element in Item ArrayList
public String toString()
{
String itemString = "";
for ( Item item : items )
{
itemString += item + "/n";
}
return String.format( " %s", itemString );
}//end method toString
} //end class Cart
public class Item
{
private String iName;
private double iPrice;
private int iQuantity;
// no argument constructor
public Item()
{}// no argument constructor
public Item(String n, double p, int q)
{
setIName(n);
setIPrice(p);
setIQuantity(q);
}// end three parameter constructor
public void setIName(String name)
{
iName = name;
} //end method setIName
public void setIPrice(double price)
{
iPrice = price;
} // end method setIPrice
public void setIQuantity(int quantity)
{
iQuantity = quantity;
} // end method setIQuantity
public String getIName()
{
return iName;
} //end method getIName
public double getIPrice()
{
return iPrice;
} //end method getIPrice
public int getIQuantity()
{
return iQuantity;
} //end method getIQuantity
// return String representation of the Item object
@Override
public String toString()
{
return String.format( "%s %d %d\n",
iName, iPrice, iQuantity );
}// end method toString for the item object
}//end class Item
Re: Instantiating an array with object of another class
ArrayList<Carts> leArray = new ArrayList<Carts>();
Re: Instantiating an array with object of another class
Can you tell us more, specifically, what line is causing the error and what is the exact error message?
Re: Instantiating an array with object of another class
The compiler error is at line 24 and 25 above
incompatible type
Required: java.lang.String
found: Item
Re: Instantiating an array with object of another class
These are lines 24 and 25:
Code:
String s = "";
s = item;
Why do you think you can assign an Item reference to a String reference?
Re: Instantiating an array with object of another class
In the line 25
string can not be referened to a object directly. If give reference then it will throw the exception like incompatible type.
if you want Item object reference to a string then you need do Item.toString().
Then exception will not occur.
Re: Instantiating an array with object of another class
Do you need to call the Item class's toString method?
Re: Instantiating an array with object of another class
Quote:
Originally Posted by
dinesh.guntha
In the line 25
string can not be referened to a object directly. If give reference then it will throw the exception like incompatible type.
if you want Item object reference to a string then you need do Item.toString().
Then exception will not occur.
Which is only a solution if the OP actually wants the result of the toString referenced by 's'.
Since 's' is not used in the above code they may intend something else entirely, and your 'fix' is simply a way to get things to compile.
Re: Instantiating an array with object of another class
It converts the item class object reference to string.
what is requirement we dont know, but he is laking at that place. so that i tryed to explain that error & how to overcome from that error.
Re: Instantiating an array with object of another class
Just fixing the cosmetic error may not fix the underlying problem, that's all I'm saying.
I see it too often in bug fixes...
Re: Instantiating an array with object of another class
Thanks for all the help. To get the program to compile I changed line 24 to read
s += item; That forces Item item into a string.
Re: Instantiating an array with object of another class
The simple way would be to call the Item class's toString() method:
String s = item.toString();