我正在創建一個銀行程序和hava類Account
(摘要),SavingsAccount
和Customer
(等等)。當我創建一個新的savingsaccount
我想爲每個帳戶獲取一個ID。我的問題是我無法弄清楚如何讓ID獨特。獲取唯一的id銀行程序java
我還缺少什麼?我怎樣才能爲客戶和賬戶使用ArrayList
?還有其他建議嗎?這對我來說非常困難,但我努力學習和理解。
在銀行類中,我創建了幾個客戶c1和c2。然後,我爲客戶c1創建兩個帳戶。
public class Bank {
public static void main(String[] args) {
ArrayList<Customer> customerList = new ArrayList<Customer>();
ArrayList<Account> accountList = new ArrayList<Account>();
Customer c1 = new Customer("John");
Customer c2 = new Customer("Sam");
SavingsAccount a1 = new SavingsAccount(c1);
SavingsAccount a2 = new SavingsAccount(c1);
customerList.add(c1);
customerList.add(c2);
accountList.add(a1);
accountList.add(a2);
}
}
帳號等級:
public abstract class Account {
protected int balance;
protected int id;
protected double interest;
protected Customer name;
public Account() {}
public Account(Customer name) {
this.name = name;
}
public abstract void deposit(int amount);
public abstract void withdraw(int amount);
public abstract int getBalance();
public abstract void transfer(int amount, Account account);
}
在類SavingsAccount:
public class SavingsAccount extends Account{
public SavingsAccount() {
super();
}
SavingsAccount(Customer person) {
name = person;
id = (int) Math.floor(Math.random()*101); //This won't generate a unique number for each savingsaccount
}
}
如果你不存儲你的uid在一個窗體中(如Vaishak Suresh的發音答案#1)它不能保證是一個唯一的ID - 這不太可能,但不能保證 - 但如果你只是爲了練習而編寫代碼,那麼你可以使用簡單的文本文件而不是數據庫(在這種情況下,UUID會適合你......所以不要介意我的提示...... – 2014-09-05 19:37:42