정모/2011.4.4/CodeRace/강소현 (rev. 1.2)
Main.java ¶
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Town a = new Town("A");
Town b = new Town("B");
Person layton = new Person();
layton.setName("Layton");
Person luke = new Person();
luke.setName("Luke");
Person badUncle = new Person();
badUncle.setName("Bad");
a.addPerson(layton);
a.addPerson(luke);
a.addPerson(badUncle);
while(b.getPeopleIndex() < 3){
if(layton.getPosition() != luke.getPosition() &&
luke.getPosition() == badUncle.getPosition()){
System.out.println("Luke가 강에 빠졌습니다ㅠㅠ");
System.exit(1);
}
a.showPeople();
b.showPeople();
System.out.print("강 건널 사람 몇 명(0~2)? ");
int num = input.nextInt();
int [] name = new int[num];
Person ship = new Person();
for(int i=0; i<num; i++){
System.out.print("건널 사람 이름?(Layton: 1, Luke: 2, Bad: 3)->");
name[i] = input.nextInt();
}
if(mustCheck(num,name)){
for(int i=0; i<num; i++){
switch(name[i]){
case 1:
ship = layton;
break;
case 2:
ship = luke;
break;
case 3:
ship = badUncle;
break;
}
if(a.getTownName().equals(ship.getPosition().getTownName())){
a.crossRiver(b, ship);
}else{
b.crossRiver(a, ship);
}
}
}
}
a.showPeople();
b.showPeople();
}
public static boolean mustCheck(int num, int [] name){
if(num > 2){
System.out.println("[Error] 배는 2인승이야!");
return false;
}
if(num == 1 && name[0]==2){//Luke 혼자 가면
System.out.println("[Error] Luke 혼자 못탐ㅋ");
return false;
}
return true;
}
}
Person.java ¶
public class Person {
private String name;
private Town position;
public void setName(String name){
this.name = name;
}
public void setPosition(Town position){
this.position = position;
}
public String getName(){
return name;
}
public Town getPosition(){
return position;
}
}
Town.java ¶
public class Town {
private String townName;
private Person [] people;
private int maxPeople = 3;
private int peopleIndex = 0;
public Town(){
people = new Person[maxPeople];
}
public Town(String townName){
this.townName = townName;
people = new Person[maxPeople];
}
public void setTownName(String townName){
this.townName = townName;
}
public String getTownName(){
return townName;
}
public int getPeopleIndex(){
return peopleIndex;
}
public void addPerson(Person p){
p.setPosition(this);
people[peopleIndex++] = p;
}
public void crossRiver(Town destination, Person ship){
for(int i=0; i<peopleIndex;i++){
if(ship.getName().equals(people[i].getName())){
people[i] = people[peopleIndex-1];
destination.addPerson(ship);
peopleIndex--;
}
}
}
public void showPeople(){
for(int i=0; i<peopleIndex; i++){
System.out.print("town : " + people[i].getPosition().getTownName());
System.out.println(", name : " + people[i].getName());
}
}
}
느낀 점 ¶
- 음...저는 메인 함수를 사랑하나봅니다/ㅁ/ - 강소현