Jess Holderbaum Jess Holderbaum - 9 days ago 6
Java Question

Error: java.util.ArrayList<object> cannot be resolved to a type

I believe this is a simple problem, but I just can't get my head around it. I am trying to load and save a ArrayList of type ClientAccount objects. The save seems to come out with nop errors (I have not actually been able to test it yet), but the readObject is failing with two different errors:

Error: java.util.ArrayList<ClientAccounts> cannot be resolved to a type
and
Error: ClientAccounts cannot be resolved to a type


Any help would be much appreciated!

ClientAccounts.java:

import java.io.Serializable;
public class ClientAccount implements Serializable {

public String accountName;
public double accountBalance;
public int accountPIN;

public ClientAccount(String accountName, double accountBalance, int accountPIN){
this.accountName=accountName;
this.accountBalance=accountBalance;
this.accountPIN=accountPIN;
}

// Account Name Methods
public String getAccountName() {
return accountName;
}
public void setAccountName(String name) {
accountName = name;
}

// Account Balance Methods
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double balance) {
accountBalance = balance;
}

// PIN Methods
public int getAccountPIN() {
return accountPIN;
}
public void setAccountPIN(int newPIN) {
accountPIN = newPIN;
}

}


MainATM.java:

// import libraries
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*; // File I/O - FileReader myReader = new FileReader(file.txt);

