从文本文件中读取,检查正确的信息,然后显示它

时间:2014-01-16 03:08:15

标签: java bufferedreader

因此,我已成功将虚假银行帐户信息的用户列表添加到文本文件中。现在我需要把它们读回我的程序。

例如:

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

Matt Smiths的其余账户信息将显示在控制台中,我希望能够做一些事情,比如加钱,查看余额和退出。

最好的方法是什么?

这是我到目前为止的代码

ATM CLASS

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(); {


};

}

USER CLASS

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] http://prntscr.com/2jqm20

0 个答案:

没有答案