ATM分配 - 为什么余额仅反映最近的交易?

时间:2015-06-29 17:46:27

标签: java arrays

我试图制作可以存款,取款和展示余额的自动柜员机。但我对程序的平衡有问题,它只显示最近十次交易的余额,而不是所有交易。

我不允许使用全局变量,其他方法和常量。

以下是它应该如何运作

Earlier transactions:
=====================
1 
2
3
4
5
6
7
8
9
10
=======================
Balance: 55   KR

Earlier transactions:
=====================
2
3
4
5
6
7
8
9
10
11
=======================
Balance: 66   KR

代码

import java.util.Scanner;

public class Bankomat 
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        // Declarer variables
        int[] trans = new int[10];  
        int amount = 0;
        int balance = 0;
        int sum = 0;
        int theChoice = 1;

        while(theChoice != 4)
        {
            theChoice= menu();
            switch(theChoice)
            {
                case 1:
                    System.out.println("\nDu valde \"deposit\"");

                    System.out.print("\nState the value you want to take in: ");
                    sum = in.nextInt();

                    if(sum == 0)
                    {
                        System.out.print("\nYou have given are wrong value.\n");
                    }
                    else
                    {
                        amount = (int) + sum;
                        makeTransactions(trans,amount);
                    }   

                    break;

                case 2:
                    System.out.println("\nDu valde \"withdrawal\"");

                    System.out.print("\nState the value you want to take in: ");
                    sum = in.nextInt();

                    if(sum == 0)
                    {
                        System.out.print("\nDu har angett ett felaktigt belopp.\n");
                    }
                    else
                    {
                        amount = (int) - sum;
                        makeTransactions(trans,amount);
                    }   

                    break;

                case 3:
                    System.out.println("\nDu valde \"Balance\"");
                    showTransactions(trans,balance);
                    break;
                case 4:
                    System.out.println("\nDu valde \"Quit\"");
                    break;
            }
        }
    }

    /**
     * MENU
     * @return val  skickar tillbaka input värdet
     */
    public static int menu()
    {
        Scanner in = new Scanner(System.in);

        int choice = 0;

        // Den här delen kommer att skriva ut menu
        System.out.println("1. deposit");
        System.out.println("2. withdrawal");
        System.out.println("3. Balance");                   
        System.out.println("4. Quit");                                   
        System.out.print("Your choice: ");

        choice = in.nextInt();

        return choice;
    }

    /**
     *  This method will sum up all the ten latest transaction and show the balance 
     * @param trans   array that saves the latest transactions 
     * @param balance Int that sums up all the values
     */
    public static void showTransactions(int[] trans, int balance )
    {
        System.out.println();
        System.out.println("Tidigare transaktioner: ");
        System.out.println("-------------------------------------------\n");

        for(int i = 0; i < trans.length; i++)
        {
            if(trans[i] == 0)
            {
                System.out.print("");
            }

            else
            {
                System.out.print(trans[i] + "\n");
                balance = balance + trans[i];
            }
        }
        System.out.println("-------------------------------------------\n");
        System.out.println("Saldo: " + balance + "KR" + "\n" );
    }

    /**
     * This method saves the latest transaction
     * @param trans array that saves the latest transactions
     * @param amount int that saves the latest transaction
     */
    public static void makeTransactions(int[] trans, int amount)
    {
        int position = findNr(trans);
        if(position == -1)
        {
            moveTrans(trans);
            trans[trans.length - 1] = amount;
        }
        else
        {
            trans[position] = amount;
        }
    }

    /**
     * This metod will look for a empty position 
     * @param trans array that saves the latest transactions
     * @return position 
     */
    private static int findNr(int[] trans) 
    {
        int position = -1;

        for(int i = 0; i < trans.length; i++)
        {
            if (trans[i] == 0)
            {
                position = i;
                break;
            }
        }
        return position;
    }

    /**
     * This method will move the transaction 
     * @param trans array that saves the latest transactions
     */
    private static void moveTrans(int[] trans)
    {
        for(int i = 0; i < (trans.length - 1); i++)
        {
            trans[i] = trans[i + 1];
        }   
    }
}

3 个答案:

答案 0 :(得分:0)

它只显示最后10个,因为您将数组初始化为10个索引

 int[] trans = new int[10]; 

