So brief overview of my assignment: I'm supposed to write an ATM class that uses an accounts class I created. (The accounts class contains methods, open - to create a new account , quit - to exit the current account , login - for a user to login to their account , deposit, withdraw, get balance, and terminate - which breaks out of the infinite loop). My ATM class must keep track of all the opened accounts, so I figure use an array list of type account. For some reason though, the .contains() method is not recognizing duplicates, so it keeps adding accounts that are already there, and I'm not quite sure how to fix this issue. Here's my code: THANKS =)
My account class:
import java.util.ArrayList;
public class Account
{
public ArrayList<Account> accounts;
public static int NextAcctNo = 999;
private int accountNo;
private double balance;
public Account()
{ // constructor
balance = 0.0;
accountNo = ++NextAcctNo;
}
public void Open()
{
System.out.println("New account number: " + accountNo);
}
public void Quit()
{
System.out.println("Goodbye");
}
public void Login(int accountNo)
{
System.out.println("Hello");
this.accountNo = accountNo;
}
public void Deposit(double amount)
{ // method
balance += amount;
System.out.println("Deposited: " + amount);
}
public void Withdraw(double amount)
{ // method
balance -= amount;
System.out.println("Withdrew: " + amount);
}
public double Balance()
{ // method
System.out.println("Balance: " + balance);
return balance;
}
public void Terminate()
{
System.out.println("Terminated");
}
public int getAccountNo()
{ // method
return accountNo;
}
}//class Account
my main method:
import java.util.Scanner;
import java.util.ArrayList;
public class ATM
{
// search for accounts for D, W, B
public static void main(String [] args)
{
ArrayList<Account> accounts = new ArrayList<Account>();
int counter = 0;
while (true)
{
Scanner commandScanner = new Scanner(System.in);
System.out.print("Enter a command - O, Q, L, D, W, B, T: ");
String command = commandScanner.nextLine();
if (command.equals("O") == true)
{
Account newAccount = new Account();
accounts.add(newAccount);
counter++;
newAccount.Open();
}
else if (command.equals("Q") == true)
{
if (accounts.size() == 0)
{
System.out.println("Error");
}
else
{
accounts.get(counter-1).Quit();
}
}
else if (command.equals("L") == true)
{
Scanner numberScanner = new Scanner(System.in);
System.out.println("Enter your account number: ");
int accountNo = numberScanner.nextInt();
Account temp = new Account();
temp.accountNo = accountNo;
if (counter == 0)
{
accounts.add(temp);
counter++;
}
else if (counter > 0)
{
if (accounts.contains(temp.accountNo) == true)
{
counter+=0;
}
else
{
Account newAccount = temp;
newAccount.Login(accountNo);
accounts.add(newAccount);
counter++;
System.out.println(accounts.size());
}
}
}
The code is still work in progress, I haven't posted it all - which is why the brackets aren't all closed off.
hashcodeandequalsmethods of theAccountclass in order for theArrayListto detect duplicate instances of an object