java - Need to retain values of checkings and savings variables through methods -
i'm having hard time keeping values of checkings , savings , being able display checkings , savings current balance.
package atmmethod; import java.util.locale; import java.util.scanner; /** * * @author jfumar */ public class atmmethod { private static int option; /** * @param args command line arguments */ public static void main(string[] args) { //prompts user options in simulation using methods { system.out.println("welcome new , improved atm program methods!"); displaymainmenu(); double savings = 1000; double checkings = 500; system.out.println("savings balance is: " + savings); system.out.println("checking balance is:" + checkings); scanner input = new scanner(system.in); system.out.print("enter option here: "); int option = input.nextint(); switch (option) { case 1: depositoption(); break; case 2: withdrawoption(); break; case 3: checkingbalance(); break; default: system.out.print("goodbye!"); system.exit(0); } } while (option != 4); system.out.println("thank using new atm"); } public static void displaymainmenu() { system.out.println("select options numbers 1, 2, 3, or 4"); system.out.println("1. deposit"); system.out.println("2. withdraw"); system.out.println("3. balance"); system.out.println("4. exit"); } public static void depositoption() { double currentsavings = 1000; double currentcheckings = 500; double amount; system.out.println("where deposit money?"); system.out.println("1. savings"); system.out.println("2. checkings"); scanner input = new scanner(system.in); system.out.print("enter choice here: "); int choice = input.nextint(); if (choice == 1) { system.out.println("how want deposit in savings?"); amount = input.nextdouble(); currentsavings += amount; system.out.println("your savings balance now: " + currentsavings); } else { system.out.println("how want deposit in checkings?"); amount = input.nextdouble(); currentcheckings += amount; system.out.println("your checkings balance now: " + currentcheckings); } } public static void withdrawoption() { double currentsavings = 1000; double currentcheckings = 500; double amount; system.out.println("where withdraw money?"); system.out.println("1. savings"); system.out.println("2. checkings"); scanner input = new scanner(system.in); system.out.print("enter choice here: "); int choice = input.nextint(); if (choice == 1) { system.out.println("how want withdraw savings?"); amount = input.nextdouble(); currentsavings -= amount; system.out.println("your savings balance now: " + currentsavings); } else { system.out.println("how want withdraw checkings?"); amount = input.nextdouble(); currentcheckings -= amount; system.out.println("your checkings balance now: " + currentcheckings); } } public static void checkingbalance() { system.out.println("current balances"); system.out.println("savings balance is: "); system.out.println("checkings balance is: "); } }
savings , checkings should fields of class, not local variables. local variables destroyed @ end of method.
public class atmmethod { private static double currentsavings = 500; private static double currentcheckings = 1000; //... }
by way methods , fields static, not good, that's story