如何在分配参数之前验证参数?

时间:2016-10-13 01:20:22

标签: java validation

我想在分配之前检查我的成绩变量的整数参数是否全部有效(非负)。编码的最佳方法是什么?

我是Java初学者,我正在尝试确定最可接受的验证成绩参数的方法。

package com.example.android.StudentRecord;

// Name of the class
public class ReportCard {

    //Declare the private variables
    private long studentID;
    private int quarterYear;
    private String subject1;
    private int subject1Grade;
    private String subject2;
    private int subject2Grade;
    private String subject3;
    private int subject3Grade;


    //Use constructor to initialize all of the parameters of ReportCard
    public ReportCard(long studentID, int quarterYear,
                      String subject1, int subject1Grade,
                      String subject2, int subject2Grade,
                      String subject3, int subject3Grade) {
        this.studentID = studentID;
        this.quarterYear = quarterYear;
        this.subject1 = subject1;
        this.subject1Grade = subject1Grade;
        this.subject2=subject2;
        this.subject2Grade = subject2Grade;
        this.subject3=subject3;
        this.subject3Grade = subject3Grade;
    }


    //Use getter and setter methods to get the values and then set the values

    public long getStudentID()  {return studentID; }

    public void setStudentID(long ID) {
        studentID = ID;
    }

    public int getQuarterYear() {
        return quarterYear;
    }

    public void setQuarterYear(int qYear) {
        quarterYear = qYear;
    }

    public String getSubject1() {
        return subject1;
    }

    public void setSubject1(String subject) {
        subject1 = subject;
    }

    public int getSubject1Grade() {
        return subject1Grade;
    }

    public void setSubject1Grade(int grade) {
        subject1Grade = grade;
    }

    public String getSubject2() {
        return subject2;
    }

    public void setSubject2(String subject) {
        subject2 = subject;
    }

    public int getSubject2Grade() {
        return subject2Grade;
    }

    public void setSubject2Grade(int grade) {
        subject2Grade = grade;
    }

    public String getSubject3() {
        return subject3;
    }

    public void setSubject3(String subject) {
        subject3 = subject;
    }

    public int getSubject3Grade() {
        return subject3Grade;
    }

    public void setSubject3Grade(int grade) {
        subject3Grade = grade;
    }


    //Returns the string representation of the {@link ReportCard} object.

    @Override
    public String toString() {
        return "ReportCard{" +
                "StudentID='" + studentID + '\'' +
                ", QuarterYear='" + quarterYear + '\'' +
                ", " + subject1 + ": " + subject1Grade + '\'' +
                ", " + subject2 + ": " + subject2Grade + '\'' +
                ", " + subject3 + ": " + subject3Grade;
    }
}

1 个答案:

答案 0 :(得分:2)

首先,您需要确保在此处验证参数,或者是否应在用户输入层验证参数。

如果您认为有必要,请在参数无效时抛出IllegalArgumentException

if (subject1Grade < 0 || subject2Grade < 0 || subject3Grade < 0) {
    throw new IllegalArgumentException("Grades must not be negative");
}

或者,如果您宁愿无声地忽略无效参数,请提供默认值。

this.subject1Grade = Math.max(0, subject1Grade);
this.subject2Grade = Math.max(0, subject2Grade);
this.subject3Grade = Math.max(0, subject3Grade);