[英]ATM assignment - why does balance reflect only most recent transactions?
我正在嘗試制作一個可以進行存款、取款和顯示余額的 ATM。 但是我對程序的余額有問題,它只顯示最近十筆交易的余額,而不是所有交易的余額。
我不允許使用全局變量、其他方法和常量。
這是它應該如何工作
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];
}
}
}
它只顯示最后 10 個,因為您將數組初始化為 10 個索引
int[] trans = new int[10];
要么讓它更大,要么使用不同的東西。
在這種情況下使用的一個很好的類是vector 。 您不需要初始化大小,因為它會動態調整自身大小。
現在,如果您使用向量,但只想打印最后 10 個交易,然后將您的打印語句括在條件中
if(i > trans.length - 10)
{
System.out.print(trans[i] + "\n");
}
原因在於您的 showTransactions() 不僅打印交易,還添加它們,計算您的余額。 我們想用您的所有交易計算您的余額(因此我們需要將您的數組更改為向量或其他具有動態調整大小的類),同時也只打印您需要的內容(因此是 if 語句)
[編輯]完整的解決方案:
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];
}
}
問題在於您如何初始化int[] trans
。 您只存儲前 10 筆交易。 您應該改用ArrayList
,因為它是動態的。
ArrayList<Integer> trans = new ArrayList<Integer>
,然后將新事務添加到您的數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.