简单的ATM任务,阵列问题。

时间:2016-05-31 08:29:34

标签: java arrays

我想在事务数组变满时覆盖它的第一个元素。因此,当我打印数组时,它总是显示最新的事务。

我认为问题出在moveTrans方法或findNr方法中,但我不确定,我无法弄清楚出了什么问题。

代码:

import java.util.Scanner;
import java.util.*;


public class BankoTest {

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

    int amount = 0;
    int choice = 0;
    int [] trans = new int[4];
    int sum;
    int balance = 0;

    while (choice != 4)
    {
        choice = menu();
        switch(choice)
        {
        case 1:// 
            System.out.print("Deposit. Amount? :");
            amount = scan.nextInt();
            balance = balance + amount;
            makeTransactions(trans, amount);
            break;

        case 2:// 
            System.out.print("Withdra. Amount?");
            amount = scan.nextInt();
            balance = balance - amount;
            makeTransactions(trans, -amount);
            break;

        case 3:
            showTransactions(trans, balance);
            break;

        case 4:
            System.out.println("Thank you. ");
            break;
        }   

    }
}


public static int menu()
{
    Scanner scan = new Scanner(System.in);
    int choice = 0;

    System.out.println("1. Deposit ");
    System.out.println("2. Withdraw ");
    System.out.println("3. Saldo ");
    System.out.println("4. End ");
    System.out.println();
    System.out.println("Choice: ");

    choice = scan.nextInt();
    return choice;
}

public static void showTransactions(int [] trans, int balance)
{
    System.out.println();
    System.out.println("Transactions summary :");
    System.out.println();

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

        else
        { 
            System.out.print(trans[i] + "\n");
            balance = balance + trans[i];

        }

    }
    // Printing saldo. 
    System.out.println();
    System.out.println("Current balance: " + balance + " kr" + "\n" );
    System.out.println();   

}


//Puts amount last among the transactions that are stored in the array. Using the findNr method to find the first available spot 
//in the array. moveTrans is used to make room for the new transaction when the array is full.

public static void makeTransactions(int [] trans, int amount )
{
    int position = findNr(trans);
    if(position == -1)
    {
        moveTrans(trans);
        position = findNr(trans);
        trans[position] = amount;
    }
    else
    {
        trans[position] = amount;
    }   

}

public static int findNr(int [] trans)
{
    int position = -1;

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

public static void moveTrans(int [] trans)
{
    for(int i = 0; i < trans.length-1; i++)

        trans[0] = trans[i + 1] ;
}
}

2 个答案:

答案 0 :(得分:0)

请检查一下它可能对你有所帮助

public static void main(String [] args){         扫描仪扫描=新扫描仪(System.in);

    int amount = 0;
    int choice = 0;
    int[] trans = new int[4];
    int sum;
    int balance = 0;

    while (choice != 4) {

        choice = menu();

        switch (choice) {
        case 1://
            System.out.print("Deposit. Amount? :");
            amount = scan.nextInt();
            balance = balance + amount;
            makeTransactions(trans, amount);
            break;

        case 2://
            System.out.print("Withdra. Amount?");
            amount = scan.nextInt();
            balance = balance - amount;
            makeTransactions(trans, -amount);
            break;

        case 3:
            showTransactions(trans, balance);
            break;

        case 4:
            System.out.println("Thank you. ");
            break;
        }

    }
}

public static int menu() {
    Scanner scan = new Scanner(System.in);
    int choice = 0;

    System.out.println("1. Deposit ");
    System.out.println("2. Withdraw ");
    System.out.println("3. Saldo ");
    System.out.println("4. End ");
    System.out.println();
    System.out.println("Choice: ");

    choice = scan.nextInt();
    return choice;
}

public static void showTransactions(int[] trans, int balance) {
    System.out.println();
    System.out.println("Transactions summary :");
    System.out.println();

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

        else {
            System.out.print(trans[i] + "\n");
            // balance = trans[i];

        }

    }
    // Printing saldo.
    System.out.println();
    System.out.println("Current balance: " + balance + " INR" + "\n");
    System.out.println();

}

// Puts amount last among the transactions that are stored in the array.
// Using the findNr method to find the first available spot
// in the array. moveTrans is used to make room for the new transaction when
// the array is full.

public static void makeTransactions(int[] trans, int amount) {
    int position = findNr(trans);

    if (position == -1) {
        moveTrans(trans);
        System.out.println("Your transaction limit is over.");
        // position = findNr(trans);
        // System.out.println("Position -------> "+position);
        // trans[position] = amount;
    } else {
        trans[position] = amount;
    }

}

public static int findNr(int[] trans) {
    int position = -1;

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

public static void moveTrans(int[] trans) {
    System.out.println("------Your Transaction Details----------");
    for (int i = 0; i < trans.length; i++) {

        System.out.println("Transation " + (i + 1) + " :: " + trans[i]);

        trans[0] = trans[i];
    }
    System.out.println("----------------------------------------");
}

答案 1 :(得分:0)

仅修改以下方法

    public static void makeTransactions(int[] trans, int amount) {
        int position = findNr(trans);
        if (position == -1) {
            moveTrans(trans);
            position = findNr(trans);
            trans[position] = amount;
        } else {
            if (position != 0 && position == trans.length - 1) {
            // shift the elements back
            for (int i = 0; i < position; i++)
                trans[i] = trans[i] + 1;
            trans[position - 1] = amount;
        }else
            trans[position] = amount;
        }

}