0% found this document useful (0 votes)
25 views3 pages

Java Java

java code

Uploaded by

rg361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Java Java

java code

Uploaded by

rg361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

class MyClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Creating an array of 4 laptops


Laptop[] laptops = new Laptop[4];
for (int i = 0; i < 4; i++) {
// Reading the input for each laptop
int laptopId = Integer.parseInt(sc.nextLine());
String brand = sc.nextLine();
String osType = sc.nextLine();
double price = Double.parseDouble(sc.nextLine());
int rating = Integer.parseInt(sc.nextLine());

// Creating a Laptop object and adding it to the array


laptops[i] = new Laptop(laptopId, brand, osType, price, rating);
}

// Reading brand and OS type to search


String brandToSearch = sc.nextLine();
String osToSearch = sc.nextLine();

// Count laptops by brand


int count = countOfLaptopsByBrand(laptops, brandToSearch);
if (count > 0) {
System.out.println(count);
} else {
System.out.println("The given brand is not available");
}

// Search laptops by OS type


Laptop[] laptopsByOs = searchLaptopByOsType(laptops, osToSearch);
if (laptopsByOs != null) {
for (Laptop laptop : laptopsByOs) {
System.out.println(laptop.getLaptopId()); // Print laptopId
System.out.println(laptop.getRating()); // Print rating
}
} else {
System.out.println("The given os is not available");
}

sc.close();
}

// Method to count laptops by brand with a rating more than 3


public static int countOfLaptopsByBrand(Laptop[] laptops, String brand) {
if (laptops == null || laptops.length == 0) {
return 0;
}

int count = 0;
for (Laptop laptop : laptops) {
// Check for matching brand (case insensitive) and rating greater than
3
if (laptop.getBrand().equalsIgnoreCase(brand) && laptop.getRating() >
3) {
count++;
}
}
return count;
}

// Method to search laptops by OS type and return them in descending order of


laptop ID
public static Laptop[] searchLaptopByOsType(Laptop[] laptops, String osType) {
if (laptops == null || laptops.length == 0) {
return null;
}

List<Laptop> matchingLaptops = new ArrayList<>();


for (Laptop laptop : laptops) {
// Case insensitive matching for osType
if (laptop.getOsType().equalsIgnoreCase(osType)) {
matchingLaptops.add(laptop);
}
}

// If no matching laptops, return null


if (matchingLaptops.isEmpty()) {
return null;
}

// Sort the matching laptops in descending order of laptopId


matchingLaptops.sort((l1, l2) -> Integer.compare(l2.getLaptopId(),
l1.getLaptopId()));

// Convert the list to an array and return it


return matchingLaptops.toArray(new Laptop[0]);
}
}

// Laptop class with attributes and getter methods


class Laptop {
private int laptopId;
private String brand;
private String osType;
private double price;
private int rating;

public Laptop(int laptopId, String brand, String osType, double price, int
rating) {
this.laptopId = laptopId;
this.brand = brand;
this.osType = osType;
this.price = price;
this.rating = rating;
}

public int getLaptopId() {


return laptopId;
}

public String getBrand() {


return brand;
}
public String getOsType() {
return osType;
}

public double getPrice() {


return price;
}

public int getRating() {


return rating;
}
}

You might also like