OOPS

Assignment - 2

Objective

Write a program to take quantity & quality of fruits Mango, Apple & Papaya as input. Quality may be good, bad or average. If entered quantity of fruit is greater than 1000, store it as half. Perform search for a given fruit with given quality.

Programming Language & Compilation

$ javac Program.java
$ java Program

Source Code

import java.lang.System;
import java.util.ArrayList;
import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    // Create object instance.
    FruitsHandler fruitsHandler = new FruitsHandler();
    // Take fruits data from user.
    fruitsHandler.askInputData();
    Scanner scanner = new Scanner(System.in);
    while (true) {
      System.out.print("Enter the fruit name you want to search: ");
      String fruitName = scanner.next();
      System.out.print("Enter the fruit quality you want to search: ");
      String fruitQuality = scanner.next();
      // Show found fruit quantity on terminal.
      int foundQuantity = fruitsHandler.search(fruitName, Quality.valueOf(fruitQuality) /* Convert String to enum */);
      System.out.println(fruitName + " with quality " + fruitQuality + " are: " + foundQuantity);
    }
  }
}

// Enum to store the different types of fruit quality.
enum Quality {
  GOOD,
  BAD,
  AVERAGE
}

// Abstract class to keep a particular fruit's quantity & quality.
// This is inherited by Mango, Apple & Papaya class.
abstract class Fruit {
  public int getQuantity() {
    return this.quantity;
  }

  public Quality getQuality() {
    return this.quality;
  }

  Fruit(int quantity, Quality quality) {
    if (quantity > 1000) {
      this.quantity = quantity / 2;
    } else {
      this.quantity = quantity;
    }
    this.quality = quality;
  }

  private int quantity;
  private Quality quality;
}

class Mango extends Fruit {
  Mango(int quantity, Quality quality) {
    super(quantity, quality);
  }
}

class Apple extends Fruit {
  Apple(int quantity, Quality quality) {
    super(quantity, quality);
  }
}

class Papaya extends Fruit {
  Papaya(int quantity, Quality quality) {
    super(quantity, quality);
  }
}

// To take the quality & quality data of all fruits from user & perform
// searches in the same.
class FruitsHandler {
  private ArrayList<Fruit> fruits = new ArrayList<Fruit>();

  // Take the quality & quality data of all fruits from user.
  public void askInputData() {
    Scanner scanner = new Scanner(System.in);
    for (Quality quality : Quality.values()) {
      int quantity;
      System.out.print("Enter the quantity of " + quality + " Mangoes:");
      quantity = scanner.nextInt();
      Fruit mango = new Mango(quantity, quality);
      System.out.print("Enter the quantity of " + quality + " Apples:");
      quantity = scanner.nextInt();
      Fruit apple = new Apple(quantity, quality);
      System.out.print("Enter the quantity of " + quality + " Papayas:");
      quantity = scanner.nextInt();
      Fruit papaya = new Papaya(quantity, quality);
      fruits.add(mango);
      fruits.add(apple);
      fruits.add(papaya);
    }
  }

  // Search for the given fruit type with given quality & return its available quantity.
  public int search(String fruitName, Quality fruitQuality) {
    // Iterate over ArrayList to find the given fruit & quality, and output the
    // quantity. Linear search.
    for (Fruit fruit : fruits) {
      if (fruit.getQuality() == fruitQuality && fruit.getClass().getSimpleName()
          .equals(fruitName)) {
        return fruit.getQuantity();
      }
    }
    return -1;
  }
}

Output

right-click & select “Open image in new tab” for larger image.