Question

Can someone tell me how to create a test for the following Hotel classes that test...

Can someone tell me how to create a test for the following Hotel classes that test all of the methods?

public class Bed



{

  private String type;

  private static final String[] TYPES ={"Single", "Double", "King Size"};



  public Bed(String type)

  {

    this.type = type;

  }



  public boolean isSingle()

  {

    return type.equals(TYPES[0]);

  }



  public boolean isDouble()

  {

    return type.equals(TYPES[1]);

  }



  public boolean isKingSize()

  {

    return type.equals(TYPES[2]);

  }



}

public class Guest

{
private String name;

public Guest (String name)
{
    this.name = name;
}

public String getName()
{
    return name;
}

//checks if instance is an object of guest
public boolean equals(Object obj)
{
    if (!(obj instanceof Guest))
      return false;
    Guest other = (Guest) obj;
    return this.name.equals(other.name); //checks the instance of the object not the actual object which is the guest//
}
}

public class Room

{

  private int number;

  private Bed bed;

  private Guest guest;

  public static final double[] PRICES = {59.50, 72.40, 89.00};

  public static final String[] BED_TYPES = {"Single", "Double", "King Size"};

  public Room (int number, String bedType)

  {

    this.number = number;

    this.bed = new Bed(bedType);

    this.guest = null;

  }



  public int getNumber()

  {

    return number;

  }

  public int getFloor()

  {

    int floor = number/100;

    return floor;



  }

public double getPrice()

{

  if (bed.isSingle())

  {

    return PRICES[0];

  }

 else if (bed.isDouble())

  {

    return PRICES[1];

  }

  else {

    return PRICES[2];

  }



}

public boolean isOccupied()

{

  return  guest != null;

}

public void registerGuest(Guest guest)

{

  this.guest = guest;

}



public void vacate()

  {

    guest = null;

  }



public String getBedType()

{

  if (bed.isSingle())

  {

    return BED_TYPES[0];

  }

  else if (bed.isDouble())

  {

    return BED_TYPES[1];

  }

  else

  {

    return BED_TYPES[2];

  }

}

 public Guest getGuest()

 {

   return guest;

 }

}
 
import java.util.ArrayList;



public class Hotel

{

  private String name;

  private Room[] rooms;



  private Hotel(String name, Room[] rooms)

  {

    this.name = name;

    this.rooms = rooms;

  }



  public String getName()

  {

    return name;

  }



  public int getNumberOfRooms()

  {

    return rooms.length;

  }



  public int getNumberOfAvailableRooms()

  {

    int count = 0;

    for (int i=0; i<rooms.length;i++)

    {

      if (!rooms[i].isOccupied())

      {

        count++;

      }

    }

    return count;

  }



  public Room getFirstAvailableRoom()

  {

    for (int i=0; i<rooms.length;i++)

    {

      if (!rooms[i].isOccupied())

      {

        return rooms[i];

      }

    }

    return null;

  }



  public Room getFirstAvailableRoom(double maxPrice)

  {

    for (int i=0; i<rooms.length;i++)

    {

      if (!rooms[i].isOccupied() && rooms[i].getPrice() <=maxPrice)

      {

        return rooms[i];

      }

    }

    return null;

  }



  public Room[] getAllAvailableRooms(String bedType)

  {

    ArrayList<Room> list = new ArrayList<>();

    for (int i = 0; i < rooms.length; i++)

    {

      if (!rooms[i].isOccupied() && rooms[i].getBedType().equals(bedType))

      {

      list.add(rooms[i]);

      }

    }

    return (Room[])list.toArray();

  }



  public boolean hasGuest(Guest guest)

  {



    for (int i = 0; i < rooms.length; i++)

    {

      if (rooms[i].isOccupied() && rooms[i].getGuest().equals(guest))

      {

       return true;

      }

    }

    return false;

  }



  public Room getRoom(Guest guest)

