Filtering of Records


Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

請撰寫一個程式,由使用者輸入五位學生的修課成績,每位學生都有三個欄位,分別是姓名、課程名稱、成績等第,
輸入完畢之後,指定某個要篩選的科目名稱、以及某個成績等第,由程式依照這個篩選條件,從五位學生中篩選出在該指定科目獲得指定成績等第的學生,最後印出這幾位學生的姓名。
如果沒有符合條件的學生,則僅輸出Not found訊息。

可以利用以下的參考程式碼作為基礎,做小幅度的修改(可能在10行以內),讓它可以滿足以上需求。
若修改正確,當程式獲得以下每一組範例輸入時,應該產生該範例的對應輸出結果。


範例輸入#1

jack math a
alice math b
bob english a
alice english c
john math b
math b

範例輸出#1

alice
john

範例輸入#2

mary biology a
jack english a
alice biology c
bob biology a
jack math b
biology a

範例輸出#2

mary
bob

範例輸入#3

mary biology b
jack english a
alice biology c
bob biology b
jack math b
biology a

範例輸出#3

Not found

參考程式碼
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class MyProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<HashMap<String, String>> records = DataCollector.getInputData(scanner, 5);

        // Get the course name and grade from the user
        String courseName = scanner.next();
        String grade = scanner.next();

        scanner.close();

        // Create an instance of DataAnalyzer and print records
        DataAnalyzer analyzer = new DataAnalyzer(records);
        ArrayList<String> names = analyzer.filterRecords(courseName, grade);

        // Print the names of students with the specified grade in the specified course
        if (names.isEmpty()) {
            System.out.println("Not found");
        } else {
            for (String name : names) {
                System.out.println(name);
            }
        }
    }
}

class DataCollector {
    // Static method to get input data from the user
    public static ArrayList<HashMap<String, String>> getInputData(Scanner scanner, int numberOfRecords) {
        ArrayList<HashMap<String, String>> records = new ArrayList<>();
        for (int i = 0; i < numberOfRecords; i++) {
            HashMap<String, String> record = new HashMap<>();
            String name = scanner.next();
            String course = scanner.next();
            String grade = scanner.next();
            record.put("name", name);
            record.put("course", course);
            record.put("grade", grade);
            records.add(record);
        }
        return records;
    }
}

class DataAnalyzer {
    private ArrayList<HashMap<String, String>> records;

    // Constructor to initialize records
    public DataAnalyzer(ArrayList<HashMap<String, String>> records) {
        this.records = records;
    }
}

Comments

There are no comments at the moment.