TCS Xplore IRA Java Solution
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[] ) throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ Scanner sc=new Scanner(System.in); int n =sc.nextInt(); Customer c[] = new Customer[n]; for(int i=0;i < n;i++){ int id = sc.nextInt(); sc.nextLine(); String name = sc.nextLine(); int loyaltyPoints = sc.nextInt(); c[i] = new Customer(id,name,loyaltyPoints); } int id=sc.nextInt(); Customer c1=getCustomerById(c, id); Customer c2=findCustomer(c); if(c1 != null){ System.out.println("Id-"+c1.getId()); System.out.println("Name-"+c1.getName()); System.out.println("LoyaltyPoints-"+c1.getLoyaltyPoints()); } else{ System.out.println("No Customer found with mentioned attribute"); } if(c2 != null){ System.out.println("Id-"+c2.getId()); System.out.println("Name-"+c2.getName()); System.out.println("LoyaltyPoints-"+c2.getLoyaltyPoints()); } else{ System.out.println("No Customer found with mentioned attribute"); } } public static Customer getCustomerById(Customer c[],int id){ for(Customer c2:c){ if(c2.getId() == id){ return c2; } } return null; } public static Customer findCustomer(Customer c[]){ int max=0; for(Customer c2:c){ if(c2.getLoyaltyPoints() > max){ max = c2.getLoyaltyPoints(); } } for(Customer c2:c){ if(c2.getLoyaltyPoints() == max){ return c2; } } return null; } } class Customer{ private int id; private String name; private int loyaltyPoints; public int getId(){ return id; } public String getName(){ return name; } public int getLoyaltyPoints(){ return loyaltyPoints; }
Comments
Post a Comment