프로그래머의 삶 Programmer's Life/Java!!

카드론 단말기 프로그램 & 그 TestCase

Oliver's World 2008. 11. 1. 15:31
728x90

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SUT {
 private String cardNum;
 private String passwd;
 private int outMoney = 0;
 private int money = 0;
 private int cardInt = 0;

 public SUT() {
  this.setCardInt(0);
  this.setPasswd("8523");
  this.setMoney(1000000);
 }

 public String getCardNum() {
  return cardNum;
 }

 public void setCardNum(String cardNum) {
  this.cardNum = cardNum;
 }

 public String getPasswd() {
  return passwd;
 }

 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }

 public int getOutMoney() {
  return outMoney;
 }

 public void setOutMoney(int outMoney) {
  this.outMoney = outMoney;
 }

 public int getMoney() {
  return money;
 }

 public void setMoney(int money) {
  this.money = money;
 }

 public int getCardInt() {
  return cardInt;
 }

 public void setCardInt(int cardInt) {
  this.cardInt = cardInt;
 }

 // char to number
 public int charToNum(char a) {
  int b = (int) (a - '0');

  return b;
 }

 // carry
 public int carry(int num) {
  int b = num % 10;
  int c = (num - b) / 10;

  return b + c;
 }

 // 카드 룬 알고리즘
 public boolean cardrun(String str) {
  int count = 0;
  int b = str.length();

  while (!(b-- == 0)) {
   count++;
   if (count % 2 == 0) {
    if ((2 * charToNum(str.charAt(b))) > 9) {
     this.setCardInt(this.getCardInt()
       + carry(2 * charToNum(str.charAt(b))));
    } else {
     this.setCardInt(this.getCardInt()
       + (2 * charToNum(str.charAt(b))));
    }
   } else
    this.setCardInt(this.getCardInt() + charToNum(str.charAt(b)));
  }
  if (this.getCardInt() % 10 == 0)
   return true;
  else
   return false;
 }

 // 사용자로부터 입력을 받는다.
 public String getInput() {
  BufferedReader in = null;
  String str = "";
  try {
   in = new BufferedReader(new InputStreamReader(System.in));
   str = in.readLine();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return str;
 }

 // 입력된 것이 숫자인지 검증하는 함수
 public boolean check_itNumber(String str) {
  for (int x = 0; x < str.length(); x++) { // 문자열 내 문자 검사(1글자씩 나누어 검증)
   if (!(str.charAt(x) >= '0' && str.charAt(x) <= '9')) {
    System.out.println("Input only Number~\n");
    return false;
   }
  }
  return true;
 }

 // 숫자의 길이 검증 함수
 public boolean LenthValidation(String str) {
  if (str.length() > 3 && str.length() <= 16) {
   return true;
  }
  System.out.println("Card Number Length Error\n Input plz~\n");
  return false;
 }

 // 카드 번호 입력 검증 집합
 public boolean ValidationCard(String str) {
  if (LenthValidation(str)) {
   if (check_itNumber(str)) {
    if (cardrun(str))
     return true;
   }
  }
  System.out.println("카드번호오류");
  return false;
 }

 public boolean ValidationMoney(int money) {

  if (check_itNumber(String.valueOf(money))) {
   if (0 < money && this.getMoney() > money) {
    return true;
   }
  }
  System.out.println("출금오류");
  return false;
 }

 public void process() {
  int count = 0;

  System.out.println("Input Card Number~?");

  this.setCardNum(this.getInput());

  while (!ValidationCard(this.getCardNum())) {
   if (count == 2) {
    System.exit(0);
   }
   System.out.println("카드번호 재입력!!");
   this.setCardNum(this.getInput());
   count++;
  }

  System.out.println("Input Card Password~?");

  count = 0;
  while (!this.getPasswd().equals(this.getInput())) {
   if (count == 2) {
    System.exit(0);
   }
   System.out.println("비번 재입력!!");
   count++;
  }
  do {
   System.out.println("얼마를 인출 하시겠습니까?");
   try {
    this.setOutMoney(Integer.parseInt(this.getInput()));
   } catch (NumberFormatException e) {

   }
  } while (!ValidationMoney(this.getOutMoney()));

  this.setMoney(this.getMoney() - this.getOutMoney());
  System.out.println(this.getOutMoney() + "원을 인출 하셨습니다.");
  System.out.println(this.getMoney() + "원이 은행에 남았습니다..");
 }

 public static void main(String[] args) {
  SUT sut = new SUT();
  sut.process();
 }
}






