views:

201

answers:

3

So I have this class:

public class Product  {
 private String name, id, info ;
 private int quantity;

 public Product(String newName, String newID, String newInfo, Integer newQuantity){
  setName(newName);
  setID(newID);
  setPrice(newInfo);
  setQuantity(newQuantity);}

 public void setName(String name) {
  this.name = name;  }

 public void setID(String id) {
  this.id = id;  }

 public void setPrice(String info) {
  this.info = info;  }

 public void setQuantity(Integer quantity) {
  this.quantity = quantity;   }

 public String getID( ) { 
    return id;  }

 public String getName( ) { 
  return name;   }

 public String getInfo( ) { 
  return info; }

 public int getQuantity( ) { 
  return quantity;  }

In another class i have this:

 public class Invoice implements Group<Product> {
   private HashMap<String, Product> prod = new HashMap<String, Product>( );

  public Invoice(){ } 
   public void addProd(Product a) {

      prod.put(??getID()??,new Product(??));
   }  
}

If this data was user generated rather than me, I would use the getID() method right? So in my class invoice, how do i use the method getID() so that I can use it in the parameter for my key value in the HashMap? Also is there a way to add 3 values (name info quan) to the hashmap without making a new class?

A: 

Your class Product does not compile, because you have the name Item in your constructor. The constructor name must match the class name. So change that to Product. The same applies to Invoice vs ShoppingCart. Constructor and Class names must match.

As per your comment, you'd like to add four product values to a Map. The key being one of the values of the product itself. Try this:

Product p = new Product(name, id, info, quantity);
cart.addProd(p);

...

public void addProd(Product p) {
    prod.put(p.getId(), p);
}

Maps can only map a single value to a single key, so you must have some sort of container for the values you wish to collate into one value. This can be an object (Product) or you could use a collection (e.g. List). I strongly recommend the former.

Synesso
not exactly. what this program does is that I asked a user for a command, say he gives me data for an item with the id, name, info, quan. then the Invoice class would add these 4 things into the HashMap with id being the key. I actually don't know what should be in the prod.put() I was guessing that I would use getID() to obtain the user input. My bad on the typo
Jack
Oh group is just an interface with the addProd method. Do I actually need the Invoice constructor in Invoice?
Jack
As long as you don't have any other constructor, the default constructor is optional.
Synesso
Note, the default constructor is public ClassName() {super();}, not public ClassName(){}. Though if you only extend Object then the effect is the same.
Synesso
would the Product p line of code by in the Invoice class? And was is cart?
Jack
+3  A: 

I see that you get Product object with ref "a" as parameter to your addProd method.

And you can get id by just using a.getID(). It should look as:

  public void addProd(Product a) {

      prod.put(a.getID(),a);
  }  

I didn't understand second part of your question.. I think you already have 3 values in your Product object and you put Product object to Map, So why do you require another way ?

YoK
ok now it works. for some reason i put that and it didnt work. Thanks!
Jack
@Jack if you are satisfied with my answer please accept my answer with tick :). This will also improve your acceptance rate. And acceptance rate motivate ppl to answer questions asked by you :)
YoK
Why vote down ? Please explain. Someone who does vote down needs to explain reason for same. Its not rule but general ethics on Stackoverflow.
YoK
A: 

For your question about putting 3 values in your map, I don't think there's a way for you to put 3 values into one key without creating a class. An alternative is to store a Map<String, List<String>> assuming your 3 values are type String, or, Map<String, Map<String, String>>.

Glide