Ricardo
Ricardo

Reputation: 97

Call method from Object within Object?(Java)

Ive tried searching and reading trough (similar sounding) problems, but I cant seem to find a solution.

I am currently making a prototype to control some lamps with a text input. The text input is already working and it can correctly filter trough the inputs and only pick out the relevant parts.

Anyway, for that to be usefull now I need to first create Objects of my House, which contain all floors, which contain all rooms, which contains all actors(Lamps etc.).

These are the snippets for all those Objects, from top to bottom as theyre supposed to be stacke inside eachother(The code isnt finished yet nor optimized, I know I can use switch-case for all the if-else statements):

import java.util.ArrayList;

public class Building {
    String Name;
    ArrayList<floor> floor = new ArrayList<floor>();

    public Building(String Name){
        this.Name = Name;
    }

    public boolean checkFloor(String Name){
        int count = floor.size();
        boolean ret = false;
        while(count < floor.size()){
            if (floor.get(count).Name == Name){
                count = floor.size();
                ret = true;
            }
            count++;
        }        
        return(ret);
    }

    public void newFloor(String Name){
        Name = Name.toLowerCase();
        if (Name == "keller" || Name == "basement"){
            floor basement = new floor("basement");
            floor.add(basement);
        }
        else if (Name == "eg"){
            floor ground = new floor("eg");
            floor.add(ground);
        }
        else if (Name == "1og"){
            floor first = new floor("first");
            floor.add(first);
        }
        else if (Name == "2og"){
            floor second = new floor("second");
            floor.add(second);
        }
        else if (Name == "3og"){
            floor third = new floor("third");
            floor.add(third);
        }    
            }    
    public void newRoom(String Floor, String Name){        
    }

}

    public class floor {
    String Name;
    ArrayList<room> room = new ArrayList<room>();
    transcoder transcode = new transcoder();

    public floor(String Name){
        this.Name = Name;
    }



    public boolean checkRoom(String Name){
        int count = room.size();
        boolean ret = false;
        while(count < room.size()){
            if (room.get(count).Name == Name){
                count = room.size();
                ret = true;
            }
            count++;
        }

        return(ret);
    }

    public void newRoom(String mName){
        String Name = transcode.getRoom(mName);
        if( Name == "ground"){
            room ground = new room("ground");
            room.add(ground);
        }
        else if ( Name == "basement"){
            room basement = new room("basement");
            room.add(basement);
        }
        else if ( Name == "first"){
            room first = new room("first");
            room.add(first);
        }
        else if ( Name == "second"){
            room second = new room("second");
            room.add(second);
        }      
    }
}

public class room {
    String Name;
    ArrayList<Actor> Actors = new ArrayList<Actor>();

    public room(String Name){
        this.Name = Name;
    }

    public void addActor(String Name, int Type, String Address, int Channel, boolean Dim){
        Actors.add(new Actor(Name, Type, Address, Channel, Dim));
    }

    public void removeActor(String Name){
        int count = 0;
        while (count <= Actors.size()){
           if (Actors.get(count).Name == Name){
               Actors.remove(count);
               count = Actors.size();
           }
           count++;
        }
    }

    public boolean containsActor(String Name){
        int count = 0;
        boolean ret = false;
        while (count < Actors.size()){

                if (Actors.get(count).Name == Name){
                    ret = true;
                    count = Actors.size();
                }
                count++;
            }
        return(ret);
    }

    public String getAddress(String Name){
        int count = 0;
        String ret = "leer";
        while (count < Actors.size()){
            if (Actors.get(count).Name == Name){
                ret = Actors.get(count).Address;
                count = Actors.size();
            }
            count++;
        }
        return(ret);
    }

    public int getType(String Name){
        int count = 0;
        int ret = 0;
        while (count < Actors.size()){
            if (Actors.get(count).Name == Name){
                ret = Actors.get(count).Type;
                count = Actors.size();
            }
            count++;
        }
        return(ret);
    }

}

public class Actor {
    String Name;
    String Address;
    int Channel;
    int Type;
    boolean Dim;
    int On; //muss noch deklariert werden!
    int Off;


    public Actor(String Name, int Type, String Address, int Channel, boolean Dim){
       this.Name = Name;
       this.Type = Type;
       this.Address = Address;
       this.Channel = Channel;
       this.Dim = Dim;
    }
}

What I am trying to do now in my Mainclass, is to create a new Building with a floor, room and some actors. The code goes as follow:

System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor

Now at the end at Building.floor, I dont quite see how I can now add a new Room to the object floor. Id appreciate any help, as Im not quite that fancy with Java yet and probably am missing an obvious part.

Upvotes: 1

Views: 225

Answers (2)

Daniel Oram
Daniel Oram

Reputation: 8411

To answer your question, the simplest way to add new room to the floor of the building is thus:

Building.floor.get(0).newRoom(Name);

floor is an ArrayList and calling get() on the first index will allow you to then call newRoom().

Some suggestions to make your code more readable/understandable:

  • Capitalize your class names by changing floor to Floor and room to Room.

  • Change the name of your ArrayList from floor to floors.

  • Change the name of your ArrayList from room to rooms.
  • Declare separate String variables in your main class for storing user input.
  • Store your objects separately before adding to the appropriate ArrayList.

The most crucial thing I would say to add is a getFloor() method to your Building class.

like this (assuming you make the above changes):

public Floor getFloor(String floorName){
    Floor correctFloor = null;
    for(Floor floor : floors) {
        if(floor.Name == floorName) {
            correctFloor = floor;
        }
    }
    return correctFloor;
}

Then you instead of

System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor.get(0).newRoom(Name);

you can put (again assuming the above changes)

System.out.println("Gebäudename eingeben(egal): ");
String buildingName = user_input.nextLine();
Building building = new Building(buildingName);
System.out.println("Stockname eingeben(eg): ");
String floorName = user_input.nextLine();
Floor floor = new Floor(floorName);
building.newFloor(floorName);
System.out.println("Raumname eingeben(wohnen): ");
String roomName = user_input.nextLine();
building.getFloor(floorName).newRoom(roomName);

These are just suggestions and there are many ways you could do it~ Good Luck!

Upvotes: 1

LumberSzquatch
LumberSzquatch

Reputation: 1083

In your Building class, make use of getters and setters. You have a floor variable but don't have any way to get it. Add this getter method to Building

public ArrayList<floor> getFloors(){
        return floor;
    }

Then in instead of saying Building.floor you can say something like

int floorToGet = (what ever floor in the ArrayList you want);
Building.getFloors.get(floorToGet).newRoom("New Room Name");

Also if I were to make a couple suggestion about your coding style and also some advice. By convention all classes start with upper case, i.e. Building as opposed to building you do this in some classes but not others. Also if you are wanting use syntax like Building.floor it's a good idea to make that variable static. Otherwise make getters/setters for the variables and instantiate a new object of building. And also by convention, variable names start lower case, i.e. in this case Building building = new Building(), what you have at first looks like your accessing a static variable that actually is not static.

One last suggestion if your familiar with this concept. It might make data access more easy if you subclass out some of your classes. And what I mean is, Buildings contain Floors, and Floors contain Rooms. So if it's not confusing, Building would be your main class, and then Floors extends Building, and Room extends Floor.

Just from a logical stand point I think that makes more sense. But hopefully this helps.

Upvotes: 1

Related Questions