1

I need to be able to update the quantity very simply in this section of code:

import java.util.HashMap; import java.util.Map; public class StockData { private static class Item { Item(String n, double p, int q) { name = n; price = p; quantity = q; } // get methods public String getName() { return name; } public double getPrice() { return price; } public int getQuantity() { return quantity; } // instance variables private String name; private double price; private int quantity; } // with a Map you use put to insert a key, value pair // and get(key) to retrieve the value associated with a key // You don't need to understand how this works! private static Map<String, Item> stock = new HashMap<String, Item>(); static { // if you want to have extra stock items, put them in here // use the same style - keys should be Strings stock.put("00", new Item("Bath towel", 5.50, 10)); stock.put("11", new Item("Plebney light", 20.00, 5)); stock.put("22", new Item("Gorilla suit", 30.00, 7)); stock.put("33", new Item("Whizz games console", 50.00, 8)); stock.put("44", new Item("Oven", 200.00, 4)); } public static String getName(String key) { Item item = stock.get(key); if (item == null) return null; // null means no such item else return item.getName(); } public static double getPrice(String key) { Item item = stock.get(key); if (item == null) return -1.0; // negative price means no such item else return item.getPrice(); } public static int getQuantity(String key) { Item item = stock.get(key); if (item == null) return -1; // negative quantity means no such item else return item.getQuantity(); } // update stock levels // extra is +ve if adding stock // extra is -ve if selling stock public static void update(String key, int extra) { Item item = stock.get(key); if (item != null) item.quantity += extra; } } 

I've built the GUI for the update page and just need the method to add into it?

Sorry for the trivial question but you have to start somewhere.

Thanks for your help.

2
  • 1
    so you want that someone help you to write a update method? Commented Mar 4, 2013 at 11:13
  • What is your question exactly? Commented Mar 4, 2013 at 11:15

2 Answers 2

1

I don´t know if I really get your problem, but this is my suggestion to increase the quantity of your Item.

Just add a public method like this:

public void addQuantity(int q) { quantity += q } 

I hope you meant this.

Sign up to request clarification or add additional context in comments.

2 Comments

Strictly speaking, this doesn't update the quantity but adds to it.
You´re right, but I think a method to add the quantity would be more suitable in his case.
0

Add this method:

public void setQuantity(int q) { quantity = q }

And you may want to start reading at http://docs.oracle.com/javase/tutorial/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.