Question

Trying to compile this but can't figure out what's wrong. Have been struggeling with this for days now. Yes, I'm a newbie... Anybody could help me? Getting the errors in the methods in the bottom of the code.

import java.util.ArrayList;
import java.util.Scanner;

public class BankLogic
{
    private long pNr;
    private int accountId;
    private double amount;
    private double balance;
    private double rate;

    private ArrayList<Customer> customerlist;
    private ArrayList<SavingsAccount> accounts;

    Scanner in = new Scanner(System.in);

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public BankLogic(){

        customerlist = new ArrayList<Customer>();
        accounts = new ArrayList<SavingsAccount>();
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public String toString(){

        String info = "Personnummer: " + pNr + "\nKontonummer: " + accountId
        + "\nSaldo: " + amount;
    }
    //----------------------------------------------------------------------
    // Beskrivning: returnerar presentation av alla kunder(pers.nr och namn)
    // Returvärde: String 
    //----------------------------------------------------------------------
    public String infoBank(){

        return customerlist.toString();
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public boolean addCustomer(String name, long pNr){

        for (Customer a : customerlist)
        {
            if (a.getPCode() == pNr)
                return false;
            else
            {
                Customer aCust = new Customer(name, pNr);   
                customerlist.add(aCust);
            }
        }
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public String infoCustomer(long pNr){

        for (Customer a : customerlist)
        {
            if (a.getPCode() == pNr)
            {
                System.out.println(a);
            }
        }
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public boolean changeCustomerName(String name, long pNr){

        for (Customer a : customerlist)
        {
            if (a.getPCode() == pNr)
            {
                Customer aCust = new Customer(name, pNr);   
                customerlist.add(aCust);
            }
        else
            return false;
        }
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public String removeCustomer(long pNr){

        for (int i = 0; i < customerlist.length; i++)
        {
            Customer a = customerlist.get(i);
            if (a.getPCode() == pNr)
            {
                customerlist.remove(i);
            }
            else
                i++;
        }

    }


    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public int addSavingsAccount(long pNr){

        for (Customer a : customerlist)
        {
            if (a.getPCode() == pNr)
            {
                boolean added = false;
                for (int i = 0; !added && i < accounts.size(); i++)
                {
                    added = accounts.get(i).addAccount(a);
                    if(added)
                    {
                        System.out.println("Kontonummer: " + accounts.get(i).getAccountId();
                    }
                    else 
                        return "Kontot skapades inte.";
                }
            }
        }
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public String infoAccount(long pNr, int accountId)
    {
        for (Customer a : customerlist)
        {
           if (a.getAccountId() == accountId && a.getPCode() == pNr)
               return a.infoCust();
         } 
       return null; 
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public boolean deposit(long pNr, int accountId, double amount){

        for (Customer a : customerlist)
        {
           if ((a.SavingsAccount.getAccountId() == accountId) && (a.getPCode() == pNr))
            {
                a.SavingsAccount.getBalance() = balance + amount;
            }
            else
                return false;
        }
    }

    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    public boolean withdraw(long pNr, int accountId, double amount){

        for (Customer a : customerlist)
        {
           if ((a.getAccountId() == accountId) && (a.getPCode() == pNr))
            {
                a.getBalance() = balance - amount;
                return true;
            }
            else
                return false;
        }
    }

} 
Was it helpful?

Solution 2

Below two statements are incorrect. Left hand side of an expression must be a variable and in your case it is a value.

        a.SavingsAccount.getBalance() = balance + amount;


          a.getBalance() = balance - amount;

correct them by using appropriate variables or setter methods. Hope this helps.

OTHER TIPS

There's all sorts of errors in the above code I can see at a glance:

  • toString() isn't returning anything (it just declares a local variable that's then immediately garbage collected as the method ends)
  • addCustomer() doesn't always return something.
  • infoCustomer() and removeCustomer() never return anything, despite declaring that they should in the method header
  • addSavingsAccount() should return an int (according to the method header), but only returns a string - sometimes.
  • Unless Customer has a public field called SavingsAccount (which would be hideously bad design from so many respects) then a.SavingsAccount.getAccountId() and similar is wrong, you probably want the method on Customer that gets its particular savings account.

You need to get into the habit of checking your code in much smaller increments, at least until you have a handle on the basic syntactical side of things. That way you won't end up in these sorts of scenarios where there's a ton of errors that you have to fix, with no real knowledge of how to fix them.

Every time you add (for instance) a method: stop, compile, check it works as you want it to, and only then move onto the next thing. It'll save you a heck of a lot of time in the long run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top