  {



    for (int i = 0; i < rooms.length; i++)

    {

      if (rooms[i].isOccupied() && rooms[i].getGuest().equals(guest))

      {

        return rooms[i];

      }

    }

    return null;

  }

}
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here in this given set of classes, we can't instantiate an object for class Hotel in order to test all the hotel related methods, it is because the Hotel class is having a private constructor. If we are allowed to make a change the constructor to public, then we can test the given Hotel class like the below given code.

import java.util.ArrayList;


class Main {

    public static void main(String[] args) {

        //creating guests objects
        Guest guest1 = new Guest("Guest One");  
        Guest guest2 = new Guest("Guest Two");
        Guest guest3 = new Guest("Guest Three");
        Guest guest4 = new Guest("Guest Four");


        //creating objects for Room for the Hotel 
        Room room1 = new Room(1,Room.BED_TYPES[0]);
        Room room2 = new Room(2,Room.BED_TYPES[1]);
        Room room3 = new Room(3,Room.BED_TYPES[2]);
        Room room4 = new Room(4,Room.BED_TYPES[2]);

        
        //register the created Guests in the created Rooms
        room1.registerGuest(guest2);
        room2.registerGuest(guest1);
        room3.registerGuest(guest3);
        
        //creating Room's array
        Room[] rooms = new Room[]{room1,room2,room3,room4};

        //Creating an Object for Hotel class with the above Rooms array
        Hotel hotel = new Hotel("Hotel Oberoi",rooms);

        //Calling all the methods of Hotel class
        System.out.println("\nHotel Name" + hotel.getName());

        System.out.println("\nTotal No:Of Rooms: "+hotel.getNumberOfRooms());

        System.out.println("\nNo:Of Available Rooms :" + hotel.getNumberOfAvailableRooms());

        System.out.println("\nDoes Hotel has Guest One?  " + hotel.hasGuest(guest1));

        System.out.println("\nIn which room Guest One staying? " + hotel.getRoom(guest1));

        Room[] availableRooms = hotel.getAllAvailableRooms(Room.BED_TYPES[2]);

        System.out.println("\nAvailable Rooms:");
        if(availableRooms.length < 1)
            System.out.println("No Rooms!");
        else
            for(Room room: availableRooms)
                System.out.println(room);


        System.out.println("\nFirst Available Rooms: " + hotel.getFirstAvailableRoom());

    }
}

class Bed {

    private static final String[] TYPES = {"Single", "Double", "King Size"};
    private String type;

    public Bed(String type) {
        this.type = type;
    }

    public boolean isSingle() {
        return type.equals(TYPES[0]);
    }

    public boolean isDouble() {
        return type.equals(TYPES[1]);
    }

    public boolean isKingSize() {
        return type.equals(TYPES[2]);
    }
}

class Guest {
    private String name;

