TCS OPA (JAVA) 31 July 2020

Mobile Details Management

Create a class Mobile with the below private attributes:
iMEICode – String
isSingleSIM – boolean
processor – String
price – double
manufacturer – String

Write getters and setters as required

Create class Solution (here I used Main) with main method. Implement two static methods –
getCountOfValidIMEIMobiles and findMobileWithMaxPrice in Solution class

getCountOfValidIMEIMobiles:
This method will take array of Mobile objects as input parameter and return the total count of mobiles that has iMEICode with 15 digits and has single SIM provision.
If no mobiles satisfy the given conditions, return 0

findMobileWithMaxPrice:
This method will take an array of Mobile objects, double parameter – discountPercentage, and a String parameter – manufacturerName as inputs.
The method will update price of all mobiles of the given manufacturer name by applying the given discountPercentage. it will return the mobile object of the given manufacturer that has the maximum price after applying discount. This method would return null if the above conditions not satisfy.
Finding newPrice,
newPrice = old price – (old price * discountPercentage)/100;

Note:
1. All String comparisons are case insensitive
2. A mobile has single SIM provision if the attribute isSingleSIM has a true value
3. IMEICode of mobiles can begin with 0. eg: 000008888888999 is a IMEICode with 15 digits.
4. All mobiles provided in the input have unique price.

The above mentioned static methods should be called from the main method.
For getCountOfValidIMEIMobiles, the main method should print the count of mobiles as it is.
For findMobileWithMaxPrice, the main method should print the IMEICode and price of the returned Mobile object. eg; 000008888888999@4421653. If the returned object is null, then it should print “No mobile found”

Input:
090909090909088
true
Kirin 990 4G
58955
Xiaomi Redmi 6A
888887777755555
true
Exynos 990
76555
Samsung Galaxy 520 Ultra
998899889988
false
Exynos 990
14000
Samsung Galaxy S20
989898989898989
true
Kirin 990 4G
30000
Huawai Honor 7
99999777778888
true
MT6732
58900
Xiaomi Redmi 6A
25
Xiaomi Redmi 6A
Output:
3
090909090909088@44216.25


Answer: 

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
public static void main(String[] args)
 {
  //code to read values 
  //code to call required method
  //code to display the result
    Mobile[] listOfMobiles=new Mobile[5];
     Scanner sc=new Scanner(System.in);
           for (int i=0;i<5;i++){
           System.out.println("iMEICODE:");
           String iMEICODE=sc.nextLine();
           System.out.println("isSIgnleSIM:");
           boolean isSIgnleSIM=sc.nextBoolean();sc.nextLine();
           System.out.println("processor:");
           String processor=sc.nextLine();
           System.out.println("Price:");
           double price=sc.nextDouble();sc.nextLine();
           System.out.println("manufacturer:");
           String manufacturer =sc.nextLine();  
           listOfMobiles[i]=new Mobile(iMEICODE,isSIgnleSIM,processor,price,manufacturer);
           }
        double discountPercentage=sc.nextDouble();sc.nextLine();
        String manucaturereName=sc.nextLine();   
        int output1=getCountOfValidIMEIMobiles(listOfMobiles);
        System.out.println(output1);   

        Mobile output2= findMobileWithMaxPrice(listOfMobiles,discountPercentage,manucaturereName);
        System.out.println(output2.getiMEICODE()+"@"+output2.getPrice());


 }

 public static int getCountOfValidIMEIMobiles(Mobile[] listOfMobiles)
  {
   //method logic
    int count=0;
    for(int i=0;i<5;i++){
          if(listOfMobiles[i].getiMEICODE().length()==15 && listOfMobiles[i].getisSIgnleSIM()==true){
              count++;  
          }
      }
      return count;
  }

public static Mobile findMobileWithMaxPrice(Mobile[] listOfMobiles,double 
discountPercentage,String manufacturerName)
  {
   //method logic
   double maxPrice=0;
   Mobile mobile2=null;
   for(int i=0;i<5;i++){
        double updatedPrice= listOfMobiles[i].getPrice()-((listOfMobiles[i].getPrice()*discountPercentage)/100); 
        listOfMobiles[i].setPrice(updatedPrice);   
        if(listOfMobiles[i].getManufacturer().equalsIgnoreCase(manufacturerName)&&listOfMobiles[i].getPrice()>maxPrice){
            mobile2=listOfMobiles[i];
            break;
        }
      }
        return mobile2;
  }
}
class Mobile
{
  //code to build the class
  private String iMEICODE;
  private boolean isSIgnleSIM;
  private String processor;
  private double price;
  private String manufacturer;

  public Mobile(String iMEICODE, boolean isSIgnleSIM,String processor, double price,String manufacturer) {
        super();
        this.iMEICODE = iMEICODE;
        this.isSIgnleSIM = isSIgnleSIM;
        this. processor = processor;
        this.price = price;
        this.manufacturer =manufacturer;
    }
    public String getiMEICODE() {
        return iMEICODE;
    }
    public void setiMEICODE(String iMEICODE) {
        this.iMEICODE = iMEICODE;
    }
    public boolean getisSIgnleSIM() {
        return isSIgnleSIM;
    }
    public void setisSIgnleSIM(boolean isSIgnleSIM) {
        this.isSIgnleSIM =isSIgnleSIM;
    }
    public String getProcessor() {
        return processor;
    }
    public void setProcessor(String processor) {
        this.processor=processor;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getManufacturer() {
        return manufacturer;
    }
    public void setmanufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

}

Comments

Popular posts from this blog

PEEPING INTO ARTISTIC LANES AT AMDAVAD NI GUFA

Angular 16 : The Best Features and How to Use Them