0

I was practicing inheritance and composition after a lesson and decided to write a small program to try some things out, stumbled upon a problem, I'll show my code, it has 4 classes including the Main.java:

public class Main {

public static void main(String[] args) {

    Person person1 = new Person("Person1", 170); //"Name", height
    Person person2 = new Person("Person2", 200); //"Name", height

    Bed bed1 = new Bed(160);

    Bedroom bedroom1 = new Bedroom(bed1, person1);

    bedroom1.sleep();

public class Bedroom {

private Bed theBed;
private Person thePerson;

//Constructors

public void sleep() {
    if(thePerson.getHeight() > 180) {
        System.out.println("You are too tall to sleep on this bed.");
    } else {
        theBed.sleepOnBed();
    }
}

//Getters

public class Bed {

private Person thePerson;
private int height;

//Constructor

public void sleepOnBed() {
        System.out.println("You sleep on the bed.");
}

//Getters 

public class Person {

private String name;
private int height;

//Constructor

//Getters

What I want to do is to use both person1 and person2 in my bedroom1 object in Main.java and then test the sleep() method on both of them but I just can't figure out a way to use it.

I tried things like:

public class Bedroom {

private Bed theBed;
private Person thePerson1;
private Person thePerson2;

public Bedroom(Bed theBed, Person thePerson1, Person thePerson2) {
    this.theBed = theBed;
    this.thePerson1 = thePerson1;
    this.thePerson2 = thePerson2;
} 


public class Main {

public static void main(String[] args) {

    Person person1 = new Person("Person1", 170);
    Person person2 = new Person("Person2", 200);

    Bed bed1 = new Bed(160);

    Bedroom bedroom1 = new Bedroom(bed1, person1, person2);

    bedroom1.sleep(); 

But as you might understand, that lead to nothing. I'm just so tired and can't find any leads online, might be because I use the wrong keywords idk.

I want my program to take multiple objects of data type Person and see if their height matches the conditions to sleep in the bed, that's pretty much it.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
Nedotian
  • 3
  • 3
  • You can define an `array of Person`, and pass that to `Bedroom` constructor. Then you can iterate over the array and check the condition you want. When you do that, you will need to set in the `Bed` object the person that validated the condition. – Santiago Salem Jun 07 '17 at 20:51

1 Answers1

1

You can replace Person with a List of Person objects in the bedroom class and then loop over the array in the sleep method.

public Bedroom(Bed bed1, List<Person> aListOfPersons)
{
  this.persons = aListOfPersons;
}

public void sleep()
{
  for(Person aPerson : persons)
   {
      //check for each aPerson if he fits in the bed
   }
 }

Welcome to Stackoverflow and happy coding!

secustor
  • 3,001
  • 2
  • 14
  • 20
  • 1
    Unless you are using primitives, it's generally a good idea to use `List` instead of an array. That way you don't care what the implementation is. https://stackoverflow.com/questions/1589813/when-to-use-a-list-over-an-array-in-java – Novaterata Jun 07 '17 at 23:52
  • Ah darn it! I haven't gotten to arrays or lists in my course yet! Thanks for the answer and the warm welcome. – Nedotian Jun 08 '17 at 15:30
  • 1
    @Nedotian If this has answered your question, please mark this question as answered. – secustor Jun 08 '17 at 15:41
  • @Novaterata Thanks for the info, I have changed the code accordingly. – secustor Jun 08 '17 at 15:44