-------------------- Test Case - At JUnit ---------------


import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TC01 {
 @Test
 public void TC_charToNum1() {
  SUT sut = new SUT();
  int ret = sut.charToNum('3');
  System.out.println(ret);
  assertEquals("3", 3, ret);
 }

 @Test
 public void TC_charToNum2() {
  SUT sut = new SUT();
  int ret = sut.charToNum('a');
  System.out.println(ret);
  assertEquals("a", 49, ret);
 }

 @Test
 public void TC_carry1() {
  SUT sut = new SUT();
  int ret = sut.carry(12);
  System.out.println(ret);
  assertEquals("12", 3, ret);
 }

 @Test
 public void TC_carry2() {
  SUT sut = new SUT();
  int ret = sut.carry(-7);
  System.out.println(ret);
  assertEquals("-7", -7, ret);
 }

 @Test
 public void TC_cardrun1() {
  SUT sut = new SUT();
  boolean ret = sut.cardrun("2121");
  System.out.println(ret);
  assertEquals("2121", true, ret);
 }

 @Test
 public void TC_cardrun2() {
  SUT sut = new SUT();
  boolean ret = sut.cardrun("1212");
  System.out.println(ret);
  assertEquals("1212", false, ret);
 }

 @Test
 public void TC_cardrun3() {
  SUT sut = new SUT();
  boolean ret = sut.cardrun("446667651");
  System.out.println(ret);
  assertEquals("1212", true, ret);
 }

 @Test
 public void TC_cardrun4() {
  SUT sut = new SUT();
  boolean ret = sut.cardrun("44666765126");
  System.out.println(ret);
  assertEquals("44666765126", true, ret);
 }

 @Test
 public void TC_check_itNumber1() {
  SUT sut = new SUT();
  boolean ret = sut.check_itNumber("44666765126");
  System.out.println(ret);
  assertEquals("44666765126", true, ret);
 }

 @Test
 public void TC_check_itNumber2() {
  SUT sut = new SUT();
  boolean ret = sut.check_itNumber("4ad");
  System.out.println(ret);
  assertEquals("4ad", false, ret);
 }

 @Test
 public void TC_check_itNumber3() {
  SUT sut = new SUT();
  boolean ret = sut.check_itNumber("125");
  System.out.println(ret);
  assertEquals("125", true, ret);
 }

 @Test
 public void TC_LenthValidation1() {
  SUT sut = new SUT();
  boolean ret = sut.LenthValidation("446667651262626262626");
  System.out.println(ret);
  assertEquals("44666765126", false, ret);
 }

 @Test
 public void TC_LenthValidation2() {
  SUT sut = new SUT();
  boolean ret = sut.LenthValidation("446667651262");
  System.out.println(ret);
  assertEquals("44666765126", true, ret);
 }

 @Test
 public void TC_LenthValidation3() {
  SUT sut = new SUT();
  boolean ret = sut.LenthValidation("444");
  System.out.println(ret);
  assertEquals("444", false, ret);
 }

 @Test
 public void TC_LenthValidation4() {
  SUT sut = new SUT();
  boolean ret = sut.LenthValidation("2121");
  System.out.println(ret);
  assertEquals("2121", true, ret);
 }

 @Test
 public void TC_ValidationCard1() {
  SUT sut = new SUT();
  boolean ret = sut.LenthValidation("2121");
  System.out.println(ret);
  assertEquals("2121", true, ret);
 }

 @Test
 public void TC_ValidationCard2() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationCard("fkje");
  System.out.println(ret);
  assertEquals("fkje", false, ret);
 }

 @Test
 public void TC_ValidationCard3() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationCard("34 34");
  System.out.println(ret);
  assertEquals("34 34", false, ret);
 }

 @Test
 public void TC_ValidationMoney1() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationMoney(100000);
  System.out.println(ret);
  assertEquals("100000", true, ret);
 }

 @Test
 public void TC_ValidationMoney2() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationMoney(0);
  System.out.println(ret);
  assertEquals("0", false, ret);
 }

 @Test
 public void TC_ValidationMoney3() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationMoney(-1);
  System.out.println(ret);
  assertEquals("-1", false, ret);
 }

 @Test
 public void TC_ValidationMoney4() {
  SUT sut = new SUT();
  boolean ret = sut.ValidationMoney(1000001);
  System.out.println(ret);
  assertEquals("1000001", false, ret);
 }
 


}

728x90