2016-05-31 3 views
0

トランザクション配列が満杯になったときに、最初の要素を上書きしたいとします。だから私は配列を印刷するとき、常に最新のトランザクションを表示しています。単純なATMタスク、配列に関する問題。

私は問題が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] ; 
} 
} 
+0

moveTransは、以下のfindNrが配信されないようにする必要があります-1だけでは、以下の方法を変更します。その 'trans [0]'も間違っています。 –

+0

@JoopEggen、どういう意味ですか?私は何を変えるべきですか? – forTheLoveOfJava

答えて

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; 
     } 

} 
+0

それは私の問題を解決しました。私はちょうどこの行を変えました:trans [i] = trans [i] + 1;これは、trans [i] = trans [i + 1]である。したがって、追加するのではなく、1つのステップを実行します。 – forTheLoveOfJava

0

それは公共の静的な無効メイン(文字列[] 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("----------------------------------------"); 
}