Thread: Help with Java

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Help with Java


    Hello all. I need some help creating a new class in my java program. I need to create a class that does two things. The first is the plan for which stores to visit in what order and what to buy at each of those stores. This plan will be stored as an ArrayList of Store objects. The StoreItems in these Store objects will eventually contain the items to buy from that store. The second component of the shopping plan is a list of items we were unable to find in any store, which will be stored in an ArrayList of RecipeItem objects. Additionally, the shopping list will have a toString method.


    I'm lost on how to go about creating this class. I will add the RecipeItem and Store classes in the comments.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Store class:

    import java.util.ArrayList;


    public class Store {

    private String id;
    private String name;
    private String address;
    private GPSLocation location;
    private ArrayList<StoreItem> items;

    /**
    * Create a store with the given name and location
    * @param name
    * @param location
    */
    public Store(String id, String name, String address, GPSLocation location){
    this.id = id;
    this.name = name;
    this.address = address;
    this.location = location;
    items = new ArrayList<StoreItem>();
    }

    /**
    * Add an item to this store
    * @param item
    * @param price
    * @param quantity
    */
    public void addItem(Item item, double price, int quantity){
    items.add(new StoreItem(item, price, quantity));
    }

    /**
    * Returns a list of items in the store
    * @return
    */
    public ArrayList<StoreItem> getInventory(){
    //i'm just going to make sure no one can change my list.
    return items;
    }

    /**
    * Returns the StoreItem given the UPC. Returns null if not found
    * @param UPC
    * @return
    */
    public StoreItem getItemByUPC(String UPC){
    for(StoreItem item : items){
    if(item.getItem().getUpc().equals(UPC)){
    return item;
    }
    }
    return null;
    }

    /**
    * @return the name
    */
    public String getName() {
    return name;
    }

    /**
    * @return the id
    */
    public String getId() {
    return id;
    }

    /**
    * @return the location
    */
    public GPSLocation getLocation() {
    return location;
    }

    /**
    * @return the address
    */
    public String getAddress() {
    return address;
    }



    @Override
    public String toString(){
    String out = getId() + "\n";
    out += getName() + "\n";
    out += getLocation() + "\n";
    for(StoreItem item : items){
    out += "\t" + item + "\n";
    }
    return out;
    }
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    RecipeItem class:

    /**
    * An item in a recipe
    *
    */
    public class RecipeItem {

    private Item item;
    private int quantity;

    public RecipeItem(Item item, int quantity) {
    this.item = item;
    this.quantity = quantity;
    }

    /**
    * @return the item
    */
    public Item getItem() {
    return item;
    }

    /**
    * @return the quantity
    */
    public int getQuantity() {
    return quantity;
    }

    @Override
    public String toString(){
    return getItem() + " Qty: " + getQuantity();
    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo