Exercise 1: Time to Go Shopping!
In this lesson, you have to make a class called Product and implement various methods in it.
We'll cover the following
Problem Statement
A class named Product
is given below. You need to define the following in it:
- The class
constructor
that takes the following as parameters in the exact order:_name
: the name of the product._price
: the price of the product in dollars._amount
: the amount of that product available._madeIn
: the country the product is made in.
Important Note: All of the above-mentioned properties should be defined as protected properties in the constructor.
-
In order to access the protected properties, the following methods need to be defined:
getName
: returns the name.getPrice
: returns the price.getAmount
: returns the amount.getMadeIn
: returns the name of the country the product is made in.setAmount(num)
: sets the value amount equal to the value passed.
-
Lastly, you need to implement the following methods inside the class:
-
canSell(num)
:num
will denote the amount of product that needs to be sold. The method checks if this amount or more is available. It’ll returntrue
if it does andfalse
if the amount available is less than what needs to be sold. -
sell(num)
: the method is used to sellnum
amount of a product. It should return the updated amount of product left after selling. If the amount of product available is less thannum
, it should restock the product by adding twice the amount requested to the previously available amount of product and should return this updated value.
-
Get hands-on with 1400+ tech skills courses.