存储布尔值

发布于 2024-12-23 17:05:55 字数 1506 浏览 2 评论 0原文

我遇到的问题是如何更改给定类的布尔值,以便再次遇到它时它具有上次设置的值。这是我的类

public class Sandwich {
    private String type;
    private double price;
    private String ing;
    public boolean owned;

    Sandwich (String t, double p, boolean o){
        type = t;
        price = p;
        owned = o;
    }

    public boolean getO(){
        return this.owned;
    }

    public void setO(boolean o){
        this.owned = o;
    }

    public String getType(){
        return this.type;
    }
}

以及它被访问和应该更改的位置:

public void purchase(Sandwich s) {
    boolean owned = s.owned;

    //I tried also with accessor and mutator here but then changed to public
    String type = s.getType();
    if (owned == false) {
        if (money <= 0){ 
            System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have enough money");
        } else {
            System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
            this.money = money;
            owned = true;

            //this is the place where it is supposed to change value to true (sandwich was bought and has owner now
            s.owned = owned;
        }
    } else if (owned == true) {
        System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
        System.out.println("Test");
    }
}

问题是,尽管过去购买了给定的三明治,但每次我尝试运行此代码时,其拥有的值都设置为 false。我需要三明治记录更改后的拥有值,以便下次运行时条件将是拥有== true。怎么可以

I have the problem how to change boolean value of a given class so that once it is encountered again it has the value last set. This is my class

public class Sandwich {
    private String type;
    private double price;
    private String ing;
    public boolean owned;

    Sandwich (String t, double p, boolean o){
        type = t;
        price = p;
        owned = o;
    }

    public boolean getO(){
        return this.owned;
    }

    public void setO(boolean o){
        this.owned = o;
    }

    public String getType(){
        return this.type;
    }
}

and place where it is accessed and supposed to change:

public void purchase(Sandwich s) {
    boolean owned = s.owned;

    //I tried also with accessor and mutator here but then changed to public
    String type = s.getType();
    if (owned == false) {
        if (money <= 0){ 
            System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have enough money");
        } else {
            System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
            this.money = money;
            owned = true;

            //this is the place where it is supposed to change value to true (sandwich was bought and has owner now
            s.owned = owned;
        }
    } else if (owned == true) {
        System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
        System.out.println("Test");
    }
}

Problem is that although a given sandwich was bought in the past its owned value is set to false each time I try to run this code. I need for the sandwich to record changed value of owned so that next time I run the condition will be owned == true. How can it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花肆意 2024-12-30 17:05:55

您的设计似乎存在缺陷。您需要在 Worker 和 sandwish 类型之间创建关系。

您可以做的只是在工人阶级中实现一个购买的三明治类型列表,并在工人购买三明治时与它进行比较。

或者,如果您愿意,您可以拥有所有三明治类型的哈希图,其中包含一个布尔值,指示该类型是否已被购买。

There seems to be a flaw in your design. You need to create a relationship between the Worker and the sandwish type.

What you can do is simply implement a List of purchased sandwish types in the worker class and compare against it whenever a worker purchases a sandwish.

Or if you want, you can have a hashmap of all sandwish types with a boolean value that indicates whether the type has already been purchased or not.

标点 2024-12-30 17:05:55

您创建了 get 和 set 例程,但没有使用它们。我会将代码更改为这样。

    public void purchase(Sandwich s){
            String type = s.getType();
            if (!(s.getO())){
                if (money <= 0){ 
                    System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have eno

ugh money");
                } else {
                    System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
                    this.money = money;
                    s.setO(true);
                }
            } else {
                System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
                System.out.println("Test");
            }
     }

You created get and set routines and then didn't use them. I would change the code to this.

    public void purchase(Sandwich s){
            String type = s.getType();
            if (!(s.getO())){
                if (money <= 0){ 
                    System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have eno

ugh money");
                } else {
                    System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
                    this.money = money;
                    s.setO(true);
                }
            } else {
                System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
                System.out.println("Test");
            }
     }

只需删除

boolean owned = s.owned;

您使用过 owned

并使用 s.getO() ,例如

if (owned == false){ 

即可

if (!s.getO()){

并使用 setter 方法 s.setO(true/false) 更改它。

例如

owned = true;
s.owned = owned;

可以替换为

s.setO(true);

Just remove

boolean owned = s.owned;

and use s.getO() where you have used owned

e.g.

if (owned == false){ 

can be

if (!s.getO()){

And use the setter method s.setO(true/false) to change it.

e.g.

owned = true;
s.owned = owned;

can be replaced by

s.setO(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文