OneCompiler

PRA2 student, contactbook

137

import java.util.*;

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

    int n = sc.nextInt();
    List<Contact> contacts = new ArrayList<>();
    
    // Input Contacts
    for (int i = 0; i < n; i++) {
        int id = sc.nextInt();
        sc.nextLine();
        String name = sc.nextLine();
        String type = sc.nextLine();
        int noOfContacts = sc.nextInt();
        Set<Integer> numbers = new TreeSet<>(); // Collect all numbers
        
        for (int j = 0; j < noOfContacts; j++) {
            int num = sc.nextInt();
            numbers.add(num);
        }
        contacts.add(new Contact(id, name, type, numbers));
    }
    
    // Input PhoneBooks
    int noOfCategories = sc.nextInt();
    List<PhoneBook> phoneBooks = new ArrayList<>();
    
    for (int i = 0; i < noOfCategories; i++) {
        sc.nextLine();
        String categoryName = sc.nextLine();
        int numContactsInCategory = sc.nextInt();
        List<Contact> contactList = new ArrayList<>();
        
        for (int j = 0; j < numContactsInCategory; j++) {
            int cid = sc.nextInt();
            for (Contact c : contacts) {
                if (c.contactID == cid) {
                    contactList.add(c); 
                }
            }
        }
        phoneBooks.add(new PhoneBook(categoryName, contactList));
    }

    sc.nextLine();
    
    // Query 1: Search Contact by Name
    String queryName = sc.nextLine();
    List<Contact> res1 = getContactByName(contacts, queryName);
    if (res1 != null) {
        for (Contact contact : res1) {
            System.out.println(contact.contactID);
            System.out.println(contact.Name);
            System.out.println(contact.contactType);
            for (Integer number : contact.phoneNumbers) { // Print all numbers
                System.out.println(number);
            }
        }
    } else {
        System.out.println("Not Found!");
    }

    // Query 2: Contacts in Category
    String categoryQuery = sc.nextLine();
    Set<String> res2 = contactsInCategory(phoneBooks, categoryQuery);
    if (res2 != null) {
        System.out.println(String.join(", ", res2));
    } else {
        System.out.println("No contacts Found!");
    }

    sc.close();
}

// Find contact by name
public static List<Contact> getContactByName(List<Contact> contacts, String name) {
    List<Contact> results = new ArrayList<>();
    for (Contact c : contacts) {
        if (c.Name.equalsIgnoreCase(name)) {
            results.add(c);
        }
    }
    return results.isEmpty() ? null : results;
}

// Find contacts in category
public static Set<String> contactsInCategory(List<PhoneBook> phoneBooks, String category) {
    TreeSet<String> names = new TreeSet<>();
    for (PhoneBook pb : phoneBooks) {
        if (pb.categoryName.equalsIgnoreCase(category)) {
            for (Contact c : pb.contactList) {
                names.add(c.Name);
            }
        }
    }
    return names.isEmpty() ? null : names;
}

}

class Contact {
int contactID;
String Name;
String contactType;
Set<Integer> phoneNumbers;

public Contact(int contactID, String name, String contactType, Set<Integer> phoneNumbers) {
    this.contactID = contactID;
    this.Name = name;
    this.contactType = contactType;
    this.phoneNumbers = phoneNumbers;
}

}

class PhoneBook {
String categoryName;
List<Contact> contactList;

public PhoneBook(String categoryName, List<Contact> contactList) {
    this.categoryName = categoryName;
    this.contactList = contactList;
}

}

==========================================================================

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of students
sc.nextLine(); // Consume newline

    Student[] students = new Student[n];

    // Input student details
    for (int i = 0; i < n; i++) {
        int id = sc.nextInt();
        sc.nextLine();
        String name = sc.nextLine();
        String branch = sc.nextLine();
        double percentage = sc.nextDouble();
        if (sc.hasNextLine()) sc.nextLine(); // Consume newline
        students[i] = new Student(id, name, branch, percentage);
    }

    // Input branch to search
    String branchToSearch = sc.nextLine();

    // Fetch students of the given branch
    List<Student> branchStudents = getStudentsByBranch(students, branchToSearch);

    if (branchStudents.isEmpty()) {
        System.out.println("No Students Found");
    } else {
        double averagePercentage = calculateAveragePercentage(branchStudents);
        System.out.println("Average Percentage-" + averagePercentage);

        // Print student details
        for (Student student : branchStudents) {
            System.out.println("studentId-" + student.getId());
            System.out.println("studentName-" + student.getName());
            System.out.println("studentBranch-" + student.getBranch());
            System.out.println("studentPercentage-" + student.getPercentage());
        }
    }

    // Print a second "No Students Found" if the branch students list is empty
    if (branchStudents.isEmpty()) {
        System.out.println("No Students Found");
    }
}

// Method to get students by branch
public static List<Student> getStudentsByBranch(Student[] students, String branch) {
    List<Student> branchStudents = new ArrayList<>();
    for (Student student : students) {
        if (student.getBranch().equalsIgnoreCase(branch)) {
            branchStudents.add(student);
        }
    }
    return branchStudents;
}

// Method to calculate average percentage for a list of students
public static double calculateAveragePercentage(List<Student> students) {
    if (students.isEmpty()) return 0.0;
    double total = 0.0;
    for (Student student : students) {
        total += student.getPercentage();
    }
    return total / students.size();
}

}

// Student class with attributes and methods
class Student {
private int id;
private String name;
private String branch;
private double percentage;

public Student(int id, String name, String branch, double percentage) {
    this.id = id;
    this.name = name;
    this.branch = branch;
    this.percentage = percentage;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public String getBranch() {
    return branch;
}

public double getPercentage() {
    return percentage;
}

}

============================================================