简体   繁体   English

为什么我的程序中始终会删除存储的值

[英]Why is the stored value always erased in my program

Ok, so I'm trying to finish our simple project for Data Strcutures but there is always some error that just keep showing up on my program. 好的,所以我正在尝试完成Data Strcutures的简单项目,但总会出现一些错误,只是一直出现在我的程序上。 I have fixed some of them but there is this one that makes me want to give up. 我已经修复了其中一些,但有一个让我想放弃。 Maybe you guys could help me find the solution? 也许你们可以帮我找到解决方案?

Here is my code (class LogInSystem): 这是我的代码(类LogInSystem):

    // 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]);
        }
    }
}

Here is the other class (cineplexError): 这是另一个类(cineplexError):

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

 }

The bug in the program is that whenever I reserve a seat and log out and log in again the name of the reservator or the name of the one that reserves a seat is being erased when I view the seat list. 该程序中的错误是,每当我预订座位并注销并再次登录时,我查看座位列表时,保留座位的名称或保留座位的名称将被删除。 Should I put a control statement that makes the value stored permanently? 我应该放置一个控制语句,使值永久存储吗? Can you help me out? 你能帮我吗? Thanks. 谢谢。

you need to make seats a member of class as password and username. 你需要让座位成为密码和用户名的成员。 it is now only defined within a function, so at the end of function call it is destroyed. 它现在只在一个函数中定义,所以在函数调用结束时它被销毁。

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

Every time you call logIn you are re-initializing the list of seats to all empty strings. 每次调用logIn时,您都会重新初始化所有空字符串的席位列表。 That initialization should be done in the constructor. 初始化应该在构造函数中完成。 So move Line 73 (declaration of String[] seats) to be an instance variable of the class, and move Line 74 to be in the constructor. 因此,将第73行(String []席位的声明)移动为该类的实例变量,并将第74行移动到构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM