[英]Java - Storing Strings to Login
我要做的任務是要求用戶輸入其姓氏,並為用戶提供一個帳號以登錄該程序。 我在下面列出了可能更有意義的步驟。
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: ");
}
從帳戶開始。 僅應使用名字(姓氏)來構造它,並具有一些用於帳號和余額的字段。
快速開始:
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 */
}
您應該自己解決其余的問題。 對於帳戶的“數據庫”,您可以簡單地使用static ArrayList<Account> allAccounts
,該聲明在UI類中的某個位置進行了聲明和初始化。 或者,您可以將其卸載到一個單獨的“容器”類中,該類可以為您完成此工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.