im trying to solve this question, i did some of it but i dont think that its correct or complete:
this is the question:
Write the definition of a class called Product. A Product object should represent a product stocked in a supermarket, e.g. a 500 gram pot of
yogurt. It should contain the following information: a code for the
product, the name of the product, the cost of the product, and the
quantity of the product currently in stock. Assume the variables code
and name are represented by strings of characters. Include the
following constructor and methods in the class definition.
i. A constructor Product(code, name) which creates a new product with the given code and name. Initially, the cost and the quantity of the product should be set to zero.
ii. An instance method getName() that will return the name of the product.
iii. An instance method addStock(int n) that will add n to the quantity of the product in stock. This method throws exception where n is negative number. Add finally block which prints the value of n.
below is the code that i have attempted to do, im only having problems solving part iii, in which i need help in.
Expand|Select|Wrap|Line Numbers
- public class Product {
- String code;
- String name;
- double cost;
- long quantity;
- public Product(){
- cost=0;
- quantity=0;
- }
- public Product(String code, String name){
- this.code=code;
- this.name=name;
- }
- public String getname(){
- return name;
- }
- public void setname(String Name){
- name=Name;
- }
outofmymind