Skip to content

Instantly share code, notes, and snippets.

@jb7959
Created December 2, 2016 11:46
Show Gist options
  • Save jb7959/e3f8d9fbc36cc12dba6f5f5457b839dc to your computer and use it in GitHub Desktop.
Save jb7959/e3f8d9fbc36cc12dba6f5f5457b839dc to your computer and use it in GitHub Desktop.
동전 갯수 세어주는 클래스
/**
* Created by jerry on 2016-12-02.
*/
public class walet {
int fiveH = 0; // 500원 짜리
int oH = 0; // 100원 짜리
int fT = 0; // 50원 짜리
int ten = 0; // 10원 짜리
public String cal(int amount) {
int temp = amount; //잔액 임시저장용
int rfiveH, roH, rft, rten;
rfiveH = cal(temp, "500");
while (rfiveH > fiveH) {
rfiveH--;
}
temp -= rfiveH * 500;
System.out.println(rfiveH + " " + temp);
roH = cal(temp, "100");
while (roH > oH) {
roH--;
}
temp -= roH * 100;
System.out.println(roH + " " + temp);
rft = cal(temp, "50");
while (rft > fT) {
rft--;
}
temp -= rft * 50;
System.out.println(rft + " " + temp);
rten = cal(temp, "10");
while (rten > ten) {
rten--;
}
temp -= rten * 10;
System.out.println(rten + " " + temp);
///////////////잔액이 남을 경우
if (temp != 0 && (ten - rten) > 0) {
rten += 1;
System.out.println("1");
}
if (temp > 0 && (oH - roH) > 0) {
roH += 1;
System.out.println("2");
}
if (temp > 0 && (fT - rft) > 0) {
rft += 1;
System.out.println("3");
}
if (temp > 0 && (fiveH - rfiveH) > 0) {
rfiveH += 1;
System.out.println("4");
}
String result = "500:" + rfiveH + "개" + "100:" + roH + "개" + "50:" + rft + "개" + "10:" + rten + "개";
return result;
}
//todo coinType은 Enum으로 짜는게 좋을 듯, 아래의 경우 반드시 숫자로 500,100,50,10 만 허용
private int cal(int amount, String coinType) {
// if(!coinType.equals("500")||!coinType.equals("100")||!coinType.equals("50")||!coinType.equals("10")){System.out.println("코인타입을 제대로 넣어주세요."); }
int coinTypeAmount = Integer.parseInt(coinType);
int result = 0;
result = amount / coinTypeAmount;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment