正の整数を入力し残高以内なら引き出すという動作をさせたいのですが、残高以内の数を入力しても残高を超えているという結果が出てしまいました。
文字数の制限上、引き出しの部分のみのコードになります。
他に必要なコード等があればご指摘お願いします。
java
1import java.util.Hashtable; 2 3public class ExtendedBank{ 4 private Hashtable<String,Account>customer; // 口座リスト 5 private int balance; // 残高格納用 6 7 public ExtendedBank(){ // 口座リスト初期化 8 customer = new Hashtable<String,Account>(); 9 } 10 public int withdraw(String name,int amount){ // 引き出し 11 Account myaccount = customer.get(name); 12 balance = myaccount.showBalance(); 13 if(myaccount != null){ 14 if(amount <= 0) 15 return -3; 16 else if(amount > balance) 17 return -1; 18 else{ 19 myaccount.withdraw(amount); 20 return 0; 21 } 22 }else return -7; 23 } 24 public int showBalance(String name){ // 残高照会 25 Account myaccount = customer.get(name); 26 if(customer.get(name) != null) return myaccount.showBalance(); 27 else return -7; 28 } 29 30 // ExtendedBank要素 31 // 引き出し 32 public int withdraw(String name,String amount){ // name:口座名 33 int result; 34 int w_result; 35 try{ 36 result = Integer.parseInt(amount); 37 }catch(NumberFormatException e){ 38 if(showBalance(name) == -7) 39 return -7; 40 else 41 return -4; 42 } 43 result = Integer.parseInt(amount); 44 if(result <= 0) 45 return -3; 46 else if(result > balance) 47 return -1; 48 else{ 49 w_result=withdraw(name,result); 50 return w_result; 51 } 52 } 53}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/18 08:20
2022/01/18 08:24