2012-04-20 8 views
0

"挿入" "削除" "検索"と "印刷"機能での比較回数をどのように入力するのが大好きです。このプログラムが必要とすることは、リストに「学生」を追加し、リストから削除したり、リストを印刷したり、その他の機能を追加することができます。ただし、挿入、削除、検索、および印刷時に比較が何回行われるかをカウントできる必要があります。私はこれがforループを必要としていることを知っていますが、私はそれを正しく得ることはできません。提案はありますか?Javaプロジェクトの比較数を数える

次は私のコード(警告され、それが大である)である:ちょうど

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; 
} 

}

答えて

0

、カウンタ変数を持っていると言う:

int count; 

し、それを毎回インクリメントプログラムは比較を行います。

+0

私はint countとforループが必要であることを理解していますが、どうすれば設定できますか?たとえばforループがあった場合、++をカウントアップしますが、私が姓で検索していたら何を比較するのでしょうか?姓は文字列です。 –

+0

@TrevorE 'delete ...'と 'find ...'メソッドに 'while'と' for'ループがあります。 – talnicolas

0

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

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

+0

カウントを実装する方法は? forループはどのように設定されますか? –

+0

count ++は 'ComparisonFactory#counted()'に実装され、 'AbstractComparison#counted()'から呼び出されます。 'for'ループは比較のチェーンを使用します – yegor256

+0

サンプルを見ることができますか?おそらくlastNameまたはGPAのためのものでしょうか?私はこれを把握していないだけです。 –