Reputation: 135
I have record which is called Station and I created 3 instances of the object. They are named for different stations. I wish to have the user enter the name of the station they want to know more about and it shows the user the information about that station.
However when I use my getter method to obtain the information because the user input is a string and the getter method wants the station object in order to function, what is the best process for achieving this while staying expandable (Being able to add more stations on the fly).
My code is as follows:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Station Reading = CreateStation("Reading", "Great Western", true);
Station Bristol = CreateStation("Bristol", "Great Western", false);
Station York = CreateStation("York", "Great Eastern", true);
System.out.println("What Station do you need to know about? ");
String UsrStation = s.nextLine();
System.out.println(staGetName(York)+" "+staGetOp(York)+" "+staGetStep(York));
}
public static Station CreateStation(String StationName, String Operator, Boolean StepFree){
Station s = new Station();
staSetName(s, StationName);
staSetOp(s, Operator);
staSetStep(s, StepFree);
return s;
}
//Getter Methods for Station
public static String staGetName(Station s){
return s.name;
}
public static String staGetOp(Station s){
return s.operator;
}
public static Boolean staGetStep(Station s){
return s.stepFree;
}
//Setter Methods for Station
public static Station staSetName(Station s, String name){
s.name = name;
return s;
}
public static Station staSetOp(Station s, String operator){
s.operator = operator;
return s;
}
public static Station staSetStep(Station s, Boolean stepFree){
s.stepFree = stepFree;
return s;
}
}
class Station{
String name;
String operator;
Boolean stepFree;
}
Sorry if this is simple, or bad practice. I am only just learning!
Upvotes: 0
Views: 1430
Reputation: 986
If you want to add only one station of one type i.e unique Stations then Use Set
.
if not then use List
.
Override equals()
in Station class. which is used to compare the two stations.
class Station{
String name;
String operator;
Boolean stepFree;
// getters and setters
//Override toString()
@Override
public boolean equals(Object o) // will compare objects based on name
{
return ((Station)o).getName().equals(this.getName);
}
}
Main class
class Main {
public static void main(String[] args) {
List<Station> list= new ArrayList<>();
list.add(new Station("York", "American Railways",true));
list.add(new Station("Moscow", "Russian Railways",false));
list.add(new Station("Paris", "French Railways",true));
System.out.println("What Station do you need to know about? ");
String UsrStation = s.nextLine();
Station station =new Station();
station.setName(UsrStation); // Preparing object to compare with List
for (Station s:list)
{
if (station.equals(s))
System.out.println(s); //Print your s object
}
}
If your list has unique stations then use Set and override hashcode()
and equals()
in Station class
Upvotes: 1
Reputation: 1249
I think this is something you wanted. I have implemented a skeleton, it's up to you to define it to you needs.
public static void main(String[] args) {
List<Station> listOfStations = new ArrayList<>();
listOfStations.add(new Station("York", "American Railways"));
listOfStations.add(new Station("Moscow", "Russian Railways"));
listOfStations.add(new Station("Paris", "French Railways"));
Scanner scanner = new Scanner(System.in);
String getStationName = scanner.nextLine();
listOfStations.stream().filter((s)-> s.getNameOfTheStation().contains(getStationName)).forEach((s)-> System.out.println(s.toString()));
}
public class Station {
private String nameOfTheStation;
private String operatorOfTheStation;
public Station() {
}
public Station(String nameOfTheStation, String operatorOfTheStation) {
this.nameOfTheStation = nameOfTheStation;
this.operatorOfTheStation = operatorOfTheStation;
}
/**
* @return the nameOfTheStation
*/
public String getNameOfTheStation() {
return nameOfTheStation;
}
/**
* @param nameOfTheStation the nameOfTheStation to set
*/
public void setNameOfTheStation(String nameOfTheStation) {
this.nameOfTheStation = nameOfTheStation;
}
/**
* @return the operatorOfTheStation
*/
public String getOperatorOfTheStation() {
return operatorOfTheStation;
}
/**
* @param operatorOfTheStation the operatorOfTheStation to set
*/
public void setOperatorOfTheStation(String operatorOfTheStation) {
this.operatorOfTheStation = operatorOfTheStation;
}
@Override
public String toString() {
return String.format("%s operated by %s", getNameOfTheStation(), getOperatorOfTheStation());
}
}
Upvotes: 0
Reputation: 557
The ideal way to set the Station
class would be like to have the getters and setters for your instance class and not in main
class.
class Station{
String name;
String operator;
Boolean stepFree;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public Boolean getStepFree() {
return stepFree;
}
public void setStepFree(Boolean stepFree) {
this.stepFree = stepFree;
}
}
You can create new stations by adding constructor to above class.
Like
public Station(String name, String operator, Boolean stepFree) {
super();
this.name = name;
this.operator = operator;
this.stepFree = stepFree;
}
By this, you can create new objects in your main
class like,
Station york = new Station("York", "Great Eastern", true);
Upvotes: 0