2015-04-09 5 views
-1

Мне сложно сохранять значения проверок и сбережений, а также отображать проверки и баланс текущего баланса.Необходимо сохранить значения проверок и переменных сбережений с помощью методов

package atmmethod; 

import java.util.Locale; 
import java.util.Scanner; 

/** 
* 
* @author jfumar 
*/ 
public class Atmmethod { 

private static int option; 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    //prompts user with options in simulation using methods 

    do { 
     System.out.println("Welcome to the new and improved atm program with 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 you for using the new atm"); 

} 

public static void displayMainMenu() { 
    System.out.println("Select options by 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 would like to deposit your 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 much do you want to deposit in your savings?"); 
     amount = input.nextDouble(); 
     currentSavings += amount; 
     System.out.println("your savings balance is now: " + currentSavings); 
    } else { 
     System.out.println("how much do you want to deposit in your checkings?"); 
     amount = input.nextDouble(); 
     currentCheckings += amount; 
     System.out.println("your checkings balance is now: " + currentCheckings); 

    } 

} 

public static void withdrawOption() { 
    double currentSavings = 1000; 
    double currentCheckings = 500; 
    double amount; 

    System.out.println("where would like to withdraw your 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 much do you want to withdraw from your savings?"); 
     amount = input.nextDouble(); 
     currentSavings -= amount; 
     System.out.println("your savings balance is now: " + currentSavings); 
    } else { 
     System.out.println("how much do you want to withdraw from your checkings?"); 
     amount = input.nextDouble(); 
     currentCheckings -= amount; 
     System.out.println("your checkings balance is now: " + currentCheckings); 

    } 

} 

public static void checkingBalance() { 
    System.out.println("CURRENT BALANCES"); 
    System.out.println("Savings balance is: "); 
    System.out.println("Checkings balance is: "); 
} 

} 

ответ

0

Сбережения и проверки должны быть полями вашего класса, а не локальными переменными. Локальные переменные уничтожаются в конце метода. не

public class Atmmethod { 

    private static double currentSavings = 500; 
    private static double currentCheckings = 1000; 

    //... 

} 

Кстати, все ваши методы и поля являются статическими, что не хорошо, но это другая история

0

Я думаю, что вы хотите, это глобальная переменная для поверок и сбережений, если я недоразумение твоя проблема. Всякий раз, когда выполняются операции depositOption() и takeOption(), он сбрасывает остатки до 1000 и 500. Просто сделайте текущие переменные currentSavings и currentCheckings глобальными, а затем в режиме снятия и депозита добавьте/вычтите сумму, которую пользователь вводит из глобальной переменной.

Смежные вопросы