ANGADJAVA Java Interview Question Using OOPs Concept: Write Java example Retrieve Top 5 Mobiles for Each Brand

Using OOPs Concept: Write Java example Retrieve Top 5 Mobiles for Each Brand

0 Comments 6:36 pm

interview, job, icon-1018332.jpg

In this blog I’ll share a Java example that uses object-oriented programming to find the top 5 mobile phones for each brand. Imagine we have a ‘Mobile‘ class representing each phone with basic attributes like brand, model, and price. Additionally, we’ll create a ‘MobileManager‘ class to handle these phones and fetch the top 5 phones for each brand.”

Here’s an example implementation:

import java.util.*;

class Mobile {
    private String brand;
    private String model;
    private double price;

    public Mobile(String brand, String model, double price) {
        this.brand = brand;
        this.model = model;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return brand + " " + model + " - $" + price;
    }
}

class MobileManager {
    private List<Mobile> mobiles;

    public MobileManager() {
        mobiles = new ArrayList<>();
    }

    public void addMobile(Mobile mobile) {
        mobiles.add(mobile);
    }

    public Map<String, List<Mobile>> getTop5MobilesByBrand() {
        Map<String, List<Mobile>> top5ByBrand = new HashMap<>();
        for (Mobile mobile : mobiles) {
            top5ByBrand.putIfAbsent(mobile.getBrand(), new ArrayList<>());
            List<Mobile> brandMobiles = top5ByBrand.get(mobile.getBrand());
            if (brandMobiles.size() < 5) {
                brandMobiles.add(mobile);
                brandMobiles.sort(Comparator.comparingDouble(Mobile::getPrice));
            } else {
                if (mobile.getPrice() > brandMobiles.get(0).getPrice()) {
                    brandMobiles.remove(0);
                    brandMobiles.add(mobile);
                    brandMobiles.sort(Comparator.comparingDouble(Mobile::getPrice));
                }
            }
        }
        return top5ByBrand;
    }
}

public class Main {
    public static void main(String[] args) {
        MobileManager manager = new MobileManager();

        // Adding mobile phones
        manager.addMobile(new Mobile("Samsung", "Galaxy S21", 999.99));
        manager.addMobile(new Mobile("Samsung", "Galaxy Note 20", 899.99));
        manager.addMobile(new Mobile("Samsung", "Galaxy A52", 399.99));
        manager.addMobile(new Mobile("Apple", "iPhone 12 Pro", 1099.99));
        manager.addMobile(new Mobile("Apple", "iPhone SE", 399.99));
        manager.addMobile(new Mobile("Apple", "iPhone 11", 699.99));
        manager.addMobile(new Mobile("Xiaomi", "Redmi Note 10", 299.99));
        manager.addMobile(new Mobile("Xiaomi", "Mi 11", 799.99));
        manager.addMobile(new Mobile("Xiaomi", "POCO X3", 249.99));

        // Getting top 5 mobiles for each brand
        Map<String, List<Mobile>> top5ByBrand = manager.getTop5MobilesByBrand();

        // Printing top 5 mobiles for each brand
        for (String brand : top5ByBrand.keySet()) {
            System.out.println("Top 5 mobiles for " + brand + ":");
            List<Mobile> top5Mobiles = top5ByBrand.get(brand);
            for (Mobile mobile : top5Mobiles) {
                System.out.println(mobile);
            }
            System.out.println();
        }
    }
}
// OUTPUT : 
/** Top 5 mobiles for Apple:
Apple iPhone SE - $399.99
Apple iPhone 11 - $699.99
Apple iPhone 12 Pro - $1099.99

Top 5 mobiles for Xiaomi:
Xiaomi POCO X3 - $249.99
Xiaomi Redmi Note 10 - $299.99
Xiaomi Mi 11 - $799.99

Top 5 mobiles for Samsung:
Samsung Galaxy A52 - $399.99
Samsung Galaxy Note 20 - $899.99
Samsung Galaxy S21 - $999.99  */

This code defines a Mobile class to represent each mobile phone, a MobileManager class to manage mobile phones, and the Main class to demonstrate the usage of the MobileManager class by adding some mobile phones and getting the top 5 mobiles for each brand.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *