简体   繁体   中英

Building construction with array list in java

I am a beginer developer and I have to do a java program with an array list. The program is a building constructor in which i must create entrances , floors and apartments in an array list and the program has to work with user iputs using the console. And for example if I want to create an apartment on the 10th floor but there are only 9 floors it has to give me an error message. That is what I did Here is a part of the code:

public class Building {
    static String answer;

public static void main(String[] args) {
    // TODO code application logic here
    Scanner input = new Scanner(System.in);

    List<Entrance> entrance = new ArrayList<Entrance>();
    List<Floors> floor = new ArrayList<Floors>();
    List<Apart> apartment = new ArrayList<Apart>();

    System.out.println("Welcome, choose (A) for operations and (B) for information!");
    answer = input.nextLine();

    if(answer.equals("A")){
        System.out.println("Chose (A) to create entrance , (B) to create floor and (V) to create apartment");
        answer = input.nextLine();

        if(answer.equals("A")){


            System.out.println("Creating entrance...");

            Entrance entranceA = new Entrance();

            entrance.add(entranceA);


            System.out.println("Create the name of the entrance:");
            entranceA.setNameEntrance(input.nextLine());


            System.out.println("How many floors are in this part of the building:");
            entranceA.setNumberFloors(input.nextInt());    
              }


        }else if(answer.equals("B")){

            System.out.println("Creating a floor...");

            Floors floorA = new Floors();
            floor.add(floorA);

            System.out.println("How many aparments are on this floor:");
            floorA.setNumberApart(input.nextInt());







    }


}

I think in terms of the design it can change a bit. For example you can pull out the Entrance, Floor,Apart into a Class called Structure or Building.

You can use what is called composition in Object Oriented terms, which basically means you have a class called Building and it contains the fields window, doors etc (has a relationship)

So basically:

class Building {

public Building(List<Entrance> entrance, List<Floors> floors, List<Apart> apart)
List<Entrance> entrance;
List<Floors> floors;
List<Apart> apart;
}

But Also why not have it so that each Floor can have Apart Objects.

So the class:

class Floor {
List<Apart> apart;
public void addApartmentWithNewOwner(String owner,Mortgage mortgage) {

  etc. etc.
}

This makes more sense because then you can build floors in the building.

Since your question is rather broad, I hope this answer will help you on your way:

  1. You should create the classes for Enterance , Floor and Appart (create a new Java file for each).
  2. You should create a Building object in your main() method. (Eg Building b = new Building() ). Since your main() is static it will not already have an object of Building .
  3. You should move the lists containing the Enterance s, Floor s and Appart ments to the Building class. And you could make them private .
  4. If you made the lists private , you need to add public methods to add/remove Enterance s, Floor s and Appart ments.

    Eg:

    public void addEnterance(Enterence e) { entrance.add(e); }

  5. It would be a good thing to move your application code (eg your main() method) to a separate class (eg BuidlingApp ) to separate the application logic from your domain logic.

I am really not sure what your question is but i think i get what are you trying to accomplish.I am really not sure about your Entrance definition So it does not cover it but it should be easy to extend.

Lets proceed on by one -

i) Let your basic unit be Floor. So, You will have a floor class with a field (integer) to denote the floor number.

ii) There can be multiple apartments on a given floor.

iii) You will have a class ConstructBuilding (Main class) which would hold a HashMap<Floor,List<Apartment>> (To give O(1) operation as it uses hashing.

iv) User needs to create a floor, So create a floor object and adds it to the map.

v) User needs to create an apartment , create an Apartment Object and look it up in the HashMap to get the list of Apartments and add this newly create apartment to the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM