diff --git a/knoda/src/B077_TicketOffice.java b/knoda/src/B077_TicketOffice.java new file mode 100644 index 0000000000000000000000000000000000000000..4b459be1040edaa86b7b8d27e56bcd582da9dac0 --- /dev/null +++ b/knoda/src/B077_TicketOffice.java @@ -0,0 +1,96 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B077_TicketOffice { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // チケットカウンターの数(counterNum)と並んでいる人数(customerNum)を入力する + final int counterNum = scan.nextInt(); + final int customerNum = scan.nextInt(); + + // counterTimeList : それぞれのチケットカウンターで購入するのにかかる時間を格納したリスト + final List counterTimeList = new ArrayList(); + // それぞれのチケットカウンターでの購入時間を入力する + for (int counter = 0; counter < counterNum; counter++) { + counterTimeList.add(scan.nextInt()); + } + + TicketOffice ticketOffice = new TicketOffice(counterTimeList, customerNum); + System.out.println(ticketOffice.getTime()); + scan.close(); + } + +} + +// チケット売り場クラス +class TicketOffice { + private final List counterTimeList; + // currentCounterTimeList : それぞれのチケットカウンターでの、お客様の残り購入時間を格納したリスト + private List currentCounterTimeList = new ArrayList(); + private final int customerNum; + + public TicketOffice(final List counterTimeList, final int customerNum) { + this.counterTimeList = counterTimeList; + this.customerNum = customerNum; + } + + // 経過時間を取得する + public int getTime() { + setUp(); + int time = 0; // time : 経過時間 + int counter; + + for (int customer = 0; customer < customerNum; customer++) { + // カウンターが空いていないとき + while ((counter = getEmptyCounter()) == -1) { + updateCurrentCounterTime(); + time++; + } + currentCounterTimeList.set(counter, counterTimeList.get(counter)); + } + + int maxCounterTime = 0; + for (int c = 0; c < currentCounterTimeList.size(); c++) { + if (maxCounterTime < currentCounterTimeList.get(c)) { + maxCounterTime = currentCounterTimeList.get(c); + } + } + time += maxCounterTime; + return time; + } + + // それぞれのチケットカウンターでの、お客様の残り購入時間を0に初期化 + private void setUp() { + for (int counter = 0; counter < counterTimeList.size(); counter++) { + currentCounterTimeList.add(0); + } + } + + // どのカウンターが空いているかを取得する + private int getEmptyCounter() { + int emptyCounter = -1; + int minCounterTime = 1000; + for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { + if (currentCounterTimeList.get(counter) == 0) { + if (counterTimeList.get(counter) < minCounterTime) { + minCounterTime = counterTimeList.get(counter); + emptyCounter = counter; + } + } + } + return emptyCounter; + } + + // それぞれのカウンターの残り時間をカウントダウンをする + private void updateCurrentCounterTime() { + for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { + currentCounterTimeList.set(counter, currentCounterTimeList.get(counter) - 1); + } + } + +} diff --git a/knoda/test/B077_TicketOfficeTest.java b/knoda/test/B077_TicketOfficeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..fe5f91f76056b6757e2c6e791a20606ea26af55c --- /dev/null +++ b/knoda/test/B077_TicketOfficeTest.java @@ -0,0 +1,58 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class B077_TicketOfficeTest { + List counterTimeList; + int customerNum; + TicketOffice sut; + + @Test + public void チケットカウンターでの購入時間が2_4_5のとき全体で8かかる() { + counterTimeList = new ArrayList(); + int customerNum = 6; + counterTimeList.add(2); + counterTimeList.add(4); + counterTimeList.add(5); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 8; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } + + @Test + public void チケットカウンターでの購入時間が1_1_1_1_100のとき全体で100かかる() { + counterTimeList = new ArrayList(); + int customerNum = 10; + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(100); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 100; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } + + @Test + public void チケットカウンターでの購入時間が2_1のとき全体で3かかる() { + counterTimeList = new ArrayList(); + int customerNum = 4; + counterTimeList.add(2); + counterTimeList.add(1); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 3; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } +}