简体   繁体   中英

How do I use the results from some values in an object class to calculate others and set when created?

I have an object class Pupilinfo

public class Pupilinfo {
    public Pupilinfo(String name, String level, int yearstarted, String currentclass) {
        this.name = name;
        this.level = level;
        this.yearstarted = yearstarted;
        this.currentclass = currentclass;
    }

    String name, level;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public int getYearstarted() {
        return yearstarted;
    }

    public void setYearstarted(int yearstarted) {
        this.yearstarted = yearstarted;
    }

    public String getCurrentclass() {
        return currentclass;
    }

    public void setCurrentclass(String currentclass) {
        this.currentclass = currentclass;
    }

    int yearstarted;
    String currentclass = "";
}

The student currentClass is a mixture of their currentyear (based on actual year and when they started) and currentlevel hence something like 10b1

I'd like any Pupil's currentClass to default to the result of

public String myclass() {
    String theclass;
    int myyear;
    DateFormat dateFormatmonth = new SimpleDateFormat("MM");
    DateFormat dateFormatyr = new SimpleDateFormat("yyyy");
    Date date = new Date();


    int monthnow = Integer.parseInt(String.valueOf(date));
    int yearnow = Integer.parseInt(String.valueOf(date));
    if (monthnow > 8) {
        Log.d("dates", "is passed August");
        myyear = yearnow - this.yearstarted + 7;
        Log.d("dates", "myyear");
    } else {
        Log.d("dates", "is before September");
        myyear = yearnow - this.yearstarted + 6;
    }


    return myyear + this.level;}

I'm really struggling being very new to this. I dont even know if it's possible.

Help?

Sorted. Call the Method from inside the constructor

public class Pupilinfo {
public Pupilinfo(String name, String level, int yearstarted, String theclass) {
    this.name = name;
    this.level = level;
    this.yearstarted = yearstarted;
    this.currentclass =myclass(yearstarted,this.level);

}

@Override
public String toString() {
    return "Pupilinfo{" +
            "name='" + name + '\'' +
            ", level='" + level + '\'' +
            ", yearstarted=" + yearstarted +
            ", currentclass='" + currentclass + '\'' +
            '}';
}

String name, level;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLevel() {
    return level;
}

public void setLevel(String level) {
    this.level = level;
}

public int getYearstarted() {
    return yearstarted;
}

public void setYearstarted(int yearstarted) {
    this.yearstarted = yearstarted;
}

public String getCurrentclass() {
    return currentclass;
}

public void setCurrentclass(String currentclass) {
    this.currentclass = currentclass;
}

int yearstarted;
String currentclass = "";



public String myclass(int ystart,String lvl) {
    String theclass;
    int myyear;
    DateFormat dateFormatmonth = new SimpleDateFormat("MM");

    Date date = new Date();
    Log.d("date", String.valueOf(date));
    int yearnow = Calendar.getInstance().get(Calendar.YEAR);

    int monthnow = Integer.parseInt(dateFormatmonth.format(date));
    if (monthnow > 8) {
        myyear = yearnow - ystart + 7;
    } else {
        myyear = yearnow - ystart + 6;
    }

    return myyear + lvl;
}
}

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