Results 1 to 4 of 4
Thread: Need major help!
- 02-01-2012, 12:22 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Need major help!
I am completely lost and i need help with this coding.
Any help with the getTotalPriceOfTrolleyItems where I have to get the Total price of the items in the trolley, clearAllItemsFromTrolley is to clear the array and removeItemFromTrolley is to remove an object from the array. Any help with these methods will be greatly appreciated.
public class ShoppingTrolley {
public static final int MAXIMUM_NUMBER_OF_ITEMS = 200;
private Item[] itemsInTrolley;
private int itemsSoFar;
public ShoppingTrolley() {
itemsInTrolley = new Item[MAXIMUM_NUMBER_OF_ITEMS];
itemsSoFar = 0;
}
public int getNumberOfItemsInTrolley() {
return itemsSoFar; //You will need to change this
}
public void addItemToTrolleyIfAvailable(Item itemToAdd) {
itemsInTrolley[itemsSoFar] = itemToAdd;
itemsSoFar++;
itemToAdd.decreaseNumberInStockBy1();
}
public void printTrolleyItems() {
for (int i=0; i <itemsInTrolley.length; i++){
System.out.println (itemsInTrolley[i].getFullDescription());
}
}
public double getTotalPriceOfTrolleyItems() {
double totalPrice = 0;
for (int i = 0; i < itemsInTrolley.length; i++){
totalPrice = totalPrice + itemsInTrolley[i].getPrice();
return totalPrice ;
}
return -1;
}
//You will need to change this
public void clearAllItemsFromTrolley() {
for (int i = 0; i < itemsInTrolley.length; i++){
itemsInTrolley [i] = null;
itemsSoFar = itemsSoFar - 1;
itemsInTrolley[i].increaseNumberInStockBy1();
}
}
public void removeItemFromTrolley(int indexToRemove) {
for(int i = 0; i < itemsInTrolley.length - 1; i++) {
itemsInTrolley[indexToRemove -1] = null;
//itemsInTrolley [itemsInTrolley.length - 1];Last edited by noncon; 02-01-2012 at 12:25 PM.
- 02-01-2012, 12:42 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Need major help!
Please use code tags when posting code, otherwise it loses all formatting.Java Code:public class ShoppingTrolley { public static final int MAXIMUM_NUMBER_OF_ITEMS = 200; private Item[] itemsInTrolley; private int itemsSoFar; public ShoppingTrolley() { itemsInTrolley = new Item[MAXIMUM_NUMBER_OF_ITEMS]; itemsSoFar = 0; } public int getNumberOfItemsInTrolley() { return itemsSoFar; //You will need to change this } public void addItemToTrolleyIfAvailable(Item itemToAdd) { itemsInTrolley[itemsSoFar] = itemToAdd; itemsSoFar++; itemToAdd.decreaseNumberInStockBy1(); } public void printTrolleyItems() { for (int i=0; i <itemsInTrolley.length; i++){ System.out.println (itemsInTrolley[i].getFullDescription()); } } public double getTotalPriceOfTrolleyItems() { double totalPrice = 0; for (int i = 0; i < itemsInTrolley.length; i++){ totalPrice = totalPrice + itemsInTrolley[i].getPrice(); return totalPrice ; } return -1; } //You will need to change this public void clearAllItemsFromTrolley() { for (int i = 0; i < itemsInTrolley.length; i++){ itemsInTrolley [i] = null; itemsSoFar = itemsSoFar - 1; itemsInTrolley[i].increaseNumberInStockBy1(); } } public void removeItemFromTrolley(int indexToRemove) { for(int i = 0; i < itemsInTrolley.length - 1; i++) { itemsInTrolley[indexToRemove -1] = null; //itemsInTrolley [itemsInTrolley.length - 1];
What problems are you having with those methods?
Compilation errors?
Runtime errors?
Logic (ie incorrect output) errors?
- 02-01-2012, 12:52 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Re: Need major help!
Noted.
I mostly have null pointer exceptions and logic errors.
- 02-01-2012, 02:04 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Major code error need help!!!
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 03-29-2010, 09:43 AM -
Major issues with code - Please help
By aldorfski_17 in forum New To JavaReplies: 3Last Post: 03-29-2010, 04:46 AM -
Major number
By lobodelbosque in forum New To JavaReplies: 1Last Post: 11-27-2009, 05:55 AM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
Software Engineer...Computer Science Major
By giganews35 in forum IntroductionsReplies: 2Last Post: 09-14-2008, 09:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks