Hi I'm currently developing a small baking system to manage bank accounts and perform transactions such as deposit and withdraw. Also it needs to be able to create unlimited amount of accounts and delete accounts giving the account number.
I have a Client.class
where I initialised all the parameters needed to create the account. I'am trying to store this class inside a HashMap as a key value, but it seems like nothing as been inserted. When I try printing out the client information I get null values
System.out.println("this has been created ");
JOptionPane.showMessageDialog(f, "Account registered successfully");
System.out.println("***********************************");
System.out.println("account registered "
+ bank.clientMap.get(userId) +" "+ bank.getLoginInfo());
System.out.println("user details " + client.getName()+" "+ client.getUsername() +" "
+ client.getAddress() +" "+ client.getBalance());
Client Account Created this has been created
account registered com.xbank.event.Client@52605789 {50.0=com.xbank.event.Client@52605789, Pass_123=com.xbank.event.Client@52605789, 115 High Road=com.xbank.event.Client@52605789, Admin=com.xbank.event.Client@52605789, user123=com.xbank.event.Client@52605789}* user details null 50.0
I don't know what I'm doing wrong, but I can't get the values inserted in the hashMap
Client.Class
public class Client {
public String name;
public String username;
public String password;
public Double balance;
public String address;
public String accNum;
public String date;
static DateFormat df = new SimpleDateFormat();
private ArrayList<Account> accounts;
// Default Constructor
public Client() {
name = "";
address = "";
username = "";
password = "";
accNum = "";
balance = 0.0;
}
// Parameterized Constructor
public Client(String accNum, String name, String username,String password, String address, double balance, Date date) {
this.name = name;
this.username = username;
this.password = password;
this.address = address;
this.balance = balance;
this.accNum = accNum;
accounts = new ArrayList<>();
}
public ArrayList<Account> getStringList(String acc_num, String From, String To) {
return accounts;
}
public void setStringList(ArrayList<Account> accounts) {
this.accounts = accounts;
}
public void showClientInfo() {
System.out.println("Name: "+name);
System.out.println("Balance: "+ balance);
System.out.println("Address: "+address);
System.out.println("Username " + username );
System.out.println("Account Number: " + accNum);
System.out.println("password: " + password);
}
public Account getAccount(String acctNum) {
for(Account a: accounts) {
if(acctNum.equals(a.getAccountNumber())) {
return a;
}
}
return null;
}
public boolean removeAccount(String acctNum) {
Account a = getAccount(acctNum);
if(a != null) {
a.setActive(false);
accounts.remove(a);
return true;
}
return false;
}
public void setBalance(double balance) {
if(balance >= 0){
this.balance = balance;
}
}
public String getName() {
return name;
}
public void setName(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
}
}
public double getBalance() {
return balance;
}
public String getAddress(String address) {
return address;
}
public String getAddress() {
return null;
}
public void setAddress(String address) {
this.address = address;
}
public static String getDate() {
Date date = Calendar.getInstance().getTime();
String dateToString = df.format(date);
return (dateToString);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword(String password) {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// end of class
}
Bank_Account.Class where I declared the HashMap
// Bank_Account Class
public class Bank_Account {
public Client newClient = new Client();
public static HashMap<String, Client> clientMap;
public HashMap<String, ArrayList<Account>> accountMap = new HashMap<String, ArrayList<Account>>();
private ArrayList<Account> accounts;
// constructor
public Bank_Account() {
clientMap = new HashMap<String, Client>();
}
// updates the object member variables
public int addInfo(String accNum, String name, String username, String password, String address, double balance, Date date) {
try {
if (!password.matches((("(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*_]).{8,}")))) {
return -1;
} else if(clientMap.containsKey(username)) {
return -2;
}
else {
newClient = new Client(accNum, name, username, password, address, balance, date);
clientMap.put(newClient.getUsername(), newClient);
clientMap.put(newClient.getPassword(password), newClient);
clientMap.put(newClient.getAddress(address), newClient);
clientMap.put(newClient.getName(), newClient);
clientMap.put(Double.toString(newClient.getBalance()), newClient);
System.out.println("Client Account Created");
return 0;
}
} catch (Exception e) {
System.out.println("Client Account Failed " + clientMap);
}
return -1;
}
public boolean closeAccount(String username, String acctNum) {
Client client = clientMap.get(username);
if (client != null) {
return client.removeAccount(acctNum);
}
return false;
}
public synchronized void addToList(String accNum) {
Account account = new Account();
if (!accountMap.containsKey(accNum)) {
accountMap.put(accNum, accounts);
}
else if (accountMap.containsKey(accNum)) {
account.getAccountNumber();
}
else {
System.out.println("account number: " + accNum);
}
}
public static void displayCustomerInformation(Client client){
if(client != null){
System.out.println(client);
}
}
public static Map<String, Client> getLoginInfo() {
return clientMap;
}
public int addAmount(int b) {
return 0;
}
public Date getDate(Date date) {
return date;
}
public int removeAmount(int b) {
return 0;
}
public void updateBalance() {
}
public void finalize() {
System.out.println("Bank Account Destructor");
}
// end of class
}