Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:49:26+00:00 2026-05-27T19:49:26+00:00

I have the problem how to change boolean value of a given class so

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:49:27+00:00Added an answer on May 27, 2026 at 7:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been have trouble understand this problem. If I change the variable name fifthViewController
I hope this is a simple problem. I have a seekbar that I want
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
I have a slight problem... we made a change to our url structure the
I have a weird problem with images in visual web developer, I cant change
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Basically, the initial problem is I need to make a boolean value serialize as
I have this class: @SuppressWarnings(serial) private class DataCellRenderer extends JLabel implements ListCellRenderer { public
i want to have in my application an alertdialog, that has its message updated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.