I am trying to improve my Java skill, so I wrote a simple ATM program.
I would like a review, to help me make the code more effective.
Main
public static void main(String[] args) {
ATM a = new ATM("A", "S", 20000);
Scanner in = new Scanner(System.in);
while(true){
System.out.println("Please choose: " + "\n" +
"1. Deposit" + "\n" + "2. Withdraw" + "\n" + "3. Info" + "\n" + "4. Exit");
int option = in.nextInt();
switch (option) {
case 1:
System.out.println("How much would you like to Deposit?");
double depositValue = in.nextDouble();
a.deposit(depositValue);
System.out.println("Your current balance is : " + a.getBalance() + "\n") ;
break;
case 2:
System.out.println("How much would you like to Withdraw?");
double withdrawValue = in.nextDouble();
a.withdraw(withdrawValue);
break;
case 3:
System.out.println(a.toString() + "\n" );
break;
case 4:
System.exit(0);
}
}
}
ATM
private String name;
private String lastName;
private double balance;
public ATM(String name, String lastName, double balance) {
this.balance = balance;
this.lastName = lastName;
this.name = name;
}
public String getName(){
return this.name;
}
public String getLastName(){
return this.lastName;
}
public double getBalance(){
return this.balance;
}
public void setBalance(int newValue){
this.balance = newValue;
}
public void deposit(double value){
balance += value;
}
public void withdraw(double value){
if(value > balance){
System.out.println("You can't withdraw that amount of money." +"\n"+ "Your balance is: " + getBalance() + "\n");
} else {
balance -= value;
System.out.println("You withdraw " + value +"from your account!" + "\n");
}
}
public String toString(){
return "First name: " + getName() +"\n" + "Last name: " + getLastName() + "\n" + "Your balance: " +getBalance();
}
import
statements? You'll probably get better reviews if you post the complete code of the program. \$\endgroup\$