2012-03-28 15 views
0

私はData Strcuturesのための簡単なプロジェクトを終了しようとしていますが、私のプログラムでは常に表示され続けるエラーがあります。私はそれらのいくつかを修正したが、私はあきらめたいと思うものがある。多分あなたたちは私が解決策を見つけるのを助けることができましたか?ここで保存された値が常に私のプログラムで消去されるのはなぜですか?

は私のコードは、(クラスLogInSystem)です:プログラムのバグは、私が予約したときにということである

// NMQ 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class cineplexError { 
    private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    private static String integerReprompt = "Invalid integer. Try again: "; 
    private static String doubleReprompt = "Invalid double. Try again: "; 
    private static String charReprompt = "Invalid character. Try again: "; 

    public static String readString() { 
     String s = null; 
     try { 
      s = in.readLine(); 
     } catch (IOException ex) {} 
     return s; 
    } 

    public static char readChar() { 
     char c = 0; 
     String s = null; 
     try { 
      s = in.readLine(); 
     } catch (IOException ex) {} 
       if (s.length() == 1) { 
        c = s.charAt(0); 
//     valid = true; 
       } else { 
        System.out.print(charReprompt); 
       } 
     return c; 
    } 

    public static int readInt() { 
     int i = 0; 

      boolean valid = false; 

      try { 
        i = Integer.parseInt(in.readLine()); 
      } catch (IOException ex) { 
       System.out.print(integerReprompt); 
       } 
        valid = true; 



     return i; 
    } 

    public static double readDouble() { 
     double d = 0.0; 

     boolean valid = true; 

     try { 
        d = Double.parseDouble(in.readLine()); 
     } catch (IOException ex) {} 
        valid = true; 
        valid = false; 
        System.out.print(doubleReprompt); 


     return d; 
    } 

    public void pause() { 
      System.out.print("Press enter to continue..."); 
      try { 
      in.readLine(); 
      } catch (IOException ex) {} 
     } 

    public static void setIntegerReprompt(String prompt) { 
     integerReprompt = prompt; 
    } 

    public void setDoubleReprompt(String prompt) { 
     doubleReprompt = prompt; 
    } 

    public void setCharReprompt(String prompt) { 
     charReprompt = prompt; 
    } 

} 

:ここ

// NMQ 

    import java.util.ArrayList; 
    import java.util.Scanner; 
    import java.util.Vector; 

public class LogInSystem { 
    private static final int MAX_SEATS = 10; 

    Vector<String> username = new Vector<String>(); 
    ArrayList<String> password = new ArrayList<String>(); 

    int p; 
    String Username, Password, rUsername, rPassword; 

    private Scanner input = new Scanner(System.in); 


    public LogInSystem() { 
     loginScreen(); 
    } 
    private void loginScreen() { 
     boolean done = false; 

     do { 
      printMainMenu(); 
      int choice = getMainMenuChoice(); 

      switch (choice) { 
      case 1: // Log In 
       logIn(); 
       break; 

      case 2: // To Register 
       register(); 
       break; 

      case 3: // Exit 
       done = true; 
       break; 
      } 

     } while (!done); 
    } 

    //Registration 

    private void register() { 
     System.out.println("Input a Username"); 
     Username = input.next(); 
     System.out.println("Input a Password"); 
     Password = input.next(); 
     System.out.println("... Registered!"); 

    } 

    //Log In 

    private void logIn() { 
     p = 0; 
     System.out.println("Input a Username"); 
     rUsername = input.next(); 
     System.out.println("Input a Password"); 
     rPassword = input.next(); 

     // If the log in is successful it will got to the next menu 

     if(rUsername.equals(Username)&& rPassword.equals(Password)) 
     { 
      System.out.println("... Logging In"); 
      boolean done = false; 

      String[] seats = new String[MAX_SEATS]; // the item list 
      initializeItems(seats); 
     // Printing of 2nd menu 
      do { 
       printMainMenu2(); 
       int choice = getMainMenuChoice2(); // choice off of the main menu 

       switch (choice) { 
       case 1: // Adding Seat 
        addSeat(seats); 
        break; 
       case 2: // Viewing Seat List 
        viewSeatList(seats); 
        break; 
       case 3: // Exit 
        done = true; 
        break; 
       } 

      } while (!done); 
     } 

     else 
      System.out.println("Invalid log in, please try again."); 

    } 
    private int getMainMenuChoice() { 
     int choice; // choice entered 
     boolean valid = false; // is choice valid? 

     do { 
      System.out.print(">>>>> "); 
      choice = cineplexError.readInt(); 

      if (1 <= choice && choice <= 4) { 
       valid = true; 
      } else { 
       System.out.println("Invalid choice."); 
      } 
     } while (!valid); 

     return choice; 
    } 
    private void printMainMenu() { 
     System.out.println("\nMain Menu\n"); 
     System.out.println("Press 1 to Log In"); 
     System.out.println("Press 2 to Register"); 
     System.out.println("Press 3 to Exit"); 
     System.out.println(); 
    } 
    public static void main(String[] args) { 
     new LogInSystem(); 
    } 

