venkivijay / Java-Solutions-TCS-Xplore-Proctored-Assessment

Java Solution to the TCS Xplore Proctored Assessment 2020
https://xplore.venkivijay.com
MIT License
248 stars 140 forks source link

Hey we can also do this question Like this: #18

Open sksharma4u opened 1 year ago

sksharma4u commented 1 year ago

import java.util.*; public class Main {

public static void main(String[] args) { Scanner sc = new Scanner(System.in); Phone[] obj = new Phone[4]; for(int i=0 ;i<4 ; i++){ obj[i] = new Phone(sc.nextInt() , sc.next() , sc.next(), sc.nextInt()); } String brand = sc.next(); String os = sc.next();

int value = findPriceForGivenBrand(obj , brand);

if(value > 0){
    System.out.println(value);
}else{
    System.out.println("The given Brand is not available");
}

Phone[] ans = getPhoneIdBasedOnOs(obj , os);
if(ans.length==0){
    System.out.println("No phones are available with specified os and price range");
}else{
    for(int i=0 ; i<ans.length ; i++){
        System.out.println(ans[i].getid());
    }
}

}

public static int findPriceForGivenBrand(Phone[] phone, String brand) { int sum =0 ;

for(int i=0 ; i<phone.length; i++){
    if(phone[i].getbrand().equalsIgnoreCase(brand)){
        sum+=phone[i].getprice();
    }
}
return sum;

}

public static Phone[] getPhoneIdBasedOnOs(Phone[] phone, String os) { int length =0; int j =0; Phone[] obj2 = new Phone[length]; for(int i=0 ; i<phone.length ; i++){ if(phone[i].getos().equalsIgnoreCase(os) && phone[i].getprice()>=50000){ obj2 = new Phone[length+1]; obj2[j] = phone[i]; j++; } } return obj2; } }

class Phone { int id; String os ; String brand; int price;

public int getid(){ return id; }

public String getos(){ return os; }

public String getbrand(){ return brand; }

public int getprice(){ return price; }

Phone(int id , String os , String brand , int price){ this.id = id; this.os = os; this.brand = brand; this.price = price; } }

sksharma4u commented 1 year ago

I think it's easy to understand and feasible to write

Abhijit06 commented 1 year ago

import java.util.*; public class Main {

public static void main(String[] args) { Scanner sc = new Scanner(System.in); Phone[] obj = new Phone[4]; for(int i=0 ;i<4 ; i++){ obj[i] = new Phone(sc.nextInt() , sc.next() , sc.next(), sc.nextInt()); } String brand = sc.next(); String os = sc.next();

int value = findPriceForGivenBrand(obj , brand);

if(value > 0){
    System.out.println(value);
}else{
    System.out.println("The given Brand is not available");
}

Phone[] ans = getPhoneIdBasedOnOs(obj , os);
if(ans.length==0){
    System.out.println("No phones are available with specified os and price range");
}else{
    for(int i=0 ; i<ans.length ; i++){
        System.out.println(ans[i].getid());
    }
}

}

public static int findPriceForGivenBrand(Phone[] phone, String brand) { int sum =0 ;

for(int i=0 ; i<phone.length; i++){
    if(phone[i].getbrand().equalsIgnoreCase(brand)){
        sum+=phone[i].getprice();
    }
}
return sum;

}

public static Phone[] getPhoneIdBasedOnOs(Phone[] phone, String os) { int length =0; int j =0; Phone[] obj2 = new Phone[length]; for(int i=0 ; i<phone.length ; i++){ if(phone[i].getos().equalsIgnoreCase(os) && phone[i].getprice()>=50000){ obj2 = new Phone[length+1]; obj2[j] = phone[i]; j++; } } return obj2; } }

class Phone { int id; String os ; String brand; int price;

public int getid(){ return id; }

public String getos(){ return os; }

public String getbrand(){ return brand; }

public int getprice(){ return price; }

Phone(int id , String os , String brand , int price){ this.id = id; this.os = os; this.brand = brand; this.price = price; } }

Thanku Sir, it was helpful for me and easy to understand.

venkivijay commented 1 year ago

@sksharma4u Please feel free to raise a PR for this. Checkout the contributing guidelines