学校の課題なのですが、預金をする際に整数を入力した際にこのエラーが出ました。
調べて自分でコードを見直してみたり、書き換えたりなどしているのですが解決の兆しが見えなかったので質問投稿させていただきます。
至らぬ点が多いため、コード不足や補足事項必要でしたらご指摘よろしくお願いいたします。
文字数の制限のため、預金処理以降のコードしか載せられませんでした。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/17 06:31
2022/01/17 17:47
2022/01/18 08:10
2022/01/18 08:23
回答3件
0
処理結果を示す戻りを無くして例外にしたり。(一応戻りの値は例外内に code として保存)
java
1package teratail_java.q_c9mdeundv32wxi; 2 3import java.util.HashMap; 4 5import teratail_java.q_c9mdeundv32wxi.Account.AmountException; 6 7public class ExtendedBank { 8 /** 口座例外 */ 9 public static class AccountException extends Exception { 10 public final int code; 11 public AccountException(String message, int code) { 12 super(message+" (code="+code+")"); 13 this.code = code; 14 } 15 } 16 17 /** 口座マップ */ 18 private HashMap<String,Account> customer = new HashMap<>(); 19 20 /** 21 * 口座オブジェクトを取り出す 22 * @param name 口座名 23 * @return Account オブジェクト 24 * @throws AccountException 口座が無かった場合 25 */ 26 private Account getAccount(String name) throws AccountException { 27 Account account = customer.get(name); 28 if(account == null) throw new AccountException(name+" NOT exists.", -7); 29 return account; 30 } 31 32 /** 33 * 金額文字列数値化 34 * @param amount 金額 35 * @throws AmountException 金額が処理出来ない場合 36 */ 37 private int parseAmount(String amount) throws AmountException { 38 try { 39 return Integer.parseInt(amount); 40 } catch(NumberFormatException e) { 41 throw new AmountException(amount, -4, e); 42 } 43 } 44 45 /** 46 * 口座開設 47 * @param name 口座名 48 * @throws AccountException 指定口座が既に有った場合 49 */ 50 public void open(String name) throws AccountException { 51 if(customer.containsKey(name)) throw new AccountException(name+" exists.", -7); 52 customer.put(name, new Account(name)); 53 } 54 55 /** 56 * 口座解約 57 * @param name 口座名 58 * @throws AccountException 指定口座が無かった又は残高が残っている場合 59 */ 60 public void close(String name) throws AccountException { 61 Account account = getAccount(name); 62 if(account.showBalance() != 0) throw new AccountException(name+" has balance.", -1); 63 customer.remove(name); 64 } 65 66 /** 67 * 預金 68 * @param name 口座名 69 * @param amount 金額 70 * @throws AccountException 指定口座が無かった場合 71 * @throws AmountException 金額が処理出来ない場合 72 */ 73 public void deposit(String name, String amount) throws AccountException, AmountException { 74 getAccount(name).deposit(parseAmount(amount)); 75 } 76 77 /** 78 * 預金 79 * @param name 口座名 80 * @param amount 金額 81 * @throws AccountException 指定口座が無かった場合 82 * @throws AmountException 金額が処理出来ない場合 83 */ 84 public void deposit(String name, int amount) throws AccountException, AmountException { 85 getAccount(name).deposit(amount); 86 } 87 88 /** 89 * 引き出し 90 * @param name 口座名 91 * @param amount 金額 92 * @throws AccountException 指定口座が無かった場合 93 * @throws AmountException 金額が処理出来ない場合 94 */ 95 public void withdraw(String name, String amount) throws AccountException, AmountException { 96 getAccount(name).withdraw(parseAmount(amount)); 97 } 98 99 /** 100 * 引き出し 101 * @param name 口座名 102 * @param amount 金額 103 * @throws AccountException 指定口座が無かった場合 104 * @throws AmountException 金額が処理出来ない場合 105 */ 106 public void withdraw(String name, int amount) throws AccountException, AmountException { 107 getAccount(name).withdraw(amount); 108 } 109 110 /** 111 * 残高照会 112 * @param name 口座名 113 * @return 預金残高 114 * @throws AccountException 指定口座が無かった場合 115 */ 116 public int showBalance(String name) throws AccountException { 117 return getAccount(name).showBalance(); 118 } 119}
java
1package teratail_java.q_c9mdeundv32wxi; 2 3public class Account { 4 /** 金額例外 */ 5 public static class AmountException extends Exception { 6 public final int code; 7 public AmountException(int amount, int code) { 8 super("amount="+amount+" (code="+code+")"); 9 this.code = code; 10 } 11 public AmountException(String amount, int code, Throwable cause) { 12 super("amount='"+amount+"' (code="+code+")", cause); 13 this.code = code; 14 } 15 } 16 17 /** 口座名 */ 18 public final String name; 19 /** 残高 */ 20 private int balance; 21 22 /** 23 * 口座 24 * @param name 口座名 25 */ 26 public Account(String name) { 27 this.name = name; 28 balance = 0; 29 } 30 31 /** 32 * 入金 33 * @param amount 金額 34 * @throws AmountException 金額が処理できない値の場合 35 */ 36 public void deposit(int amount) throws AmountException { 37 if(amount <= 0) throw new AmountException(amount, -3); 38 balance += amount; 39 } 40 41 /** 42 * 出金 43 * @param amount 金額 44 * @throws AmountException 金額が処理できない値の場合 45 */ 46 public void withdraw(int amount) throws AmountException { 47 if(amount <= 0 || balance < amount) throw new AmountException(amount, amount<=0?-3:-1); 48 balance -= amount; 49 } 50 51 /** 52 * 残高参照 53 * @return 残高 54 */ 55 public int showBalance() { 56 return balance; 57 } 58}
投稿2022/01/17 19:55
総合スコア12632
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
ベストアンサー
java
1 public int deposit(String name,String amount){ // name:口座名 2 ... 3 d_result=deposit(name,amount);
amount
のパーズ結果が正のとき、まったく同じ引数で自分自身を呼び出していて、無限再帰になっています。
d_result = deposit(name, result);
としたかったんじゃないでしょうか。
投稿2022/01/17 06:40
総合スコア20845
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/17 07:29
2022/01/17 08:07
2022/01/17 08:13
2022/01/17 08:30
2022/01/17 14:11
2022/01/17 17:53 編集
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。