    int getMainMenuChoice2() { 
     int choice; // choice entered 
     boolean valid = false; // is choice valid? 

     do { 
      System.out.print(">>>>> "); 
      choice = cineplexError.readInt(); 

      if (1 <= choice && choice <= 4) { 
       valid = true; 
      } else { 
       System.out.println("Invalid choice."); 
      } 
     } while (!valid); 

     return choice; 
    } 
    void printMainMenu2() { 
     System.out.println("\nMain Menu\n"); 
     System.out.println("Press 1 to Reserve a Seat"); 
     System.out.println("Press 2 to View the Seat List"); 
     System.out.println("Press 3 to Exit Buying"); 
     System.out.println(); 
    } 
    void initializeItems(String[] seats) { 
     for (int i = 0; i < seats.length; i++) { 
      seats[i] = ""; 
     } 
    } 
    void addSeat(String[] seats) { 
      int seatIndex = findEmptySeatList(seats); // index of first empty item list 
      if (seatIndex == seats.length) { 
       System.out.println("Seat is already occupied. Sorry."); 
      } 

      else { 
       String seat = getSeatName(); // seat's name 
       seats[seatIndex] = seat; 
       System.out.println(seat + " is on seat list #" 
         + (seatIndex + 1)); 
      } 
     } 
    int findEmptySeatList(String[] seat) { 
     for (int i = 0; i < seat.length; i++) { 
      if (isEmpty(seat, i)) { 
       return i; 
      } 
     } 

     return seat.length; 
    } 
    boolean isEmpty(String[] seats, int seatIndex) { 
     return seats[seatIndex].equals(""); 
    } 
    String getSeatName() { 
     System.out.print("Enter the name of reservator: "); 
     return cineplexError.readString(); 
    } 
    void viewSeatList(String[] seats) { 
     System.out.println("\nSeat List\n"); 
     for (int i = 0; i < seats.length; i++) { 
      System.out.println((i + 1) + ". " + seats[i]); 
     } 
    } 
} 

は、他のクラス(cineplexError)でありますもう一度ログインすると、座席リストを見ると、予約者の名前または座席を予約している人の名前が消去されています。値を永続的に格納するコントロールステートメントを置くべきですか?あなたは私を助けることができます?おかげさまで

答えて

1
Line 73: String[] seats = new String[MAX_SEATS]; // the item list 
Line 74: initializeItems(seats); 

logInを呼び出すたびに、すべての空の文字列にシートのリストを再初期化します。その初期化はコンストラクタで行う必要があります。したがって、行73(String []席の宣言)をクラスのインスタンス変数に移動し、行74をコンストラクタに移動します。

+0

私は間違ったことを見ます。ありがとうございます! – Coolai

1

あなたは、座席をパスワードとユーザー名としてクラスのメンバーにする必要があります。関数内でのみ定義されるようになりました。関数呼び出しの終わりには、それは破棄されます。

+0

ところで、誰がこれらのクラスのインスタンスを作成していますか?主な実行可能クラスはどこですか? – tartar

+0

"あなたは座席をパスワードとユーザー名としてクラスのメンバにする必要があります。これは今では関数内でのみ定義されているので、関数呼び出しの最後には破棄されます。それは私がやろうとしていることですが、私はどのように考えているのか分かりません。主なクラスはLogInSystemです。 – Coolai

+1

行10-16では、クラスのインスタンス変数の束を定義します。座席をインスタンス変数にするには、行73を行16の直後に移動してください。 – webjprgm

関連する問題