简体   繁体   English

如何在帐户上存储和修改余额:

[英]How to store and modify balance on account:

I have a small acount in which to deposit, withdrawl, view balance, and exit. 我有一小笔存款,取款,查看余额和退出。 Everything runs perfectly however I would like the balance to ajust according to what method I do (withdrawal, deposit, etc.) I started with joptionpane but had the same problem and thought I should just start over. 一切都运行得很完美,但是我希望平衡能够根据我的方法(撤回,存款等)进行调整。我从joptionpane开始但是遇到了同样的问题并且认为我应该重新开始。 Anyone point me in the right direction? 有人指出我正确的方向吗?

Main file: 主文件:

import java.util.Scanner;

public class Bank1
{
    public static void main(String[] args) 
    {
        HomeBank obj1 = new HomeBank();

        Scanner keyboard = new Scanner(System.in);
        String option;
        char object;

        System.out.println("This is your home banking account" + "\n");

        do 
        {
            System.out.println("What would you like to do today?"+ "\n" +
                    "\nSelect the following: " +
                    "\n'B' To View Balance." +
                    "\n'D' To Deposit" +
                    "\n'W' To Withdrawal" +
                    "\n'E' To Exit");

            System.out.print("Your selection: ");       

            option = keyboard.next().toUpperCase();
            object = option.charAt(0);

            System.out.print("\n");
            System.out.println("you entered: " +object+ "\n");

            if(object =='D')
                obj1.deposit();
            else if(object =='W')
                obj1.withdrawal();
            else if(object =='B')
                obj1.balance();
            else
                System.out.println("**Invalid input**" +
                        "\n Please try again");            
        } while(object !='E');

        System.out.println("The End");    
    }
}

Class: 类:

import java.util.InputMismatchException;
import java.util.Scanner;

public class HomeBank 
{
    private double withdrawal, deposit, balance;

    public HomeBank() 
    {
        balance = 100;
        withdrawal = 50;
        deposit = 150;
    }

    public HomeBank(double bal, double with, double de) 
    {
        balance = bal;
        withdrawal = with;
        deposit = de;
    }

    public static void balance() 
    {
        double balance = 250.00d;

        System.out.println("You have $" + balance+"dollars");  
    }

    public static void deposit() 
    {
        Scanner keyboard = new Scanner(System.in);
        double deposit = 0.00d;
        double balance = 250.00d;
        boolean goodput =true;

        do 
        {
            try 
            {
                System.out.print("How much would you like to deposit today? :");
                deposit = keyboard.nextDouble();

                if(deposit > 0.00) 
                {
                    System.out.println("you entered $" +deposit+" dollars");
                    System.out.println("you now have $" + (deposit + balance)+" dollars");
                    goodput = false;
                }
                else
                {
                    System.out.println("\n**Error**\nYou cannot deposit a "
                            + "negative amount\n");
                    System.out.println("Please try again\n");
                    goodput = true;
                }
            }
            catch (InputMismatchException x) 
            {
                System.out.println("I'm sorry but that is an invalid input" +
                        "\nYou will be redirected to the main menu shortly..."+
                        "\n");
                goodput = false;
            }
        } while (goodput == true); 
    }

    public static void withdrawal() 
    {
        Scanner keyboard = new Scanner(System.in);
        double withdrawal = 0.00d;
        double balance = 250.00d;
        boolean goodput = true;

        do 
        {
            try
            {
                System.out.println("How much would you like to withdrawal?");
                withdrawal = keyboard.nextDouble();

                if (withdrawal < 0 || withdrawal > balance) 
                {
                    System.out.println("You have either entered a negative number"
                            + "or trying to withdrawal more than is in your account");
                    System.out.println("You cannot withdrawal more than $"+balance);
                    System.out.println("Please try again");
                    goodput = true;
                }              
                else 
                {
                    System.out.println("You now have $" +(balance-withdrawal)+ "dollars");
                    System.out.println("Thank you for choosing HomeBank\n"
                            + "\nYou will be redirected to the main menu\n");
                    goodput =false;
                }
            }
            catch (InputMismatchException x) 
            {
                System.out.println("I'm sorry but that is an invalid input" +
                        "\nYou will be redirected to the main menu shortly..."+
                        "\n");
                goodput = false;
            }
        } while (goodput == true);    
    }
}

The issue is how you are updating the values. 问题是如何更新值。 In your methods, you have something that goes like the following: 在您的方法中,您有类似以下内容:

double deposit = 0.00d;
double balance = 250.00d;

In there, notice how you are putting the double keyword before the variable. 在那里,请注意你如何在变量之前放置double关键字。 By doing that, you are telling Java to create a new variable named deposit and balance in the local scope. 通过这样做,您告诉Java在本地范围内创建一个名为deposit and balance的新变量。

If you want to modify the fields named deposit and balance , you would definitely have to remove the double keyword before them, and optionally put this. 如果你想修改名为deposit and balance的字段,你肯定必须在它们之前删除double关键字,并且可以选择放置this. in front of the variable. 在变量前面。 It would look something like the following: 它看起来像下面这样:

deposit = 0.00d;
balance = 250.00d;

Or 要么

this.deposit = 0.00d;
this.balance = 250.00d;

You would replace all instances of the variables withdraw , balance , and deposit like that, but only the declaration. 您将替换所有变量withdrawbalancedeposit实例,但仅限于声明。

The reason you don't need to specify the type is because you already define it in the field declaration. 您不需要指定类型的原因是因为您已在字段声明中定义它。 Therefore when you again define it in the method, you are saying to create a new variable with that name and type in the current scope. 因此,当您再次在方法中定义它时,您要创建一个具有该名称的变量并在当前范围中键入。

Edit: As Suyash Limaye said, you will have to remove the static keywords from the methods. 编辑:正如Suyash Limaye所说,你必须从方法中删除static关键字。 Having the static keywords prevents the methods from being able to access the non-static fields, which will cause conflicts. 使用static关键字可以防止方法访问非静态字段,从而导致冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM