0

「挿入」、「削除」、「検索」、「印刷」機能で比較をカウントする方法について、いくつかの入力が欲しいです。このプログラムには、「学生」をリストに追加したり、リストから削除したり、リストを印刷したり、その他の機能を実行したりすることができます。ただし、挿入、削除、検索、および印刷時に行われた比較の数をカウントできる必要があります。これにはforループが必要ですが、正しく理解できません。何か提案はありますか?

以下は私のコードです(注意してください、それは大きいです):

import java.io.*;
import java.util.*;

import javax.swing.JOptionPane;

public class Assignment3{
static String students = ""; //Records of matching students
static int matches = 0; //Number of matching students
static Set <Student> names = new HashSet<Student>(); //HashSet to store Students

public static void main(String[] args) throws FileNotFoundException{ 

    int ID = 0;                 
    String lastName;        
    double GPA;             
    boolean run = true;

    while (run == true){
        String menu = JOptionPane.showInputDialog(null, "1) Loading students" +
                "\n2) Adding new student" +
                "\n3) Removing student " +
                "\n4) Searching students" +
                "\n5) Printing students " +
                "\n6) Quit");

        //Loading students from a text file
        if (menu.equals("1")){
            String inputFileName = JOptionPane.showInputDialog(null, "Input file:");
            File inputFile = new File(inputFileName);
            Scanner in = new Scanner(inputFile);

            while (in.hasNext()){ //Loading names from text file to array!
                ID = in.nextInt();
                lastName = in.next();
                GPA = in.nextDouble();
                Student student = new Student(ID,lastName,GPA);
                names.add(student);
            }
            in.close(); //Done loading from file, closing input!
        }

        //Adding more students to your list
        if (menu.equals("2")){
            String idString = JOptionPane.showInputDialog(null, "Please Enter an ID for student");
            ID = Integer.parseInt(idString);
            lastName = JOptionPane.showInputDialog(null, "Please enter a last name for student");
            String gpaString = JOptionPane.showInputDialog(null, "Please enter a GPA for student");
            GPA = Double.parseDouble(gpaString);
            Student student = new Student(ID,lastName,GPA);
            names.add(student);
        }

        //Remove student
        if (menu.equals("3")){
            String idString = JOptionPane.showInputDialog(null, "Please enter an ID for student to remove");
            deleteStudentByID(Integer.parseInt(idString),names);
        }

        //Search
        if (menu.equals("4")){
            String searchMenu = JOptionPane.showInputDialog(null, "1) Search by ID" +
                    "\n2) Search by name" +
                    "\n3) Search by GPA");


            if (searchMenu.equals("1")){


                int count = 0;
                for (int n=1; n<ID; n++)  
                    count++;   // insert a[n] into a[0..(n-1)]



                String idSearch = JOptionPane.showInputDialog(null, "Please enter ID number");
                JOptionPane.showMessageDialog(null, "Found " + (findStudentByID(Integer.parseInt(idSearch),names) +  " Student\n" + students));
                System.out.println("The number of comparisons is " + count );

            }
            if (searchMenu.equals("2")){
                String nameSearch = JOptionPane.showInputDialog(null, "Enter last name");
                JOptionPane.showMessageDialog(null, "Found: " + findStudentByName(nameSearch,names) + "\n" + matches + " student(s) with last name of " + nameSearch);
            }
            if (searchMenu.equals("3")){



                String gpaSearch = JOptionPane.showInputDialog(null, "Enter GPA");
                JOptionPane.showMessageDialog(null, "Found " + (findStudentByGPA(Double.parseDouble(gpaSearch),names) +  " Student(s)\n" + students));
            }
        }

        //Print
        if (menu.equals("5")){
            String printMenu = JOptionPane.showInputDialog(null, "1) Print to console" +
                    "\n2) Print to file");
            //Console
            if (printMenu.equals("1")){
                Iterator <Student> iter = names.iterator();
                while (iter.hasNext()){
                    Student tempStudent = iter.next();
                    System.out.println("ID: " + tempStudent.getID() + " Name: " + tempStudent.getName() + " GPA: " + tempStudent.getGPA() + " Hash Code: " + tempStudent.hashCode());    
                }
                JOptionPane.showMessageDialog(null,"Output to console complete");
            }
            //Text file
            if (printMenu.equals("2")){
                String outputFileName = JOptionPane.showInputDialog(null, "Output file:");
                PrintWriter out = new PrintWriter(outputFileName);
                Iterator <Student> iter = names.iterator();
                while (iter.hasNext()){
                    Student tempStudent = iter.next();
                    out.println("ID: " + tempStudent.getID() + " Name: " + tempStudent.getName() + " GPA: " + tempStudent.getGPA() + " Hash Code: " + tempStudent.hashCode());    
                }
                out.close(); //Done writing to file, close output
                JOptionPane.showMessageDialog(null,"Output saved to " + outputFileName);
            }
        }

        //Quit
        if (menu.equals("6")){
            System.exit(0);
        }
    }
}

//Delete a student
static void deleteStudentByID(int id, Set <Student> list){ 
    matches = 0;
    students = "";
    Iterator<Student> iterator = names.iterator();
    while (iterator.hasNext()) {
        Student student = iterator.next();
        if (student.getIDHash() == Integer.toString(id).hashCode()){
            matches++;
            students = "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
            list.remove(student);
            JOptionPane.showMessageDialog(null, "Deleted " + matches +  " Student\n" + students);
            break;
        }   
    }

    if (matches == 0){
        JOptionPane.showMessageDialog(null,"Student not found");
    }
}
//Search by ID
static int findStudentByID(int id,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list){
        if (student.getIDHash() == Integer.toString(id).hashCode()){
            matches++;
            students = "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
        }
    }
    return matches; 
}
//Search by name
static String findStudentByName(String name,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list) {
        if (student.getNameHash() == name.hashCode()){
            matches++;
        }
    }
    if (matches > 0){ 
        return "YES";
    }
    return "NO";
}
//Search by GPA
static double findStudentByGPA(double gpa,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list){
        if (student.getGPAHash() == Double.toString(gpa).hashCode()){
            matches++;
            students += "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
        }
    }
    return matches; 
}

}

4

2 に答える 2

0

Chain of ResponsibilityパターンをAbstract Methodと組み合わせて使用​​することをお勧めします。

つまり、あなたの例はオブジェクト指向ではありません。関数型プログラミングを行っているため、このカウントは深刻なコードの重複を意味します (多くの場所で行う必要がありますcount++)。Comparisonクラスと一連のAbstractComparison実装を紹介します。

于 2012-04-20T15:44:18.777 に答える
0

カウンター変数を用意するだけです。

int count;

プログラムが比較を行うたびにインクリメントします。

于 2012-04-20T15:39:44.267 に答える