要么变大,要么使用不同的东西。

在这种情况下使用的优秀课程是laravel relationships。您不需要初始化大小,因为它会动态调整大小。

现在,如果您使用向量,但只想打印最近十个事务,那么在条件语句中包围您的print语句

if(i > trans.length - 10)
{
   System.out.print(trans[i] + "\n");
}

对此的共鸣是在你的showTransactions()中,你不仅打印交易,你还添加它们,计算你的余额。我们想要计算你所有交易的余额(因此我们需要将数组更改为矢量或其他具有动态调整大小的类),同时也只打印你需要的内容(因此if语句)

答案 1 :(得分:0)

[已编辑]完整的解决方案:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    // Declarer variables
    int[] trans = new int[10];  
    int amount = 0;
    int balance = 0;
    int sum = 0;
    int theChoice = 1;

    while(theChoice != 4)
    {
        theChoice= menu();
        switch(theChoice)
        {
            case 1:
                System.out.println("\nDu valde \"deposit\"");

                System.out.print("\nState the value you want to take in: ");
                sum = in.nextInt();

                if(sum == 0)
                {
                    System.out.print("\nYou have given are wrong value.\n");
                }
                else
                {
                    amount = (int) + sum;
                    balance += amount;
                    makeTransactions(trans,amount);
                }   

                break;

            case 2:
                System.out.println("\nDu valde \"withdrawal\"");

                System.out.print("\nState the value you want to take in: ");
                sum = in.nextInt();

                if(sum == 0)
                {
                    System.out.print("\nDu har angett ett felaktigt belopp.\n");
                }
                else
                {
                    amount = (int) - sum;
                    balance += amount;
                    makeTransactions(trans,amount);
                }   

                break;

            case 3:
                System.out.println("\nDu valde \"Balance\"");
                showTransactions(trans,balance);
                break;
            case 4:
                System.out.println("\nDu valde \"Quit\"");
                break;
        }
    }
}

/**
 * MENU
 * @return val  skickar tillbaka input värdet
 */
public static int menu()
{
    Scanner in = new Scanner(System.in);

    int choice = 0;

    // Den här delen kommer att skriva ut menu
    System.out.println("1. deposit");
    System.out.println("2. withdrawal");
    System.out.println("3. Balance");                   
    System.out.println("4. Quit");                                   
    System.out.print("Your choice: ");

    choice = in.nextInt();

    return choice;
}

/**
 *  This method will sum up all the ten latest transaction and show the balance 
 * @param trans   array that saves the latest transactions 
 * @param balance Int that sums up all the values
 */
public static void showTransactions(int[] trans, int balance )
{
    System.out.println();
    System.out.println("Tidigare transaktioner: ");
    System.out.println("-------------------------------------------\n");

    for(int i = 0; i < trans.length; i++)
    {
        if(trans[i] == 0)
        {
            System.out.print("");
        }

        else
        {
            System.out.print(trans[i] + "\n");
        }
    }
    System.out.println("-------------------------------------------\n");
    System.out.println("Saldo: " + balance + "KR" + "\n" );
}

/**
 * This method saves the latest transaction
 * @param trans array that saves the latest transactions
 * @param amount int that saves the latest transaction
 */
public static void makeTransactions(int[] trans, int amount)
{
    int position = findNr(trans);
    if(position == -1)
    {
        moveTrans(trans);
        trans[trans.length - 1] = amount;
    }
    else
    {
        trans[position] = amount;
    }
}

/**
 * This metod will look for a empty position 
 * @param trans array that saves the latest transactions
 * @return position 
 */
private static int findNr(int[] trans) 
{
    int position = -1;

    for(int i = 0; i < trans.length; i++)
    {
        if (trans[i] == 0)
        {
            position = i;
            break;
        }
    }
    return position;
}

/**
 * This method will move the transaction 
 * @param trans array that saves the latest transactions
 */
private static void moveTrans(int[] trans)
{
    for(int i = 0; i < (trans.length - 1); i++)
    {
        trans[i] = trans[i + 1];
    }   
}

答案 2 :(得分:0)

问题在于您初始化int[] trans的方式。您只存储前10个交易。您应该使用ArrayList代替,因为它是动态的。

ArrayList<Integer> trans = new ArrayList<Integer>,然后向您的数组添加新交易