Exercise 2: Expired or Not?

In this exercise, you have to compare the expiry dates of two products and, based on that, suggest which brand's product should be bought.

Problem Statement

In this exercise, you are given a class Product. You need to implement the following tasks:

Task 1

Define the constructor which takes and initializes the following properties:

  • name: the name of the product.

  • price the price of the product.

  • amount: the amount of product available in inventory.

  • madeIn: the country in which the product is made in.

  • expiryDate: the expiry date of the product.

  • brand: the name of the brand that made the product.

Task 2

Define a static function checkExpiry(product1,product2). Here’s what it should do:

  • It should take two product objects of the same type but from different brands as parameters.

  • It should compare their expiryDate with the current date.

  • Based on the difference of the expiryDate from the current date it should return which brand’s product to buy, i.e., return the brand property of that product.

    • It should return Neither if both products have expired.

    • It should return brand property of product1 if product2 has expired, but product1 hasn’t.

    • It should return brand property of product2 if product1 has expired, but product2 hasn’t.

    • If neither of the products has expired it should compute the difference of both of their expiry dates from the current date and return the brand of the product whose difference is greater.

    • It should return Either if both products have the same expiry date.

Get hands-on with 1200+ tech skills courses.