diff --git a/sitou/src/B118.java b/sitou/src/B118.java new file mode 100644 index 0000000000000000000000000000000000000000..a1795def98478b0ee5acd45b90e0403691a5ef34 --- /dev/null +++ b/sitou/src/B118.java @@ -0,0 +1,50 @@ +package src; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B118 { + private static int studentsNum; + private static final List studentsList = new ArrayList<>(); + private PrintStream out; + + public B118(InputStream in, PrintStream out) { + final Scanner scan = new Scanner(in); + + try { + studentsNum = scan.nextInt(); + for (int n = 0; n < studentsNum; n++) { + final String name = scan.next(); + final int height = scan.nextInt(); + final int birthMonth = scan.nextInt(); + final Student student = new Student(name, height, birthMonth); + studentsList.add(student); + } + + } finally { + scan.close(); + } + this.out = out; + } + + public static void main(String[] args){ + final B118 b118 = new B118(System.in, System.out); + final List result = b118.execute(); + b118.output(result); + } + + public List execute(){ + Classroom classroom = new Classroom(studentsList); + return classroom.sortStudents(); + } + + public void output(final List result) { + for (int i = 0; i < result.size(); i++) { + out.println(result.get(i)); + } + } + +} diff --git a/sitou/src/Classroom.java b/sitou/src/Classroom.java new file mode 100644 index 0000000000000000000000000000000000000000..3d0336864c98e825af1df9103edd662597c32d0d --- /dev/null +++ b/sitou/src/Classroom.java @@ -0,0 +1,72 @@ +package src; + +import java.util.ArrayList; +import java.util.List; + +public class Classroom { + private List studentsList; + + public Classroom(final List studentsList) { + this.studentsList = studentsList; + } + + /** + * 生徒を背の順にしてデータを格納する. + * + * @return 格納したデータ + */ + public List sortStudents() { + final List result = new ArrayList<>(); + + // findfrontStudentで見つけた生徒はstudentsListから削除していく + while (studentsList.size() > 0) { + result.add(findFrontStudent()); + } + + return result; + } + + /** + * 残っている生徒の中で、背の順の最も小さい生徒を探す. + * + * @return 最小の生徒の名前 + */ + private String findFrontStudent() { + // 与えられた条件で最後尾になる生徒情報を初期値にする。 + Student frontStudent = new Student("zzzzzzzzzz", 200, 1); + + int studentNum = 0; + for (int i = 0; i < studentsList.size(); i++) { + final Student studentToCheck = studentsList.get(i); + if (isFront(frontStudent, studentToCheck)) { + frontStudent = studentToCheck; + studentNum = i; + } + } + + studentsList.remove(studentNum); + + return frontStudent.getName(); + } + + /** + * 問題で与えられた条件で生徒を比較する. 条件 ・背の低い順番に前から並ばせる ・背の高さが同じ場合、誕生月の数字が大きい方を前にする + * ・背の高さが同じでかつ誕生月が同じ場合、名前を辞書順で並べた時、早い方の人を前にする + * + * @param frontStudent 現在最小と思われる生徒 + * @param studentToCheck 比較対象の生徒 + * @return true/false + */ + private boolean isFront(final Student frontStudent, final Student studentToCheck) { + if (frontStudent.getHeight() > studentToCheck.getHeight()) { + return true; + } else if (frontStudent.getHeight() == studentToCheck.getHeight()) { + if (frontStudent.getBirthMonth() < studentToCheck.getBirthMonth()) { + return true; + } else if (frontStudent.getBirthMonth() == studentToCheck.getBirthMonth()) { + return frontStudent.getName().compareTo(studentToCheck.getName()) > 0; + } + } + return false; + } +} diff --git a/sitou/src/Student.java b/sitou/src/Student.java new file mode 100644 index 0000000000000000000000000000000000000000..2a3a09d3809c5d6837438a34156135ad23e207f1 --- /dev/null +++ b/sitou/src/Student.java @@ -0,0 +1,31 @@ +package src; + +public class Student implements Cloneable { + private final String name; + private final int height; + private final int birthMonth; + + public Student(final String name, final int height, final int birthMonth) { + this.name = name; + this.height = height; + this.birthMonth = birthMonth; + } + + public String getName() { + return name; + } + + public int getHeight() { + return height; + } + + public int getBirthMonth() { + return birthMonth; + } + + @Override + public Student clone() throws CloneNotSupportedException { + Student clone = (Student) super.clone(); + return clone; + } +} diff --git a/sitou/test/B118_Test.java b/sitou/test/B118_Test.java new file mode 100644 index 0000000000000000000000000000000000000000..44cbeb33b3bbd0f7f2bf480186afeb1578aff131 --- /dev/null +++ b/sitou/test/B118_Test.java @@ -0,0 +1,87 @@ +package test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import src.B118; +import src.Classroom; +import src.Student; + +public class B118_Test { + + @Test + public void 正しく動くかテスト() throws CloneNotSupportedException { + final InputStream is = new ByteArrayInputStream(("5\n" + + "aria 145 12\n" + + "bim 132 8\n" + + "cindy 178 4\n" + + "deen 145 3\n" + + "emma 145 3").getBytes()); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final PrintStream ps = new PrintStream(baos); + + final B118 b118 = new B118(is, ps); + final List result =b118.execute(); + b118.output(result); + + final String actual = baos.toString(); + assertEquals("bim" + + System.lineSeparator() + + "aria" + + System.lineSeparator() + + "deen" + + System.lineSeparator() + + "emma" + + System.lineSeparator() + + "cindy" + + System.lineSeparator(), actual); + } + + @Test + public void 背の順で並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 130, 4)); + + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); + + assertThat(result, is(contains("cindy", "bim", "aria"))); + } + + @Test + public void 同じ身長がいるとき条件通り並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 140, 4)); + + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); + + assertThat(result, is(contains("bim", "cindy", "aria"))); + } + + @Test + public void 同じ身長で誕生月も同じとき条件通り並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 140, 8)); + studentsList.add(new Student("deen", 140, 8)); + + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); + + assertThat(result, is(contains("cindy", "deen", "bim", "aria"))); + } + +}