4月14日

テストファーストの練習をもう一度

TDDブートキャンプ大阪の課題にチャレンジしてみよう!

TDDBC大阪

package tdd;

import static org.junit.Assert.*;

import org.junit.Test;

public class VendingMachineTest {

	@Test
	public void testInsert() {
		VendingMachine vm = new VendingMachine();
		int change = vm.insert(10);
		assertEquals(0, change);
		int amount = vm.getAmount();
		assertEquals(10, amount);
		vm.insert(10);
		amount = vm.getAmount();
		assertEquals(20, amount);
		change = vm.refund();
		assertEquals(20, change);
		amount = vm.getAmount();
		assertEquals(0, amount);
	}

	@Test
	public void testRefund() {
		VendingMachine vm = new VendingMachine();
		vm.insert(100);
		int amount = vm.getAmount();
		assertEquals(100, amount);
		int change = vm.refund();
		assertEquals(100, change);
	}

	@Test
	public void testInsertX() {
		VendingMachine vm = new VendingMachine();
		int change = vm.insert(1);
		assertEquals(1, change);
		change = vm.insert(5);
		assertEquals(5, change);
	}
}
package tdd;

public class VendingMachine {
	private int amount = 0;

	public int insert(int money) {
		if (!isAcceptable(money)) return money;
		amount += money;
		return 0;
	}

	private boolean isAcceptable(int money) {
		if (money == 10) return true;
		if (money == 50) return true;
		if (money == 100) return true;
		if (money == 500) return true;
		if (money == 1000) return true;
		return false;
	}

	public int getAmount() {
		return amount;
	}

	public int refund() {
		int a = amount;
		amount = 0;
		return a;
	}
}