计算Java项目中的比较次数

时间:2012-04-20 15:38:14

标签: java loops for-loop counting

我很想知道如何计算“插入”“删除”“搜索”和“打印”功能中的比较。该程序需要的是它可以将“学生”添加到列表中,从列表中删除它们,打印列表以及其他功能。但是,它需要能够计算插入,删除,搜索和打印时进行的比较。我知道这需要一个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; 
}

}

2 个答案:

答案 0 :(得分:0)

只要有一个反变量,比如说:

int count;

并在每次程序进行比较时递增它。

答案 1 :(得分:0)

我建议将Chain of Responsibility模式与Abstract Method结合使用。

换句话说,您的示例根本不是面向对象的。你正在进行函数式编程,这就是为什么这个计数意味着严重的代码重复(在许多地方你必须做count++)。介绍Comparison类和AbstractComparison实现链。