0

I am trying to code ATM code with bank account. I used arraylist and when the user creating his account I added his account to the list. I used switch-case and I couldn't use methods.

If someone can fix this it would be perfect I am really trying to figure out how to do this. in case 1 you can see I added the new bankaccount to the list, I made an ATM c object on case 1 and I am trying to make is accesible in case 2. ty guys

Main:

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    ArrayList<BankAccount> accounts = new ArrayList<BankAccount> ();
    boolean exitRequested = false;
    while (!exitRequested) {
        System.out.println("Please chose an option:" +"\n" +
                "1.New account" + "\n" +
                "2.Exist account" + "\n" +
                "3.Exit");
        int options = Integer.parseInt(in.nextLine());
        switch (options) {
            case 1:
                System.out.println("Welcome to our bank!" + "\n" + "Please answer few questions in order to complete registation.");
                System.out.println("First name: ");
                String fn = in.nextLine();
                System.out.println("Last name: ");
                String ln = in.nextLine();
                System.out.println("Welcome " + fn + "!");
                accounts.add(new BankAccount(1,fn,ln));
                System.out.println(accounts.get(0).toString());
                ATM c = new ATM(accounts.get(0));
                break;
            case 2:
                System.out.println("Please enter your bankaccount id: ");
                int customerId = Integer.parseInt(in.nextLine());
                if(customerId == accounts.get(0).getId()) {
                System.out.println("What would you like to do?" +"\n" +
                        "1. Withdraw" + "\n" +
                        "2. Deposit" + "\n" +
                        "3. info");
                String chose = in.nextLine();
                if(chose.equals("1")) {
                    System.out.println("How much would you like to withdraw?");
                    int withdrawValue = Integer.parseInt(in.nextLine());
                    c.withdraw(withdrawValue); // here is the problem
                    System.out.println("You've withraw " +withdrawValue + "from your account!");
                } else if(chose.equals("2")){
                    System.out.println("How much would you like to deposit?");
                    int depositValue = Integer.parseInt(in.nextLine());
                    c.deposit(depositValue); // also here ..
                    System.out.println("You've deposit " +depositValue +"to your account succesfuly!");
                } else if(chose.equals("3")) {
                    System.out.println(c.toString()); // ...
                }

            } else {
                    System.out.println("Invaild id");
                }
                break;

            case 3:
                exitRequested = true;
                break;
        }
    }
}

ATM:

public class ATM extends BankAccount{

    private BankAccount bank;

public ATM(BankAccount bank){

}

public ATM() {

}

public void withdraw(int value){
    if(getBalance() < value ){
        System.out.println("You dont have enough funds.");

    } else {
        setBalance(getBalance() - value);
    }
}

public void deposit(int value){
    setBalance(getBalance() + value);
}

BankAccount:

public class BankAccount {

private int id;
private String fName;
private String lName;
private int balance;


public BankAccount(int id, String fName, String lName){
    this.id = id;
    this.fName = fName + "";
    this.lName = lName + "";
    this.balance = 0;
}

public BankAccount() {

}

public int getId(){
    return this.id;
}

public void setId(int value){
    this.id = value;
}

public String getFirstName(){
    return this.fName;
}

public void setFirstName(String value){
    this.fName = value;
}

public String getLastName(){
    return this.lName;
}

public void setLastName(String value){
    this.lName = value;
}

public int getBalance(){
    return this.balance;
}

public void setBalance(int value){
    this.balance = value;
}


public String toString(){
    return "Name: " + getFirstName() +"\n" +
           "Last name: " + getLastName() + "\n" +
            "ID: " + getId() + "\n" +
            "Balance: " + getBalance();
}
stuss07
  • 13
  • 3
  • 1
    This is not a code writing or homework service. If you describe what is wrong with your code, we will show you what you need to do to fix it yourself. One place to start is [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) which has some tips about debugging which are critical for you to learn as a new programmer. I use these techniques every day as a professional. – Code-Apprentice Mar 08 '21 at 14:45
  • When you say you "can't see it", what do you mean? What is the exact output of your program when you run it? And what do you expect instead? Please [edit] your question to include these details. – Code-Apprentice Mar 08 '21 at 14:47
  • Look up what "scope" means in Java. The problem is that your create the ATM variable called `c` in one scope, and that variable is not accessible in the scope you try to access it. – Michael Mar 08 '21 at 14:47
  • I mean how can I make "c" accesible? – stuss07 Mar 08 '21 at 15:28
  • You should make a constructor in the `ATM` class that calls the base class: `public ATM(int id, String fName, String lName) { super(id, fName, lName); }`. Then when the user creates a new account, add it to the list: `accounts.add(new ATM(1, fn, ln));` – 001 Mar 08 '21 at 16:01
  • Later, in `case 2`, search for it in the list. There are lots of ways to search a list. For ex: `ATM c = (ATM)accounts.stream().filter(act -> act.getId() == customerId).findAny().orElse(null);` – 001 Mar 08 '21 at 16:03

0 Answers0