public class MainATM {

public static ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(2);
public static ClientAccount editBankAccount;

public static void main(String[] args) {

// Declare Variables and Class references
boolean verifiedAccount = false;
int numberOfAccounts = 1;
// Create ATM account ArrayList
// ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(2);
// ClientAccount editBankAccount;
// Get Account data from files
loadATMAccounts(accounts);
int accountNumber=(accounts.size()+1);

// User Login
// Loop until successfull Login
while (verifiedAccount == false) {
// Get Account Number
accountNumber = getAccountNumber(accounts.size());
editBankAccount = accounts.get(accountNumber);
// Process PIN
verifiedAccount = verification(editBankAccount);
}

// Account Operations
editBankAccount = atmOperations(editBankAccount);
accounts.set(accountNumber, editBankAccount);
saveATMAccounts(accounts);
System.out.println(editBankAccount);
System.out.println("Logged Out");

}

// Enter Account Number
public static int getAccountNumber (int numberOfAccounts){
String userAccountNumberString = new String();
int inputAccountNumber=(numberOfAccounts+1);
boolean gotAccountNumber = false;
while (gotAccountNumber == false){
userAccountNumberString = JOptionPane.showInputDialog("<html> There are "+(numberOfAccounts)+"<br>What is your Account number? ");
inputAccountNumber = Integer.parseInt(userAccountNumberString);
if (inputAccountNumber > (-1) && inputAccountNumber < numberOfAccounts) gotAccountNumber = true;
}
return inputAccountNumber;
}

// Verify User PIN
public static boolean verification (ClientAccount tempAccount){
String userInputPIN = JOptionPane.showInputDialog("<html> The user pin for this account is: "+(tempAccount.getAccountPIN())+"<br> Enter the PIN for "+tempAccount.getAccountName()+": ");
int inputPIN = Integer.parseInt(userInputPIN);
boolean verifiedAccount = false;
if (inputPIN == tempAccount.getAccountPIN()){
verifiedAccount = true;
}
else {
verifiedAccount = false;
}
return verifiedAccount;
}

// Main ATM window
public static ClientAccount atmOperations (ClientAccount tempAccount){
boolean logout = false;
int mM;
String userChoice;
while (logout == false){

// Begin Main Jframe ------------------------------------
JFrame atmFrame = new JFrame("Main ATM Menu");
JPanel atmPanel = new JPanel();
atmPanel.setLayout(new FlowLayout());
JLabel atmLabel = new JLabel("ATM Label?");
JButton atmBalanceButton = new JButton();
JButton atmDepositButton = new JButton();
JButton atmWithdrawButton = new JButton();
JButton atmExitButton = new JButton();
atmBalanceButton.setText("Balance");
atmDepositButton.setText("Deposit");
atmWithdrawButton.setText("Withdraw");
atmExitButton.setText("Exit/Logout");
atmPanel.add(atmLabel);
atmPanel.add(atmBalanceButton);
atmPanel.add(atmDepositButton);
atmPanel.add(atmWithdrawButton);
atmPanel.add(atmExitButton);
atmFrame.add(atmPanel);
atmFrame.setSize(300, 300);
atmFrame.setLocationRelativeTo(null);
atmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
atmFrame.setVisible(true);
// End Main JFrame --------------------------------

userChoice = JOptionPane.showInputDialog("<html>Main ATM Menu<br> 1) Balance Button<br> 2) Deposit Button<br> 3) Withdraw Button<br> 9) Exit/Logout Button<br><br> "+(tempAccount.getAccountName())+", what would you like to do? ");
mM = Integer.parseInt(userChoice);
if (mM == 1){
balance(tempAccount);
} else if (mM == 2){
tempAccount = deposit(tempAccount);
} else if (mM == 3){
tempAccount = withdraw(tempAccount);
} else if (mM == 9) logout = true;
}
return tempAccount;
}

// Show Balance Window
public static void balance(ClientAccount accountBalance){
JOptionPane.showMessageDialog(null, "Your current balance is: $"+accountBalance.getAccountBalance());
}

// Make Deposit Window
public static ClientAccount deposit(ClientAccount accountDeposit){
double editBalance = Double.parseDouble(JOptionPane.showInputDialog("<html>Your current balance is: $"+accountDeposit.getAccountBalance()+"<br> Enter deposit amount: $"));
accountDeposit.setAccountBalance(accountDeposit.getAccountBalance()+editBalance);
return accountDeposit;
}

// Make Withdrawal Window
public static ClientAccount withdraw(ClientAccount accountWithdraw){
double editBalance = Double.parseDouble(JOptionPane.showInputDialog("<html>Your current balance is: $"+accountWithdraw.getAccountBalance()+"<br> Enter withdrawal amount: $"));
if (editBalance > accountWithdraw.getAccountBalance()){
String ynPrompt = JOptionPane.showInputDialog("<html>-----OVERDRAW ALERT-----<br><br>Your current balance is: $"+accountWithdraw.getAccountBalance()+"<br> Your intended withdrawal amount is: $"+(editBalance)+"<br> A $35 overdraw fee will be assesed to your account if you continue!<br><br> If you Still wish to overdraw your account type 'YES' all other input will cancel the transaction: ");
if (Objects.equals("YES", ynPrompt)){
accountWithdraw.setAccountBalance((accountWithdraw.getAccountBalance()) - editBalance - 35);
}
} else {
accountWithdraw.setAccountBalance(accountWithdraw.getAccountBalance() - editBalance);
}
return accountWithdraw;
}

// Save ArrayList of ATM Objects //call by: saveATMAccounts(accounts);
public static void saveATMAccounts(ArrayList<ClientAccount> saveAccounts){
try{
FileOutputStream saveFile=new FileOutputStream("ATMAccounts.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(saveAccounts);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}

}

// Load ArrayList of ATM Objects //call by: loadATMAccounts(accounts);
public static void loadATMAccounts(ArrayList<ClientAccount> loadAccounts){
try{
FileInputStream loadFile=new FileInputStream("ATMAccounts.sav");
ObjectInputStream getLoad = new ObjectInputStream(loadFile);
ArrayList<ClientAccount> accounts = (ArrayList<ClientAccounts>) getLoad.readObject();
getLoad.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
}

RPD RPD
Answer

As Andy mentioned, change:

ArrayList<ClientAccount> accounts = (ArrayList<ClientAccounts>) getLoad.readObject();

to

ArrayList<ClientAccount> accounts = (ArrayList<ClientAccount>) getLoad.readObject();

I'm not sure how it even compiled, that Object doesn't exist or maybe it does but I can't see it in the package.