0

I am trying to design a java program based on the following UML class diagram for an assignment and I am stuck. When I enter a name and xPosition in the create method of the House and ApartmentBuilding classes I end up with the most recently entered results for all of the House objects that are created.enter image description here

Drawable:

import javafx.scene.canvas.GraphicsContext;



 interface Drawable {

   void draw(GraphicsContext canvas);

   public double getXPosition();
   
}

Village:

import javafx.scene.canvas.GraphicsContext;

import java.util.Scanner;

public class Village extends Building{

    public static final double Y_FLOOR = 300;
    private int size;
    private final String villageName;
    private final Building[] buildings;


    private Village(String villageName, int size){
        super(name, xPosition);
        this.size = size;
        this.villageName = villageName;
        this.buildings = new Building[size];
    }

    public static Village create() {

        Scanner scan = new Scanner(System.in);
        Village a;

        System.out.println("Enter name of village: ");
        String villageName = scan.nextLine();
        System.out.println("Enter number of buildings: ");
        int num = scan.nextInt();

        a = new Village(villageName, num);

        for(int i = 0; i < num; i++) {

            System.out.println("Enter type of Building: 1= House, 2= Apartment, 3= Store ");
            int choice = scan.nextInt();

            if (choice == 1){
                a.buildings[i] = House.create();
            }

            if (choice == 2){
                a.buildings[i] = ApartmentBuilding.create();
            }
        }

        return a;

    }


    public int getPopulation(){

        return size;
    }

    public void draw(GraphicsContext canvas){

    }

    public String toString(){

        String str = "\n"+ "Village of " + villageName + "\n\n";

        for (int i=0; i<buildings.length; i++) {
            str = str + buildings[i].toString() + "\n"; // this adds each buildings information to the string
        }
        return str;
    }
}

Building:

import javafx.scene.canvas.GraphicsContext;

public class Building implements Drawable {


    static String name;
    static double xPosition;



    public Building(String name, double xPosition){
            Building.name = name;
            Building.xPosition = xPosition;
    }




    public String getName(){
        return name;
    }


    public void draw(GraphicsContext canvas) {

    }


    public double getXPosition() {
        return xPosition;
    }






    @Override
    public String toString(){
            return "Type... Building:  " + "name= " + getName() + ", xPosition= " + getXPosition() + "\n";}
    }

House:

import javafx.scene.canvas.GraphicsContext;

import java.util.Scanner;

public class House extends Building implements Dwelling  {

    private int bedrooms;
    private int occupants;

    House(String name, double xPosition, int bedrooms, int occupants){
        super(name, xPosition);
        this.bedrooms = bedrooms;
        this.occupants = occupants;


    }
    public static House create() {



        Scanner scan = new Scanner(System.in);
        House a;


        System.out.println("Enter name of the House: ");
        name = scan.nextLine();
        System.out.println("Enter XPosition of the House: ");
        xPosition = scan.nextInt();


        System.out.println("Enter number of bedrooms: ");
        int bedrooms = scan.nextInt();
        System.out.println("Enter number of occupants: ");
        int occupants = scan.nextInt();

        a = new House(name, xPosition, bedrooms, occupants);


        return a;

    }
    public void draw(GraphicsContext canvas){


    }



    @Override
    public String toString(){
        return "House: " + "bedrooms= " + bedrooms + " occupants= " + occupants + "\n" + super.toString();
    }


    @Override
    public int getNumberOfOccupants() {
        return occupants;
    }
}

ApartmentBuilding:

import javafx.scene.canvas.GraphicsContext;

import java.util.Scanner;

public class ApartmentBuilding extends HighRise implements Dwelling{

    private final int occupantsPerFloor;
    private final int numberOfFloors;

    ApartmentBuilding(String name, double xPosition, int numberOfFloors, int occupantsPerFloor){
        super(name, xPosition);
        this.occupantsPerFloor = occupantsPerFloor;
        this.numberOfFloors = numberOfFloors;


    }

    public static ApartmentBuilding create() {

        Scanner scan = new Scanner(System.in);
        ApartmentBuilding a;

        System.out.println("Enter name of the Apartment Building: ");
        name = scan.nextLine();

        System.out.println("Enter XPosition of the Apartment Building: ");
        xPosition = scan.nextInt();
        System.out.println("Enter number of floors: ");
        int numberOfFloors = scan.nextInt();
        System.out.println("Enter number of occupants per floor: ");
        int occupantsPerFloor = scan.nextInt();

        a = new ApartmentBuilding(name, xPosition, numberOfFloors, occupantsPerFloor);


        return a;
    }


    public void draw(GraphicsContext canvas){

    }
    @Override
    public String toString(){

        return "Apartment Building: " + "occupantsPerFloor= " + occupantsPerFloor + "\n" + super.toString() + "\n";
    }

    @Override
    public int getNumberOfOccupants() {
        return 0;
    }
}

Sample output:

enter image description here

I am new at programming and trying my best to learn but I am getting stuck on this, unfortunately.

  • 2
    Does this answer your question? [Why are static variables considered evil?](https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) `name` and `xPosition` in your Building class are static. – AndiCover Jul 29 '21 at 04:42
  • ok this messes up other methods that i have that are static – Delano Petrilli Jul 29 '21 at 05:03
  • You can instead use class instances and call the problematic methods on those class instances. I recommend reading up on some tutorials/documentation first however, otherwise you might have trouble understanding the concepts mentioned: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Koenigsberg Jul 29 '21 at 06:13

0 Answers0