카테고리 없음

[자바] Java 배열이 작동하지 않음

행복을전해요 2021. 2. 3. 21:06

추상화를 재고해야한다고 생각합니다. 이것은 더 많은 OO 접근법을 사용하기위한 빠른 개편이었습니다. 고객과 방을 분리하여이 아이디어를 확장 할 수 있습니다. 객실과 고객의 모음 인 호텔 객체가 있어야합니다.

import java.util.LinkedList;

public class Hotel{
LinkedList<Customer> customers;

public void addCustomer(Customer c){
    customers.add(c);
    }
    public void removeCustomer(int roomNumber){
        boolean flag = true;
            for(int i = 0; i < customers.size() && flag; ++i){
                    if(customers.get(i).getRoomNumber() == roomNumber)
                                customers.remove(i);
                                            flag = false;
                                                }
                                                }
                                                
                                                public String toString(){
                                                    String s = "";
                                                        for(Customer c : customers)
                                                                s += c.toString() + "\n";
                                                                    return s;
                                                                    }
                                                                    }
                                                                    
                                                                    class Customer {
                                                                    
                                                                    private String roomType;
                                                                    private String address;
                                                                    private String name;
                                                                    private String phone;
                                                                    private String price;
                                                                    private int roomNumber;
                                                                    
                                                                    public Customer(String rmTp, String addrss, String nm, String phn, String prc, int rmNm){
                                                                        roomType   = rmTp;
                                                                            address    = addrss;
                                                                                name       = nm;
                                                                                    phone      = phn;
                                                                                        price      = prc;
                                                                                            roomNumber = rmNm;
                                                                                            }
                                                                                            
                                                                                            public int getRoomNumber(){ return roomNumber; }
                                                                                            public String toString(){
                                                                                                String receipt = "";
                                                                                                    receipt += "Customer Name: " + name ;
                                                                                                        receipt += "Address: " + address ;
                                                                                                            receipt += "Phone number: " + phone ;
                                                                                                                receipt += "Thanks for making your room reservation for Geek Speak with the Orozco Hotel!" ;
                                                                                                                    receipt += "We have you booked in room number " + roomNumber + ", which is a " + roomType + "." ;
                                                                                                                        receipt += "Your charges for the convention will be $" + price + "." ;
                                                                                                                            receipt += "We hope you enjoy your stay with us and the convention.";
                                                                                                                                receipt += "The Orozco Hotel Staff";
                                                                                                                                    return receipt;
                                                                                                                                    }
                                                                                                                                    
                                                                                                                                    }
                                                                                                                                    


출처
https://stackoverflow.com/questions/22050013