Java 读取文本文件,检查正确信息,然后显示

Java 读取文本文件,检查正确信息,然后显示,java,bufferedreader,Java,Bufferedreader,因此,我已经成功地将一组伪造银行账户信息的用户添加到一个文本文件中。现在我需要某种方法把它们读回我的程序 例如: 如果我输入用户名-mattsmith 和PIN-1234 Matt Smith的其他后台帐户信息将显示在控制台中,我希望能够执行诸如添加资金、检查余额和取款等操作 最好的方法是什么 这是我目前掌握的代码 ATM类 public class ATM { public static void main(String[] args) { // variables Str

因此,我已经成功地将一组伪造银行账户信息的用户添加到一个文本文件中。现在我需要某种方法把它们读回我的程序

例如:

如果我输入用户名-mattsmith 和PIN-1234

Matt Smith的其他后台帐户信息将显示在控制台中,我希望能够执行诸如添加资金、检查余额和取款等操作

最好的方法是什么

这是我目前掌握的代码

ATM类

public class ATM {

public static void main(String[] args) {

    // variables
    String dash = "-------------------\n";
    int accounts = 0;
    Bank bank = new Bank();
    boolean login = true;

    // Scanner
    Scanner scanner = new Scanner(System.in);

    // Welcome screen
    System.out.print(dash);
    System.out.print("Welcome to the Bank\n");
    System.out.print(dash);

    System.out.println("Do you have an account with us? (y/n) ");

    String answer = scanner.nextLine();

    if (answer.equalsIgnoreCase("y")) {

        while (login) {

            System.out.println("Username: ");
            String lUsername = scanner.nextLine();
            System.out.println("PIN: ");
            int lPin = scanner.nextInt();

            if (lUsername.equals(SampleUser.myUserName)
                    && lPin == SampleUser.myPin) {
                login = false;
                System.out.println("\nLogin successful\n");
            }

        }

        Bank.menu();

    } else {

        // new user is created

        System.out.println("Enter your full name below (e.g. John M. Smith): ");
        String name = scanner.nextLine();
        System.out.println("Create a username: ");
        String userName = scanner.nextLine();
        System.out.println("Enter your starting deposit amount: ");
        double balance = scanner.nextInt();

        System.out.print(dash);
        System.out.print("Generating your information...\n");
        System.out.print(dash);

        int pin = bank.PIN();
        String accountNum = bank.accountNum();

        User user = new User(name, userName, pin, accountNum, balance);

        // new user gets added to the array list
        Bank.users.add(user);

        System.out.println(user);

    }

    try {

        File file = new File("users.text");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter  bw = new BufferedWriter(fw);

        bw.append(String.valueOf(Bank.users));
        bw.close(); 

    } catch (IOException e) {
        e.printStackTrace();
    }

}
public class Bank  {

// Generate a random 16 digit bank account number
public String accountNum() {

    int max = 9999;
    int min = 1000;

    int a1 = (int) (Math.random() * (max - min) + min);
    int a2 = (int) (Math.random() * (max - min) + min);
    int a3 = (int) (Math.random() * (max - min) + min);
    int a4 = (int) (Math.random() * (max - min) + min);

    String accountNum = a1 + "-" + a2 + "-" + a3 + "-" + a4;

    return accountNum;
}

// Generate a random 4 digit PIN
public int PIN() {

    int max = 9999;
    int min = 1000;

    int PIN = (int) (Math.random() * (max - min) + min);

    return PIN;
}

public static void menu() {

    while (true) {

        System.out.print("\n1"); System.out.print(" - View Balance       ");
        System.out.print("3"); System.out.print(" - Deposit Money\n"); 
        System.out.print("2"); System.out.print(" - Withdraw Money     ");
        System.out.print("4"); System.out.print(" - Exit\n\n");
        System.out.print("I would like to: \n");

        Scanner bscanner = new Scanner(System.in);
        int mc = bscanner.nextInt();

        if (mc == 1) {
            System.out.print("Your current balance is: $" + SampleUser.getMyBalance() + "\n");
        } else if (mc == 2) {
            System.out.print("Enter withdrawl amount: ");
            double wd = bscanner.nextDouble();
            SampleUser.myBalance -= wd;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");
        } else if (mc == 3) {
            System.out.print("Enter deposit amount: ");
            double dp = bscanner.nextDouble();
            SampleUser.myBalance += dp;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");

    }
    }
}

public static void readFile() {

}


// array list for users
static ArrayList users = new ArrayList(); {


};
public class User implements Serializable{

String name;
String userName;
String accountNum;
int pin;
double balance;

public User (String name, String userName, int pin, String accountNum, double balance) {

    this.name = name;
    this.userName = userName;
    this.accountNum = accountNum;
    this.pin = pin;
    this.balance = balance;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getAccountNum() {
    return accountNum;
}

public void setAccountNum(String accountNum) {
    this.accountNum = accountNum;
}

public int getPin() {
    return pin;
}

public void setPin(int pin) {
    this.pin = pin;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}
}

银行类

public class ATM {

public static void main(String[] args) {

    // variables
    String dash = "-------------------\n";
    int accounts = 0;
    Bank bank = new Bank();
    boolean login = true;

    // Scanner
    Scanner scanner = new Scanner(System.in);

    // Welcome screen
    System.out.print(dash);
    System.out.print("Welcome to the Bank\n");
    System.out.print(dash);

    System.out.println("Do you have an account with us? (y/n) ");

    String answer = scanner.nextLine();

    if (answer.equalsIgnoreCase("y")) {

        while (login) {

            System.out.println("Username: ");
            String lUsername = scanner.nextLine();
            System.out.println("PIN: ");
            int lPin = scanner.nextInt();

            if (lUsername.equals(SampleUser.myUserName)
                    && lPin == SampleUser.myPin) {
                login = false;
                System.out.println("\nLogin successful\n");
            }

        }

        Bank.menu();

    } else {

        // new user is created

        System.out.println("Enter your full name below (e.g. John M. Smith): ");
        String name = scanner.nextLine();
        System.out.println("Create a username: ");
        String userName = scanner.nextLine();
        System.out.println("Enter your starting deposit amount: ");
        double balance = scanner.nextInt();

        System.out.print(dash);
        System.out.print("Generating your information...\n");
        System.out.print(dash);

        int pin = bank.PIN();
        String accountNum = bank.accountNum();

        User user = new User(name, userName, pin, accountNum, balance);

        // new user gets added to the array list
        Bank.users.add(user);

        System.out.println(user);

    }

    try {

        File file = new File("users.text");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter  bw = new BufferedWriter(fw);

        bw.append(String.valueOf(Bank.users));
        bw.close(); 

    } catch (IOException e) {
        e.printStackTrace();
    }

}
public class Bank  {

// Generate a random 16 digit bank account number
public String accountNum() {

    int max = 9999;
    int min = 1000;

    int a1 = (int) (Math.random() * (max - min) + min);
    int a2 = (int) (Math.random() * (max - min) + min);
    int a3 = (int) (Math.random() * (max - min) + min);
    int a4 = (int) (Math.random() * (max - min) + min);

    String accountNum = a1 + "-" + a2 + "-" + a3 + "-" + a4;

    return accountNum;
}

// Generate a random 4 digit PIN
public int PIN() {

    int max = 9999;
    int min = 1000;

    int PIN = (int) (Math.random() * (max - min) + min);

    return PIN;
}

public static void menu() {

    while (true) {

        System.out.print("\n1"); System.out.print(" - View Balance       ");
        System.out.print("3"); System.out.print(" - Deposit Money\n"); 
        System.out.print("2"); System.out.print(" - Withdraw Money     ");
        System.out.print("4"); System.out.print(" - Exit\n\n");
        System.out.print("I would like to: \n");

        Scanner bscanner = new Scanner(System.in);
        int mc = bscanner.nextInt();

        if (mc == 1) {
            System.out.print("Your current balance is: $" + SampleUser.getMyBalance() + "\n");
        } else if (mc == 2) {
            System.out.print("Enter withdrawl amount: ");
            double wd = bscanner.nextDouble();
            SampleUser.myBalance -= wd;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");
        } else if (mc == 3) {
            System.out.print("Enter deposit amount: ");
            double dp = bscanner.nextDouble();
            SampleUser.myBalance += dp;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");

    }
    }
}

public static void readFile() {

}


// array list for users
static ArrayList users = new ArrayList(); {


};
public class User implements Serializable{

String name;
String userName;
String accountNum;
int pin;
double balance;

public User (String name, String userName, int pin, String accountNum, double balance) {

    this.name = name;
    this.userName = userName;
    this.accountNum = accountNum;
    this.pin = pin;
    this.balance = balance;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getAccountNum() {
    return accountNum;
}

public void setAccountNum(String accountNum) {
    this.accountNum = accountNum;
}

public int getPin() {
    return pin;
}

public void setPin(int pin) {
    this.pin = pin;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}
}

用户类

public class ATM {

public static void main(String[] args) {

    // variables
    String dash = "-------------------\n";
    int accounts = 0;
    Bank bank = new Bank();
    boolean login = true;

    // Scanner
    Scanner scanner = new Scanner(System.in);

    // Welcome screen
    System.out.print(dash);
    System.out.print("Welcome to the Bank\n");
    System.out.print(dash);

    System.out.println("Do you have an account with us? (y/n) ");

    String answer = scanner.nextLine();

    if (answer.equalsIgnoreCase("y")) {

        while (login) {

            System.out.println("Username: ");
            String lUsername = scanner.nextLine();
            System.out.println("PIN: ");
            int lPin = scanner.nextInt();

            if (lUsername.equals(SampleUser.myUserName)
                    && lPin == SampleUser.myPin) {
                login = false;
                System.out.println("\nLogin successful\n");
            }

        }

        Bank.menu();

    } else {

        // new user is created

        System.out.println("Enter your full name below (e.g. John M. Smith): ");
        String name = scanner.nextLine();
        System.out.println("Create a username: ");
        String userName = scanner.nextLine();
        System.out.println("Enter your starting deposit amount: ");
        double balance = scanner.nextInt();

        System.out.print(dash);
        System.out.print("Generating your information...\n");
        System.out.print(dash);

        int pin = bank.PIN();
        String accountNum = bank.accountNum();

        User user = new User(name, userName, pin, accountNum, balance);

        // new user gets added to the array list
        Bank.users.add(user);

        System.out.println(user);

    }

    try {

        File file = new File("users.text");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter  bw = new BufferedWriter(fw);

        bw.append(String.valueOf(Bank.users));
        bw.close(); 

    } catch (IOException e) {
        e.printStackTrace();
    }

}
public class Bank  {

// Generate a random 16 digit bank account number
public String accountNum() {

    int max = 9999;
    int min = 1000;

    int a1 = (int) (Math.random() * (max - min) + min);
    int a2 = (int) (Math.random() * (max - min) + min);
    int a3 = (int) (Math.random() * (max - min) + min);
    int a4 = (int) (Math.random() * (max - min) + min);

    String accountNum = a1 + "-" + a2 + "-" + a3 + "-" + a4;

    return accountNum;
}

// Generate a random 4 digit PIN
public int PIN() {

    int max = 9999;
    int min = 1000;

    int PIN = (int) (Math.random() * (max - min) + min);

    return PIN;
}

public static void menu() {

    while (true) {

        System.out.print("\n1"); System.out.print(" - View Balance       ");
        System.out.print("3"); System.out.print(" - Deposit Money\n"); 
        System.out.print("2"); System.out.print(" - Withdraw Money     ");
        System.out.print("4"); System.out.print(" - Exit\n\n");
        System.out.print("I would like to: \n");

        Scanner bscanner = new Scanner(System.in);
        int mc = bscanner.nextInt();

        if (mc == 1) {
            System.out.print("Your current balance is: $" + SampleUser.getMyBalance() + "\n");
        } else if (mc == 2) {
            System.out.print("Enter withdrawl amount: ");
            double wd = bscanner.nextDouble();
            SampleUser.myBalance -= wd;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");
        } else if (mc == 3) {
            System.out.print("Enter deposit amount: ");
            double dp = bscanner.nextDouble();
            SampleUser.myBalance += dp;
            System.out.println("\n$" + SampleUser.getMyBalance() + " is your new account total");

    }
    }
}

public static void readFile() {

}


// array list for users
static ArrayList users = new ArrayList(); {


};
public class User implements Serializable{

String name;
String userName;
String accountNum;
int pin;
double balance;

public User (String name, String userName, int pin, String accountNum, double balance) {

    this.name = name;
    this.userName = userName;
    this.accountNum = accountNum;
    this.pin = pin;
    this.balance = balance;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getAccountNum() {
    return accountNum;
}

public void setAccountNum(String accountNum) {
    this.accountNum = accountNum;
}

public int getPin() {
    return pin;
}

public void setPin(int pin) {
    this.pin = pin;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}
}


[1]

您当前的方法有什么问题?你也可以显示你的文本文件的一个片段。@peeskillet我只是卡住了,我不知道下一步该怎么办,屏幕截图马上就会出来。我知道你卡住了,但你必须解释你到底卡住了什么。我们猜不出你的问题在哪里。但是如果你清楚地指出问题所在,我们可以帮助你解决一个具体的问题。看起来你已经做了很多工作,所以你似乎正在取得进展。但是你被卡住了。那么,在这段代码中,你被困在了哪里?这个项目给你带来问题的领域是什么?e、 g.你无法在屏幕上打印一些内容@peeskillet屏幕截图已打开,我还在每个项目下评论了它们代表的内容。因此,我正在尝试为银行创建一个登录屏幕,如果用户名和pin与文本文件中存储的帐户匹配,那么其余信息将被删除,如果用户名和pin与帐户不匹配,那么我将打印一个错误。所以基本上我的问题是我不知道如何读取文件,如果可能的话,检查文件的细节