I want to remove an object from an ArrayList when I type in on of the object fields value in the input field.
I have a superclass with general info for the different meters I will add.
public class Meter
{
// instance variables - replace the example below with your own
public String regNum;
public String workOrNot;
public String location;
/**
* Constructor for objects of class Clock
*/
public Meter(String regNum, String workOrNot, String location)
{
// initialise instance variables
setRegNum(regNum);
setWorkOrNot(workOrNot);
setLocation(location);
}
//REGISTRATION NUMBER
public void setRegNum(String regNum){
this.regNum = regNum;
}
public String getRegNum(){
return regNum;
}
//WORK OR NOT
public void setWorkOrNot(String workOrNot){
this.workOrNot = workOrNot;
}
public String getWorkOrNot(){
return workOrNot;
}
//LOCATION
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return location;
}
}
Then I have a Clock class that extends the superclass and has some more values.
public class Clock extends Meter
{
// instance variables - replace the example below with your own
public double minTime;
/**
* Constructor for objects of class Clock
*/
public Clock(String regNum, String workOrNot, String location, double
minTime)
{
// initialise instance variables
super(regNum, workOrNot, location);
setMinTime(minTime);
}
//MINNIMUM TIME
public void setMinTime(double minTime){
this.minTime = minTime;
}
public double getMinTime(){
return minTime;
}
//EQUALS METHOD --- NOT SURE WHAT IT SHOULD DO... YET!
public boolean equals (Clock other){
return location.equals(other.location);
}
public String toString(){
String retur = super.toString() + "regNum: " + regNum +
"Does it work: " + workOrNot +
"Location: " + location +
"Min time value: " + minTime;
return retur;
}
}
Then I have an archive class that creates an arrayList that holds the different objects and also will have a method to remove object, when the user gets prompted with an field where they can type in the "regNum" as a String.
Then the plan is that, it iterates over the List and finds the one object with the same "regNum" and removes it from the List.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MeterArchive
{
// instance variables - replace the example below with your own
ArrayList<Meter> meterList = new ArrayList<Meter>();
public void createClocks(){
Clock clockOne = new Clock("KH001", "Yes", "ClassRoom005", 0.0);
meterList.add(clockOne);
Clock clockTwo = new Clock("KH002", "Yes", "ClassRoom006", 0.0);
meterList.add(clockTwo);
}
public void remove( String regNumInput){
for(Meter meter : meterList){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput == meterList.get(i).getRegNum()){
remove(meterList.get(i));
}else{
System.out.println("Did not found");
}
}
}
}
public void showAll2(){
System.out.println(meterList);
}
}
Can anyone explain how to make the remove method in MeterArchive class remove the object that I want to remove, when I type in the correct "regNum" in the inputfield. I know that I can use the remove() method but not sure how to use it here. Thank you!