From 31306337f26308cafbc9fb049d57351451186592 Mon Sep 17 00:00:00 2001 From: poopki Date: Sun, 17 Jul 2022 20:51:40 +0900 Subject: [PATCH] ArrayEvent handler - Overflow bug fix --- Account.java | 26 ++++++++++++++++++++------ AccountArray.java | 20 ++++++++++++++------ AccountEventHandler.java | 27 +++++++++++++++++++++------ 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/Account.java b/Account.java index 40ff105..50707ae 100644 --- a/Account.java +++ b/Account.java @@ -1,19 +1,33 @@ package org.poopki.duckdns.user_db; + public class Account { private int m_Balance; - + private int MAX_BALANCE = 2147483647; public Account(int Balance){ // Account Balance 초기화 m_Balance = Balance; } - public void setBalance(int Amount){ m_Balance = Amount;} + public boolean setBalance(Long Amount){ + if(Amount= Amount){ - m_Balance -= Amount; + m_Balance -= Amount.intValue(); return true; } return false; diff --git a/AccountArray.java b/AccountArray.java index a0fb005..1898e7b 100644 --- a/AccountArray.java +++ b/AccountArray.java @@ -7,15 +7,23 @@ import java.util.UUID; public class AccountArray { private static Map m_AccountArray = new HashMap(); - public boolean transfer(UUID Src, UUID Des, int Amount){ // 송금 method + public int transfer(UUID Src, UUID Des, Long Amount){ // 송금 method Account m_Src = m_AccountArray.get(Src); Account m_Des = m_AccountArray.get(Des); if(m_Src.withdraw(Amount)){ //인출 성공시 입금 - m_Des.credit(Amount); - return true; + if(m_Des.credit(Amount)) + { + return 1; + } + else{ + m_Src.credit(Amount); + return 2; + } + } + else { + return 3; } - return false; } public int getAccountInfo(UUID uuid) { // DB backup시 account 정보 접근 method @@ -27,8 +35,8 @@ public class AccountArray { m_AccountArray.put(uuid, new Account(Amount)); } - public void setAccountInfo(UUID uuid, int Amount) { + public boolean setAccountInfo(UUID uuid, Long Amount) { Account m_Account = m_AccountArray.get(uuid); - m_Account.setBalance(Amount); + return m_Account.setBalance(Amount); } } diff --git a/AccountEventHandler.java b/AccountEventHandler.java index fcad465..3df1eca 100644 --- a/AccountEventHandler.java +++ b/AccountEventHandler.java @@ -33,8 +33,12 @@ public class AccountEventHandler implements CommandExecutor { if (op != null) { if (args.length > 2) { if (isStringDouble(args[2])) { - m_AccountArray.setAccountInfo(op.getUniqueId(), Integer.parseInt(args[2])); - p.sendMessage(op.getName() + "님의 잔액을" + Integer.parseInt(args[2]) + "원으로 설정하였습니다."); + if(m_AccountArray.setAccountInfo(op.getUniqueId(), Long.parseLong(args[2]))){ + p.sendMessage(op.getName() + "님의 잔액을" + Integer.parseInt(args[2]) + "원으로 설정하였습니다."); + } else{ + p.sendMessage("최대 금액 이상으로 설정할 수 없습니다."); + } + } else { p.sendMessage("설정 금액은 숫자만 가능합니다."); } @@ -64,10 +68,21 @@ public class AccountEventHandler implements CommandExecutor { if (op != null) { if (isStringDouble(args[2])) { if(p.getUniqueId() != op.getUniqueId()) { - if (m_AccountArray.transfer(p.getUniqueId(), op.getUniqueId(), Integer.parseInt(args[2]))) - p.sendMessage(op.getName() + "님에게 " + Integer.parseInt(args[2]) + "원을 이체하였습니다."); - else - p.sendMessage("잔액이 부족합니다."); + switch(m_AccountArray.transfer(p.getUniqueId(), op.getUniqueId(), Long.parseLong(args[2]))) + { + case 1: { + p.sendMessage(op.getName() + "님에게 " + Integer.parseInt(args[2]) + "원을 이체하였습니다."); + break; + } + case 2: { + p.sendMessage("잔액은 최대 금액을 초과할 수 없습니다."); + break; + } + case 3:{ + p.sendMessage("잔액이 부족합니다."); + break; + } + } } else{ p.sendMessage("자기 자신에게는 이체할 수 없습니다."); }