简体   繁体   中英

Initialized ArrayList doesn't show up

I have this code of a larger assignment. I have omitted any unnecessary code. The thing is that I'm asked to initialize the ArrayList by adding objects to it. However, when I type 3(the option to show the buildings), nothing is shown but the menu again. Any help will be appreciated!

PS I do NOT ask you to solve my assignment. We have done similar smaller case examples in the labs of my uni. However, I can't get it to work for some reason and I can't find out why.

import java.util.*;

public class mainApp {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BuildingList myBList = new BuildingList();
        ExpenseList myExpList = new ExpenseList();
        BuildingExpenseList myExpTList = new BuildingExpenseList();
        boolean done = false;

        while (!done)
        {
            System.out.println("\n1. Insert new building");
            System.out.println("2. Insert new expense");
            System.out.println("3. Show all buildings");
            System.out.println("4. Show the expenses for a building");
            System.out.println("5. Calculate the cost for a building");
            System.out.println("6. Calculate the cost of an expense");
            System.out.println("0. Exit");
            System.out.print("> ");
            answer = in.nextLine();

            if (answer.equals("3"))
            {
            myBList.showBuilds();
            }
            else if (answer.equals("0")) done = true;
       }

       Building b1 = new Building("1", "Offices", "5th Avenue", 130, 340);
       Building b2 = new Building("2", "Shop", "St. John", 100, 200);
       Building b3 = new Building("3", "Fire Station", "Weston Road", 250, 550);
       Building b4 = new Building("4", "Police Station", "Two Bridges Road", 700, 1000);
       Building b5 = new Building("5", "Restaurant", "St Nicholas Avenue", 400, 600);

       myBList.addBuilding(b1);
       myBList.addBuilding(b2);
       myBList.addBuilding(b3);
       myBList.addBuilding(b4);
       myBList.addBuilding(b5);
  }
}

import java.util.ArrayList;

public class BuildingList
{
    ArrayList<Building> Buildings = new ArrayList<Building>();

    public void addBuilding(Building b)
    {
        Buildings.add(b);
    }
    public void showBuilds()
    {
        for (Building b : Buildings)
        {
        System.out.println(b.printBuilding());
        }
    }
}

public class Building
{
    String code;
    String description;
    String address;
    float areaPrice;
    float sqM;

    public Building (String code, String description, String address, float areaPrice, float sqM)
    {
    this.code = code;
    this.description = description;
    this.address = address;
    this.areaPrice = areaPrice;
    this.sqM = sqM;
    }
    public String printBuilding()
    {
        return "Code " +code+ "\t Description " +description+ "\t Address " +address+ "\t Area Price ";
    }

The problem is that the list is never filled when you press "3":

This Code:

   Building b1 = new Building("1", "Offices", "5th Avenue", 130, 340);
   Building b2 = new Building("2", "Shop", "St. John", 100, 200);
   Building b3 = new Building("3", "Fire Station", "Weston Road", 250, 550);
   Building b4 = new Building("4", "Police Station", "Two Bridges Road", 700, 1000);
   Building b5 = new Building("5", "Restaurant", "St Nicholas Avenue", 400, 600);

   myBList.addBuilding(b1);
   myBList.addBuilding(b2);
   myBList.addBuilding(b3);
   myBList.addBuilding(b4);
   myBList.addBuilding(b5);

Must be implemented before the while loop.

Also, try implementing your builder class like this, constructing the ArrayList in the objects constructor.

public class BuildingList
{
    List<Building> Buildings;

    public BuildingList() {
        Buildings = new ArrayList<Building>();
    }

    public void addBuilding(Building b)
    {
        Buildings.add(b);
    }
    public void showBuilds()
    {
        for (Building b : Buildings)
        {
            System.out.println(b.printBuilding());
        }
    }
}

And if you want to look really cool for your professor, you can substring the ArrayList.toString() method, remove and replace the commas with the new line character "\\n". You can do this in one System.out.println() call, as opposed to a loop

There is no way it could print the list because the values are inserted after the loop execution.
Cut all the code that puts the values in the myBList, paste it above the loop and execute it.

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