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();
}