    public Guest(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    //checks if instance is an object of guest
    public boolean equals(Object obj) {
        if (!(obj instanceof Guest))
            return false;
        Guest other = (Guest) obj;
        return this.name.equals(other.name); //checks the instance of the object not the actual object which is the guest//
    }

    @Override
    public String toString() {
        return "Guest{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Room {

    public static final double[] PRICES = {59.50, 72.40, 89.00};
    public static final String[] BED_TYPES = {"Single", "Double", "King Size"};
    private int number;
    private Bed bed;
    private Guest guest;

    public Room(int number, String bedType) {
        this.number = number;
        this.bed = new Bed(bedType);
        this.guest = null;
    }

    public int getNumber() {
        return number;
    }

    public int getFloor() {
        int floor = number / 100;
        return floor;
    }

    public double getPrice() {
        if (bed.isSingle()) {
            return PRICES[0];
        } else if (bed.isDouble()) {
            return PRICES[1];
        } else {
            return PRICES[2];
        }
    }

    public boolean isOccupied() {
        return guest != null;
    }

    public void registerGuest(Guest guest) {
        this.guest = guest;
    }

    public void vacate() {
        guest = null;
    }

    public String getBedType() {
        if (bed.isSingle()) {
            return BED_TYPES[0];
        } else if (bed.isDouble()) {
            return BED_TYPES[1];
        } else {
            return BED_TYPES[2];
        }
    }

    public Guest getGuest() {
        return guest;
    }

    @Override
    public String toString() {
        return "Room{" +
                "number=" + number +
                ", bed=" + getBedType() +
                ", guest=" + guest +
                '}';
    }
}


class Hotel {

    private String name;
    private Room[] rooms;

    public Hotel(String name, Room[] rooms) {
        this.name = name;
        this.rooms = rooms;
    }


    public String getName() {
        return name;
    }

    public int getNumberOfRooms() {
        return rooms.length;
    }

    public int getNumberOfAvailableRooms() {
        int count = 0;

        for (int i = 0; i < rooms.length; i++) {
            if (!rooms[i].isOccupied()) {
                count++;
            }
        }
        return count;
    }

    public Room getFirstAvailableRoom() {

        for (int i = 0; i < rooms.length; i++) {
            if (!rooms[i].isOccupied()) {
                return rooms[i];
            }
        }
        return null;
    }

    public Room getFirstAvailableRoom(double maxPrice) {

        for (int i = 0; i < rooms.length; i++) {
            if (!rooms[i].isOccupied() && rooms[i].getPrice() <= maxPrice) {
                return rooms[i];
            }
        }
        return null;
    }

    public Room[] getAllAvailableRooms(String bedType) {

        ArrayList<Room> list = new ArrayList<>();
        for (int i = 0; i < rooms.length; i++) {
            if (!rooms[i].isOccupied() && rooms[i].getBedType().equals(bedType)) {
                list.add(rooms[i]);
            }
        }
        Room[] rooms = new Room[list.size()];
        return list.toArray(rooms);     //changed due to java.lang.ClassCastException:
    }

    public boolean hasGuest(Guest guest) {

        for (int i = 0; i < rooms.length; i++) {
            if (rooms[i].isOccupied() && rooms[i].getGuest().equals(guest)) {
                return true;
            }
        }
        return false;
    }

    public Room getRoom(Guest guest) {

        for (int i = 0; i < rooms.length; i++) {
            if (rooms[i].isOccupied() && rooms[i].getGuest().equals(guest)) {
                return rooms[i];
            }
        }
        return null;
    }
}

-----------------------------

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Can someone tell me how to create a test for the following Hotel classes that test...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FRQ: Hotel Rooms A & B

    The management of a motel wishes to keep track of the occupancy of rooms in the motel. Each room is represented by an object of the following Room class.public class Room {   private int roomNumber;   private boolean occupied;   private String bookingName;       /** Attempts to check a new guest into the room. If the room is not occupied (the      * variable occupied is false), changes bookingName to name and sets     * occupied to true.     * Otherwise does not change the values of the member variables.     * @param name the name of the guest checking in as a String     * @return true if the check-in was successful and bookingName was changed     *  false otherwise     */   public boolean checkInNewGuest(String name)   {  /* implementation not shown */  }       /** @return the room number */   public int getNumber()   {  return roomNumber;  }   /** @return true if the room is occupied; false otherwise. */   public boolean isOccupied()   {  return occupied;  }     // There may be instance variables, constructors and methods that are not shown. }The following OccupancyInfo class creates and maintains a database of the rooms in the hotel. The rooms are not sorted in the database, but no two...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • How to create a constructor that uses parameters from different classes? I have to create a...

    How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • WRITE IN JAVA: I've been trying to do this for hours and I can't figure this...

    WRITE IN JAVA: I've been trying to do this for hours and I can't figure this out. I tried the toArray method and I'm stumped. I'm not sure if the remove method is right aswell. If you need anymore info, let me know. I need the following methods completed: toArray Remove ReverseArray /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template...

  • Can you help with the merge method? This method should create and return an ArrayBagcontaining one...

    Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT