【问题标题】:Undeclared method:contains(java.lang.String)未声明的方法:包含(java.lang.String)
【发布时间】:2022-11-24 01:53:19
【问题描述】:

我正在解决一个问题,但我非常困惑。请帮忙!!

问题:

getMatchingRooms(String) 接收房间类型作为参数,并返回酒店中类型与参数匹配且未被占用的房间的 ArrayList。例如,调用带有参数“Single”的方法将返回所有类型为single 的空闲房间的列表。 提示:使用局部变量创建一个新的空房间列表,将任何匹配的空闲房间添加到该列表,然后在完成后返回列表。

到目前为止,这是我的代码:

public class Room
{

    // 1b. Add private fields to the Room class

    // guest, the person who booked the room
    private String guest;
    // number, an identifier for the room
    private String number;
    // dailyRate, a double value giving the standard daily rate for the room
    private double dailyRate;

    // 1c. Add constructor
    public Room(String aGuest, String aNumber, double aRate)
    {
        this.guest = aGuest;
        this.number = aNumber;
        this.dailyRate = aRate;                                        
    }

    // 1di. Add public getter methods
    public String getGuest() {
        return guest;
    }

    public String getNumber() {
        return number;
    }

    public double getDailyRate() {
        return dailyRate;
    }

    // 1dii. Add setter method setGuest
    public void setGuest(String newGuest) {
        this.guest = newGuest;
    }

    // 1diii. Add setter methid setDailyRate
    public void setDailyRate(double newDailyRate) {
        this.dailyRate = newDailyRate;
    }

    // 1e. Add method to tell if room is available
    public boolean isAvailable(){
        if (guest.isEmpty()){
            return true;
        }  
        else {
            return false;
        }
    }

    // 1f. Add method to tell if room has a valid number
    public boolean verifyRoom() {

        // Check if string is 3 characters long
        if (number.length() == 3){
            // check if first two characters represent a room number between 01 to 99
            char a = number.charAt(0);
            char b = number.charAt(1);
            char c = number.charAt(2);
            if ((a + b > 96) && (a + b < 115) && (a > 47) && (b > 47) && (a < 58) && (b < 58)) {
             // Check if number finishes with A,B or C
                if (c == 65 || c == 66 || c == 67) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    // 1g. Add method to return string of room type
    public String getType() {
        char a = number.charAt(2);
        if (a == 65) {
            return "Single";
        }
        else if (a == 66) {
            return "Double";
        }
        else {
            return "Family";
        }
    }

    // 1h. A method that describes the room
    public String description() {
        String available = "";
        String guest = "";
        if (isAvailable() == true) {
            available = "(available)";
            guest = "";
        }
        else {
            available = "(reserved)";
            guest = getGuest();
        }

        return getType() + " room " + getNumber() + " " + available + " " + guest;

    }
}

// import the ArrayList class
import java.util.ArrayList; 

public class Hotel
{
    //2b. Add two private fields
    private ArrayList<Room> rooms;
    private String name;
    
    //2c. 
    public Hotel(String aName) {
        this.name = aName;
        ArrayList<Room> rooms = new ArrayList<>();
    }
    
        /**
     * (d) Adds some unoccupied test rooms to the hotel
     */
    public void addRooms()
    {
        int i = 10;
        while (i < 19) {
            String number = "" + i;
            double rate;

            if(i % 3 == 1) {
                number += "A";
                rate = 100;
            }
            else if (i % 3 == 2) {
                number += "B";
                rate = 180;
            }
            else
            {
                number += "C";
                rate = 250;
            }

            Room r = new Room("", number, rate);

            rooms.add(r);
            i++; 
        }
    }
    
    public double calculateBill(Room rate, int days){
        
        if (days > 3) {
            return days * 0.9 * rate.getDailyRate();
        }
        else {
            return days * rate.getDailyRate();
        }
        
    }
    
    public ArrayList<Room> getMatchingRooms(String roomType) {
        // create new empty array
        ArrayList<Room> matchingRooms = new ArrayList<>();

        // loop which adds any empty rooms with correct room type to array
        for (int i = 0; i < rooms.size(); i++) {
            String a = String.valueOf(rooms.get(i));
                if (rooms.get(i).contains(roomType)) {
                    matchingRooms.add(b);
                }
 
        }

              
        return matchingRooms;
        
    }
    
    
}

【问题讨论】:

  • rooms.get(i) 返回 RoomroomTypeStringRoom 中没有contains 方法。可能您想使用rooms.get(i).getType().contains(roomType)

标签: java arrays loops arraylist


【解决方案1】:

这种比较应该工作正常 rooms.get(i).getType().equals(roomType)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2018-03-28
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多