前提・実現したいこと
Javaを用いて簡単な銀行口座のプログラムを作っています。ユーザーがDepositを選択するとユーザーの好きな額を入金し、withdrawalを選択すると好きな額を引き下ろし、output balanceを選択するとその時の口座に入っているお金を表示し、Output all depositsを選択するとその時までの入金履歴を表示し、Output all withdrawalsを選択するとその時までの出金履歴を表示します。現在、入金履歴と出金履歴の仕方がよく分からなくこちらで質問させて頂きました。
下記のコードでdeposit.lengthとwithdrawal.lengthが0だと思うので入金履歴と出金履歴が表示されないのだと思います。そしてcountDpとcountWdのカウントの仕方が間違えていると思います。正しくするにはどのようにすれば良いでしょうか?教えて頂きたいです。
このプログラムの問題文にarrayを使えと書いてあるのでarrayを使う前提でお願いします。
該当のソースコード
Java
1import java.util.Scanner; 2import java.io.*; 3import java.io.FileNotFoundException; 4import java.io.PrintWriter; 5 6class Account { 7 private float balance; 8 9 private int countDp; 10 private int countWd; 11 12 float[] deposit = new float[10]; 13 float[] withdrawal = new float[10]; 14 15 Scanner input = new Scanner(System.in); 16 17 public Account() { 18 balance = 0; 19 countDp = 0; 20 countWd = 0; 21 } 22 23 private float[] enhanceArray(float[] array) { 24 float[] newArray = new float[array.length + 10]; 25 for (int i = 0; i < array.length; i++) { 26 newArray[i] = array[i]; 27 } 28 return newArray; 29 } 30 31 32 public float depositing() { 33 System.out.println("Enter the amount you would like to deposit: "); 34 35 float depositAmount = input.nextFloat(); 36 37 if (countDp == deposit.length) { 38 deposit = enhanceArray(deposit); 39 } 40 deposit[countDp] = depositAmount; 41 balance += depositAmount; 42 43 countDp += 1; 44 return balance; 45 } 46 47 public float withdrawing() { 48 System.out.println("Enter the amount you would like to withdraw: "); 49 50 float withdrawalAmount = input.nextFloat(); 51 52 if (balance >= withdrawalAmount) { 53 if (countWd == deposit.length) { 54 withdrawal = enhanceArray(withdrawal); 55 } 56 withdrawal[countDp] = withdrawalAmount; 57 balance -= withdrawalAmount; 58 countWd += 1; 59 } else { 60 System.out.println("Less balance, transaction failed."); 61 } 62 63 return balance; 64 } 65 66 public float balanceOfAccount() { 67 return balance; 68 } 69 70 public void outputAllDeposits() { 71 72 System.out.println("Outputting all your deposits:"); 73 74 for (int i = 0; i < countDp; i++) { 75 deposit[i] = input.nextFloat(); 76 77 if(deposit[i] >= 0.0f) { 78 System.out.println("$" + deposit[i]); 79 } 80 } 81 } 82 83 public void outputAllWithdraws() { 84 System.out.println("Outputting all your withdrawals:"); 85 86 for (int i = 0; i < countWd; i++) { 87 withdrawal[i] = input.nextFloat(); 88 89 if(withdrawal[i] >= 0.0f) { 90 System.out.println("$" + withdrawal[i]); 91 } 92 } 93 } 94 95 public void outputToTextFile() { 96 try { 97 PrintWriter pw = new PrintWriter("bankAcount.txt"); 98 99 pw.println("Your balance: " + balanceOfAccount()); 100 101 pw.println("Outputting all your withdrawals:"); 102 103 for (int i = 0; i < withdrawal.length; i++) { 104 withdrawal[i] = input.nextFloat(); 105 106 if(withdrawal[i] >= 0.0f) { 107 pw.println(withdrawal[i]); 108 } 109 } 110 111 pw.println("Outputting all your deposits:"); 112 113 for (int i = 0; i < deposit.length; i++) { 114 deposit[i] = input.nextFloat(); 115 116 if(deposit[i] >= 0.0f) { 117 pw.println(deposit[i]); 118 } 119 } 120 121 pw.close(); 122 } catch (IOException ex) { 123 ex.printStackTrace(); 124 } 125 } 126} 127 128public class Main { 129 130 public static void main(String[] args) throws Exception{ 131 int choice = 0; 132 Account account = new Account(); 133 134 Scanner in = new Scanner(System.in); 135 136 137 do { 138 System.out.println("**************************************\n" + 139 "* Bank Account Program: *\n" + 140 "* Enter # to run program or Quit *\n" + 141 "* 1) Make a Deposit *\n" + 142 "* 2) Make a Withdrawal *\n" + 143 "* 3) Output balance *\n" + 144 "* 4) Output all deposits *\n" + 145 "* 5) Output all withdrawals *\n" + 146 "* 6) Quit *\n" + 147 "**************************************\n"); 148 149 choice = in.nextInt(); 150 151 switch (choice) { 152 case 1: 153 account.depositing(); 154 break; 155 case 2: 156 account.withdrawing(); 157 break; 158 case 3: 159 System.out.println("Your balance is " + account.balanceOfAccount()); 160 161 break; 162 case 4: 163 164 account.outputAllDeposits(); 165 break; 166 case 5: 167 account.outputAllWithdraws(); 168 break; 169 170 case 6: 171 System.out.println("Thank you for using this Bank."); 172 break; 173 174 default: 175 break; 176 } 177 } while(choice != 6); 178 179 account.outputToTextFile(); 180 } 181}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/18 21:15
2018/11/19 02:25
2018/11/19 06:37