Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: Need major help!

  1. #1
    noncon is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need major help!

    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];
    Please use code tags when posting code, otherwise it loses all formatting.

    What problems are you having with those methods?
    Compilation errors?
    Runtime errors?
    Logic (ie incorrect output) errors?

  3. #3
    noncon is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need major help!

    Noted.
    I mostly have null pointer exceptions and logic errors.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need major help!

    So it compiles.

    People here aren't going to run your code to see where you are having problems.
    You need to tell us what, where, stack traces etc.
    DarrylBurke likes this.

Similar Threads

  1. Major code error need help!!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-29-2010, 09:43 AM
  2. Major issues with code - Please help
    By aldorfski_17 in forum New To Java
    Replies: 3
    Last Post: 03-29-2010, 04:46 AM
  3. Major number
    By lobodelbosque in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 05:55 AM
  4. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  5. Software Engineer...Computer Science Major
    By giganews35 in forum Introductions
    Replies: 2
    Last Post: 09-14-2008, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •