I am new to Java and am using BlueJ.
Overview of the context of the program.
I am trying to create a Club Database (arraylist, not actual database) where the user can add a new climber (name, age, gender) and which mountain they've climbed (name, height) to the arraylist.
I've created the Climber class and the mountain class. I've also created an ArrayList for the climbers and can add to this array. My question is.. How can I add a climber to the climber ArrayList, and be able to add which mountain they've climbed and its height at the same time?
The method of adding a climber needs to access both the Climber and Mountain class?
Whilst code solving the issue is appreciated, would be helpful if I was shown in the right direction so I can understand it more!
Thanks.
import java.util.ArrayList; import java.util.Scanner; /** * Write a description of class ClubStats here. * * @author (your name) * @version (a version number or a date) */ public class ClubStats { // An ArrayList for storing climber details. private ArrayList<Climber> climbers; // An ArrayList for storing mountain details. private ArrayList<Mountain> mountains; /** * Constructor for objects of class ClubStats */ public ClubStats() { // Initialise instance variables. climbers = new ArrayList<Climber>(); mountains = new ArrayList<Mountain>(); } public void addClimber(Climber newName) { climbers.add(newName); } public Climber getClimber(String name) { Climber foundClimber = null; int index = 0; boolean searching = true; while(searching && index < climbers.size()) { Climber climber = climbers.get(index); if(climber.getName().equals(name)) { searching = false; foundClimber = climber; } else { System.out.println(name + " not found"); index++; } } return foundClimber; } public void addMountain(Mountain newName) { mountains.add(newName); } public Mountain getMountain(String name) { Mountain foundMountain = null; int index = 0; boolean searching = true; while(searching && index < mountains.size()) { Mountain mountain = mountains.get(index); if(mountain.getName().equals(name)) { searching = false; foundMountain = mountain; } else { System.out.println(name + " not found"); index++; } } return foundMountain; } } public class Climber { // Instance variables. // The climber name. private String name; // The climber age private int age; // The climber gender. private String gender; /** * Constructor for objects of class Climber */ public Climber(String newName, int newAge, String newGender) { // Initialise instance variables. name = newName; age = newAge; gender = newGender; } /** * Accessor method for climber's name. */ public String getName() { return name; } /** * Set the climber's name. */ public void setName(String newName) { name = newName; } /** * Accessor method for climber's age. */ public int getAge() { return age; } /** * Set the climber's age. */ public void setAge(int newAge) { age = newAge; } /** * Set the climer's gender. */ public String getGender() { return gender; } /** * Accessor method for climber's gender. */ public void getGender(String newGender) { gender = newGender; } } public class Mountain { // Instance variables. private double height; private String name; /** * Constructor for objects of class Mountain */ public Mountain(String mName, double mHeight) { // Initialise instance variables name = mName; height = mHeight; } /** * Accessor method for mountain name. */ public String getName() { return name; } /** * Set the mountain name. */ public void setName(String newName) { name = newName; } /** * Accessor method for mountain height. */ public double getHeight() { // put your code here return height; } /** * Set the mountain height. */ public void setHeight(double newHeight) { height = newHeight; } }