Advertisement
Guest User

Item.java

a guest
Sep 16th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. // ***************************************************************
  2. // Item.java
  3. //
  4. // Represents an item in a shopping cart.
  5. // ***************************************************************
  6. import java.text.NumberFormat;
  7. public class Item
  8. {
  9. private String name;
  10. private double price;
  11. private int quantity;
  12.  
  13. // -------------------------------------------------------
  14. // Create a new item with the given attributes.
  15. // -------------------------------------------------------
  16. public Item (String itemName, double itemPrice, int numPurchased)
  17. {
  18. name = itemName;
  19. price = itemPrice;
  20. quantity = numPurchased;
  21. }
  22.  
  23. // -------------------------------------------------------
  24. // Return a string with the information about the item
  25. // -------------------------------------------------------
  26. public String toString ()
  27. {
  28. NumberFormat fmt = NumberFormat.getCurrencyInstance();
  29. return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
  30. + fmt.format(price*quantity));
  31. }
  32.  
  33. // -------------------------------------------------
  34. // Returns the unit price of the item
  35. // -------------------------------------------------
  36. public double getPrice()
  37. {
  38. return price;
  39. }
  40.  
  41. // -------------------------------------------------
  42. // Returns the name of the item
  43. // -------------------------------------------------
  44. public String getName()
  45. {
  46. return name;
  47. }
  48.  
  49. // -------------------------------------------------
  50. // Returns the quantity of the item
  51. // -------------------------------------------------
  52. public int getQuantity()
  53. {
  54. return quantity;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement