简体   繁体   English

如何重置构造函数的获取器和设置器

[英]How to reset the getters and setters of a constructor

in my recent uni work ive been given a task which i believe is to reassign values that have been set by getters and setters. 在我最近的大学工作中,我认为要完成一项任务,即重新分配由getter和setter方法设置的值。 The exact question is " Edit the product's main information that shares among all class such as ID, Description, etc" 确切的问题是“编辑在所有类别之间共享的产品主要信息,例如ID,描述等”

My attempt at this question is as followed. 我对这个问题的尝试如下。

 public void editProductInformation() { //Standard level task 2 method 3

        this.ID = 456890;
        this.description = "Eggs";
        this.recommendedUnitPrice = 10;
        this.Unit = 6;
        this.weight = 750;              
    }

}

this is also the code for the rest of the class 这也是课程其余部分的代码

public class Product {

    Scanner input = new Scanner(System.in);

    protected int ID;
    protected String description;
    protected double recommendedUnitPrice;
    protected double actualPrice;
    protected int Unit = 1;
    protected double weight;
    protected LocalDate expiaryDate;
    protected LocalDate expireDate;

    public Product(int ID, String description, double recommendedUnitPrice, int unit, double weight, LocalDate expiarayDate) {
        this.ID = ID;
        this.description = description;
        this.recommendedUnitPrice = recommendedUnitPrice;
        this.Unit = unit;
        this.weight = weight;
        this.expiaryDate = expiarayDate;

    }

    public int getID() {
        return this.ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescripption(String description) {
        this.description = description;
    }

    public double getRecommendedUnitPrice() {
        return this.recommendedUnitPrice;
    }

    public void setRecommendedUnitPrice(double recommendedUnitPrice) {
        this.recommendedUnitPrice = recommendedUnitPrice;
    }

    public int getUnit() {
        return this.Unit;
    }

    public void setUnit(int unit) {
        this.Unit = unit;
    }

    public double getWeight() {
        return this.weight;
    }

    public void setWeight(double Weight) {
        this.weight = Weight;
    }

    public LocalDate getExpiaryDate() {
        return expiaryDate;
    }

    public void setExpiaryDate(LocalDate expiaryDate) {
        this.expiaryDate = expiaryDate;
    }

    public void setPrice() {
        System.out.println("Enter the price: ");
        this.actualPrice = input.nextDouble();
    }

    public void setExpireDate() {

        System.out.println("Enter the given date: ");
        int day = input.nextInt();
        int month = input.nextInt();
        int year = input.nextInt();
        LocalDate date = LocalDate.of(day, month, year);
        this.expireDate = date;
    }

    public void comparePrice() { // Standard level task 2 method 1

        if (recommendedUnitPrice == actualPrice) {
            System.out.println("Price is equal");
        } else if (recommendedUnitPrice < actualPrice) {
            System.out.println("Price is less");
        } else if (recommendedUnitPrice > actualPrice) {
            System.out.println("Price is more");
        }

    }

    public void verifyExpireProduct() { //Standard level task 2 method 2

        if(expireDate == expiaryDate) {
            System.out.println("Product is expired");
        }
        else System.out.println("Product is not expired");
    }

I understand this question might not be explained well enough so i understand if people wish to downvote it. 我知道这个问题可能解释得不够好,所以我理解人们是否希望对此问题投反对票。 Any insight or help is much appreciated! 任何见解或帮助将不胜感激!

You can make an object of this class and then by using setter function defined in class you can set the value . 您可以创建此类的对象,然后使用类中定义的setter函数来设置值。

Product p1 = new Product();
p1.setId(5);
//This will set the Id to 5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM