2013-03-05 2 views
0

나는 자바 자신을 가르치 려하고 BlueJ 예제를 살펴보기 시작했다.간단한 자바 배열 초보자

수술 시스템의 경우 환자와 환자의 주소를 배열에 추가 한 다음 환자를 추가 한 다음 환자를 나열 할 수 있어야합니다.

이 예제에서는 벡터에 이름을 추가하는 하드 코드를 사용하지만 배열을 사용하고 사용자가 배열에 이름과 주소를 추가 할 수있게하려고합니다.

누구에게 가이드를 제공 할 수 있습니까?

나는 다음과 같은 사항이 있지만 여기에서 어디로 가야할지 잘 모릅니다.

public class Patient 
{ 
    public String name; 
    public String address; 

    public Patient(String n, String a) { 
     name = n; 
     address = a; 
    } 


} 
+1

아마도 [Java 자습서] (http://docs.oracle.com/javase/tutorial/java/TOC.html)로 시작해야합니다. 처음부터 시작하는 것이 좋지만, [배열] (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) 섹션도 있습니다. –

+1

여기에 '벡터'를 사용하는 것이 옳은 일일 것입니다. Java에서 (많은 언어에서와 같이) 배열 차원이 고정되어 있기 때문에 더 많은 요소를 추가 할 수 있도록 배열을 _grow_하도록 장려합니다. 'Vector' (그리고'List's와'Set's 같은 다른 자바 표준 콜렉션 클래스)가 더 효과적입니다. – RudolphEst

+1

아마도 'Vector' 클래스를 사용하는 예제가 있다면 그것들은 거의 구식 일 것입니다 - ArrayList는 수년 동안 그것을 대체했습니다. – berry120

답변

1

이것은 숙제와 매우 흡사 하듯이 정확한 답을 제공하지는 않지만 여기에 arr을 사용하는 호텔 ays. 그것을 통해 가서 그것을 이해하려고 노력하십시오. 그렇게하면 환자 문제를 스스로 해결할 수 있습니다.

import java.util.*; 

class Customer 
{ 
    private String name; 
    private int room; 

    public void setName(String name) 
    { 
     this.name=name; 
    } 

    public String getName() 
    { 
     return this.name; 
    } 

    public void setRoom(int room) 
    { 
     this.room=room; 
    } 

    public int getRoom() 
    { 
     return this.room; 
    } 
} 

class Hotel 
{ 
    public static void initialize(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      RoomList[i]=new Customer(); 
      RoomList[i].setName("EMPTY"); 
      RoomList[i].setRoom(i+1); 
     } 
    } 

    public static void viewList(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println("Room number "+RoomList[i].getRoom()+" is vacant."); 
      else 
       System.out.println("Room number "+RoomList[i].getRoom()+" is ocupied by "+RoomList[i].getName()+"."); 
     } 
     System.out.println(); 
    } 

    public static boolean addCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals("EMPTY")) 
      { 
       RoomList[i].setName(name); 
       return true; 
      } 
     return false; 
    } 

    public static void showEmptyRooms(Customer RoomList[]) 
    { 
     System.out.println("Available rooms are:"); 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println(RoomList[i].getRoom()); 
     System.out.println(); 
    } 

    public static boolean deleteCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
      { 
       RoomList[i].setName("EMPTY"); 
       System.out.println("Deletion successful.\n"); 
       return true; 
      } 
     return false; 
    } 

    public static int getIndex(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
       return i; 
     return -1; 
    } 

    public static void main(String[] args) 
    { 
     int numOfCustomers=10; 
     Customer[] RoomList = new Customer[numOfCustomers]; 
     String name; 
     initialize(RoomList); 
     Scanner input = new Scanner(System.in); 
     int option=0; 

     do 
     { 
      System.out.println("  Hotel Booking Options"); 
      System.out.println("====================================="); 
      System.out.println("1: To View all rooms"); 
      System.out.println("2: To Add customer to a room"); 
      System.out.println("3: To Display empty rooms"); 
      System.out.println("4: To Delete customer from a room"); 
      System.out.println("5: Find room from customer name"); 
      System.out.println("0: Exit"); 

      System.out.print("\nEnter your choice: "); 
      option = input.nextInt(); 
      System.out.println(); 

      switch(option) 
      { 
       case 1: 
       { 
        viewList(RoomList); 
        break; 
       } 
       case 2: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        if(!addCustomer(RoomList, name)) 
         System.out.println("No rooms available!"); 
        break; 
       } 
       case 3: 
       { 
        showEmptyRooms(RoomList); 
        break; 
       } 
       case 4: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        deleteCustomer(RoomList, name); 
        break; 
       } 
       case 5: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        System.out.println("Customer's room: "+RoomList[getIndex(RoomList, name)].getRoom()+"\n"); 
        break; 
       } 
       case 0: 
       { 
        System.out.println("\nThank you!\n"); 
        break; 
       } 
       default: 
       { 
        System.out.println("Invalid option!\n"); 
        break; 
       } 
      } 


     }while(option!=0); 
    } 
} 
+0

고마워요. Roney, 숙제가 아니에요, 때로는 책이 무엇을 얻고 있는지 파악하기가 때로는 어렵습니다. 내가 더 고민하는 것은 배열에 2 개의 다른 필드를 추가 한 다음 필드 중 하나에서 값을 반환하는 것입니다. 나는 당신의 모범을 연구하고 내가 시도하는 것에 그것의 일부를 적용하려고 노력할 것이다. 대단히 감사합니다. – user1295053

+0

@ user1295053 : 저의 실수. :) 어쨌든,이 코드는 이러한 의구심을 해소해야한다고 생각합니다. –

5

정보의이 종류는 아주 잘 here를 설명되어 있습니다. 이 유형의 배열을 만들려면처럼 경우

, 그것은 보인다 Patient

환자 10 명을 수용 할 수있는 배열을 생성하는 구문은 다음과 같습니다

Patient[] patients = new Patients[10]; 

그리고 당신은 설명서를 참조 후 , 당신은 원하는 기능을 얻기 위해 제공 한 구문을 사용할 수 있습니다 :)

+0

도움 주셔서 감사합니다. 크리스 – user1295053

+0

내 기쁨이었습니다. :) – christopher