Java - 存储字符串登录

时间:2016-02-15 18:30:32

标签: java string login account

我有一项任务要做,包括要求用户输入他们的姓氏并给用户一个帐号来登录该程序。我列出了下面可能更有意义的步骤。

1)用户创建帐户

2)用户输入姓氏

3)为用户提供帐号

4)用户可以使用他们的姓氏和帐号登录

用户输入他们的姓氏后,他们会得到一个帐号,然后用来登录存款,取款和支票余额。

如何在不使用数据库的情况下创建程序?

帐户类

private static int number = 500;

Account(){

    accountNumber = number++;
}

创建帐户类

public void createAccount(){

    String firstName;

    System.out.print("Please Enter Last Name: ");
    lastName = scanner.nextLine();
    System.out.println("This is your Account Number to log into: " + _______ );
}

public void logIn(){

    System.out.println("Please enter your last name: ");

    System.out.println("Please enter your account number: ");

}

1 个答案:

答案 0 :(得分:0)

从帐户开始。应该只给出一个名称(姓氏),并为帐号和余额设置一些字段。

快速入门:

public class Account 
{
    private static int number = 500;

    private int accountNumber; 
    private int balance = 0; //(in cents or a float if this is in full dollars)
    private String name;

    public Account(String name){
        accountNumber = number++;
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public int getBalance(){
        return this.balance;
    }

    public boolean withdrawMoney(int amount){
        if(this.balance < amount)
            return false;
        this.balance -= amount;
        return true;
    }

    public int getAccountNumber(){
        return this.accountNumber;
    }
}

然后,为每个输入的名称创建该用户的帐户,然后随意执行任何操作。

public void createAccount(){

    System.out.print("Please Enter Last Name: ");
    String lastName = scanner.nextLine();

    /* Create an account */
    Account account = new Account(lastName);

    /* TODO: Save all accounts into a ArrayList<Account> to keep track of them */

    System.out.println("This is your Account Number to log into: " + account.getAccountNumber());
}

public void logIn(){

    System.out.println("Please enter your last name: ");
    String lastName = scanner.nextLine();

    System.out.println("Please enter your account number: ");
    String accountNumber = scanner.nextLine();

    /* TODO: Convert accountNumber to an int */

    /* TODO: Check if an account exists in that list of saved accounts which has
       that lastName and that account number 
    */

    /* TODO: Add interaction with the bank account */
}

你应该自己弄清楚其余部分。对于&#34;数据库&#34;在帐户中,您可以简单地使用在UI类中的某个地方声明和初始化的static ArrayList<Account> allAccounts。或者你可以将它卸载到一个单独的容器&#34;为你做这件事的课程。