简体   繁体   English

如何跟踪银行帐户上的余额?

[英]How do I keep track of the balance on a bank account?

I am going to create a program that keeps track of the balance on a bank account. 我将创建一个跟踪银行帐户余额的程序。 The program shall use a loop that continues until the user choses to exit by answering no to the question Do you want to continue? 该计划应使用一直持续到用户的动产回答没有 做你想要继续的问题退出循环 .

In the loop the user shall be asked to enter an amount (positive for deposit and negative for withdraw). 在循环中,将要求用户输入一个金额(存款为正数,取款为负数)。 The amount shall be added/subtracted from an account balance variable. 该金额应从帐户余额变量中添加/减去。 All deposits/withdraws shall be saved as a history so that we can print it later. 所有存款/提款应保存为历史记录,以便我们以后打印。 When the user choses to exit the loop the current account balance together with the account history (from the array/ArrayList) shall be printed. 当用户选择退出循环时,应打印当前帐户余额以及帐户历史记录(来自数组/ ArrayList)。

Now, I want to use an array with ten slots for the history feature. 现在,我想将具有十个插槽的阵列用于历史记录功能。

My question is how can I keep track of the all deposit, withdraw and current account balance (using an array with ten slots for the history feature) so that I can print it out while the user exits the program? 我的问题是,如何跟踪所有存款,取款和经常账户余额(使用具有十个插槽的历史功能阵列),以便在用户退出程序时可以将其打印出来?

My code: 我的代码:

BankApp class: BankApp类:

package bankapp;

import java.util.Scanner;

public class BankApp {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        askingUser au = new askingUser();

        System.out.println("WELCOME TO OUR BANK!\nYou have 100 SEK by default in your account.");

        while (true) {

            au.userInput();

            System.out.println("Do you want to continue? Answer by Yes or No.");
            String yesOrNo = input.next();

            if (yesOrNo.equalsIgnoreCase("yes")) {

                au.userInput();

            } else if (yesOrNo.equalsIgnoreCase("no")) {
                System.out.println("History: ");

                //print out the transaction history
                System.exit(0);

            } else {

                System.out.println("Invalid character input.");

            }

        }

    }
}

askingUser class: askUser类:

package bankapp;

import java.util.Scanner;

public class askingUser {

    Scanner input = new Scanner(System.in);
    double initialBal = 100;

    public void userInput() {
        System.out.println("Enter your amount: (+ve for deposit & -ve for withdraw)");
        double inputAmount = input.nextDouble();

        if (inputAmount >= 0) {

            double newPosAm = initialBal + inputAmount;
            System.out.println("Your current balance is: " + newPosAm + " SEK");

        } else {

            double newNegAm = initialBal + inputAmount;
            System.out.println("Your current balace is: " + newNegAm + " SEK");
        }

    }

}

If you use an array, you have to keep track of the number of elements stored inside and resize the array when necessary. 如果使用数组,则必须跟踪存储在其中的元素的数量,并在必要时调整数组的大小。 The easiest way would be to keep the history as strings in ArrayList . 最简单的方法是将历史记录保留为ArrayList中的字符串。 You would add one message to that list per transaction: 您将在每笔交易中向该列表添加一条消息:

ArrayList<String> history = new ArrayList<String>();

void addToHistory(String transaction) {
    history.add(transaction);
}

void printHistory() {
    for(String s : history) {
        System.out.println(s);
    }
}

addToHistory("Withdrawal: 100 SEK" );
addToHistory("Deposit: 200 SEK" );
printHistory();

You need a queue to do that. 您需要执行队列 However, for a simple, fast and primitive implementation you can: 但是,对于简单,快速和原始的实现,您可以:

  • Define an object called Transaction ( deposit - double, withdraw - double, current account balance - double ) 定义一个名为Transaction的对象( deposit - double, withdraw - double, current account balance - double
  • Add a List of Transaction s into askingUser class as an attribute . TransactionList作为属性添加到askingUser类中。 I strongly recommend renaming the class name to AskingUser to make it seen as object. 我强烈建议将类名重命名AskingUser ,以使其视为对象。
  • At each operation add a new Transaction to end of the List you just added. 在每个操作中,将新Transaction添加到刚添加的List末尾。
  • At exit, print out the last -say- 10 elements of the List ; 在出口处,打印出List的最后10个元素; you can reach it through askingUser object. 您可以通过askingUser对象访问它。 You can also define a function in askingUser class to print out the last 10 elements, if you make the function work according to selected number of elements, you can add number of Transaction s to the function's inputs. 您还可以在askingUser类中定义一个函数以打印出最后10个元素,如果使函数根据选定的元素数工作,则可以将Transaction的数量添加到函数的输入中。

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

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