简体   繁体   中英

Update field value in object

I need to update a fields value inside one of my object, just update the String. The object is inside of an array. But I will target the object by typing in the objects "regNum" in the parameter.

This is what I tried, I really don't know how to use the set() method when I need to enter the list and the objects specific value.

public boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput == meterList.get(i).getRegNum()){
            meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
        }
    }
    return true;
}

This Is the whole MeterArchive class that stores the meters and have some methods to it.

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 boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput == meterList.get(i).getRegNum()){
            meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
        }
    }
    return true;
}

public void showAllMeter(){
    for(Meter meter : meterList){
        System.out.println(meter);
    }
}
}

This is the clock class that has specific clock values that you can add.

public class Clock extends Meter
{

/**
 * 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;
}
}

This is the super class that has more general input for the different meters.

public class Meter
{

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;
}
}

So in the MeterArchive class I want to change the field value "workOrNot" from whatever it is (most likely "Yes") to "No". I found out that set() is usually the way to go, but in this program I want the user to add the specific "regNum" and then the method will change to String inside the "workOrNot" field to "No". As I said earlier I dont know how to target the specific field inside the object. Can someone explain how to do this?

You need to use setter method setWorkOrNot() to update field workOrNot on the desired Meter object.

Use the below code:

public boolean doesNotWork( String regNumInput ){
    for(int i = 0; i < meterList.size(); i++){
        if(regNumInput.equals(meterList.get(i).getRegNum())){
            meterList.get(i).setWorkOrNot("No");
        }
    }
    return true;
}

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