Results 1 to 4 of 4
  1. #1
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default changing an element in an ArrayList

    hello there,
    this is the first time for me to post something, but i am in serious need and just can't find what is wrong... I am asked to write classes for a GUI code of a shopping program, I wrote it all but, in the class shoppingCart i am asked to write a method that "Adds an item order to the list, replacing any previous order for this item with the new order." Thus resetting the total price shown, and not adding the new total price to the old one. For example changing 1 selly Pitty (costs $3.95) with 3 changes the total price from 3.95 to 7.9 and not 11.85
    This is what i wrote
    Last edited by baf06; 04-07-2011 at 05:57 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default

    Check out the link in my signature on asking smart questions. Mentioning your urgency will actually decrease your chances of getting help- it makes you look impatient and selfish, as their are hundreds of posts here, each with its own urgent user waiting on a reply.

    If you want help, you'll have to narrow your problem down to an SSCCE that demonstrates what you've tried. We simply do not have time to wade through attached files and tons of code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    hi again,
    and thanks for the link and the comment,

    import java.util.*;
    public class ShoppingCart
    {
    private ArrayList<ItemOrder> cart;
    private boolean hasDiscount;

    /**
    * Constructor that creates an empty ArrayList of ItemOrder
    */
    public ShoppingCart(){
    cart = new ArrayList<ItemOrder>();
    }

    /**
    * Method that adds an ItemOrder to the list
    * if the list contains any previous order of this item
    * it replaces it with the new order
    * @param ItemOrder the order to be added
    */
    public void add(ItemOrder itemOrder){
    if(cart.contains(itemOrder.getItem())){
    int index = cart.indexOf(itemOrder.getItem());
    cart.set(index, itemOrder);
    } else cart.add(itemOrder);
    }

    /**
    * Method that sets whether or not this order gets a discount
    * @param boolean that determines if it gets a discount
    */
    public boolean setDiscount(boolean value){
    return hasDiscount = value;
    }

    /**
    * @return the total cost of the shopping cart
    * taking into consideration if the order gets a discount
    */
    public double getTotal(){
    double totalPrice = 0;
    for(int i = 0; i < cart.size(); i++){
    totalPrice += cart.get(i).getPrice();
    }
    if(hasDiscount)
    totalPrice *= 0.9;
    return totalPrice;
    }
    }

    this is my class that i am having problems with, in the add method i am supposed to replace any previous order with the new one, thus changing the total price and not adding the new order to the old one

  4. #4
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    this is the Item class to define an item

    PHP Code:
    import java.text.*;
    public class Item
    {
        private String itemName;
        private double itemPrice;
        private double itemBulkPrice;
        private int itemBulkQuantity;
        
        /**
         * This method creates an item throwing an exception if price is negative
         * @param String name of item 
         * @param double price of the item
         */
        public Item (String name, double price)
        {        
            if(price < 0)
                throw new IllegalArgumentException("Price you entered is negative");
            itemName = name;
            itemPrice = price;
        }
        
        /**
         * This method creates an item throwing an exception if price is negative
         * @param String name of item 
         *      double price of the item
         *      int Bulk Quantity
         *      double bulk price
         */
        public Item (String name, double price, int bulkQuantity, double bulkPrice)
        {
            if(price < 0 || bulkQuantity < 0 || bulkPrice < 0)
                throw new IllegalArgumentException("Price you entered is negative");
            itemName = name;
            itemPrice = price;
            itemBulkQuantity = bulkQuantity;
            itemBulkPrice = bulkPrice;
        }
        
        /**
         * This method return the price of the item
         * with considering the bulk price if it exists
         * @param int quantity to calculate its price
         */
        public double priceFor (int quantity)
        {
            if(quantity < 0)
                throw new IllegalArgumentException("Price you entered is negative");
            double totalPrice = 0;
            if (itemBulkQuantity > 0 ){
                int numOfBulk = quantity / itemBulkQuantity;
                int remainder = quantity % itemBulkQuantity;
                totalPrice = (numOfBulk * itemBulkPrice) + (remainder * itemPrice);
            } else 
                totalPrice = quantity * itemPrice;
            return totalPrice; 
        }
        
        /**
         * This method writes the item as a string of a certain form
         */
        public String toString(){
            NumberFormat prices = NumberFormat.getCurrencyInstance();
            String str = itemName + ", " + prices.format(itemPrice);
            if (itemBulkPrice > 0){
                str += " (" + itemBulkQuantity + " for " + prices.format(itemBulkPrice) + ")";
            }
            return str;
        }
    }
    this is my ItemOrder class

    PHP Code:
    import java.util.*;
    public class ItemOrder
    {
        private int orderQuantity;
        private Item orderItem;
        
        /**
         * Constructor that creates an item order for the given item
         * @param Item the ordered item
         * @param int the quantity order from the item
         */
        public ItemOrder(Item item, int quantity){
            orderItem = item;
            orderQuantity = quantity;
        }
    
        /**
         * @return the total cost of the item order
         */
        public double getPrice(){
            return orderItem.priceFor(orderQuantity);
        }
    
        /**
         * @return a reference to the Item object of the item order
         */
        public Item getItem(){
           return  this.orderItem;
    [/PHP]}
    }

    and the catalog class that stores information about a collection of items

    PHP Code:
    import java.util.*; 
    public class Catalog
    {
        private ArrayList<Item> catalogItems;
        private String catalogName;
    
        /**
         * Constructor that creates a list of Item objects
         * @param String the name of the catalog to be formed
         */
        public Catalog(String name){
            catalogName = name;
            catalogItems = new ArrayList<Item>();
        }
    
        /**
         * Method that adds an Item to the end of the list in the catalog
         * @param name of the Item to be added
         */
        public void add(Item item){
            catalogItems.add(item);
        }
    
        /**
         * @return the size of the catalog
         */
        public int size(){
            return catalogItems.size();
        }
    
        /**
         * Method that return the wanted Item object
         * @param int index of the Item from the catalog
         */
        public Item get(int index){
            return catalogItems.get(index);
        }
    
        /**
         * @return the name of this catalog
         */
        public String getName(){
            return catalogName;
        }
    }

    this is all working correctly but in the ShoppingCart class the add method is not working since the totalPrice is never being reset when an order is changed, it is being added to the old order

Similar Threads

  1. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 01:43 AM
  2. Very simple problem
    By Andyj in forum Java Certification
    Replies: 3
    Last Post: 09-17-2010, 06:49 AM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. simple GUI problem
    By McChill in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 03:13 AM
  5. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM

Posting Permissions

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