-1

I am building this program:

A farm that sells eggs to customers charges $3.25 for a dozen eggs, or 45 cents for individual eggs that are not part of a dozen. Write a program that prompts a user for the number of eggs and then displays the amount owed with a full explanation.

I am experiencing problems with making the program aware of what is and isn't a dozen and how to calculate what is left after subtracting the dozen. So far, this is what I have:

import javax.swing.JOptionPane;
public class Eggs
{
    public static void main(String[] args)
    {
        String orderString;
        final double DOZEN = 3.25;
        final int INDIVIDUAL = 45;
        int order;
        
        orderString = JOptionPane.showInputDialog(null, "How many eggs do you want?", "Order dialog", JOptionPane.INFORMATION_MESSAGE);
        boolean isDozen = (orderstring >= 12);
        
        order = Double.parseDouble(orderString) * DOZEN;
        
        String.out.print("You ordered " + orderString + " eggs.");
        
    }
}

Before including the boolean variable I was not getting errors, but after that I was. I thought I was to use it to create a conditional statement, But I am confused now.

Thank you for assisting.

Pointy
  • 405,095
  • 59
  • 585
  • 614
Faerydust
  • 1
  • 1
  • Java is not JavaScript. – Pointy Mar 25 '21 at 12:29
  • You cannot compare the String `orderstring` to a number. You must convert the string to a number first, and compare the result. Also, you probably want to work with integers as much as possible. Among the advantages of doing so in this particular program is that the integer division (`/`) and modulus (`%`) operators will help you break the egg count into dozens and left over individual eggs. – John Bollinger Mar 25 '21 at 12:34
  • You may ultimately need a conditional depending on your goal (otherwise why create a boolean). But your immediate problem has been answered [here](https://stackoverflow.com/a/66799622/1552534) – WJS Mar 25 '21 at 12:50

2 Answers2

1

You're trying to compare a String (orderstring) with an Integer (12), which is not possible this way.

You should parse the input value into an Integer. Then you can compare this orderValue with an Integer:

Integer orderValue = Integer.parseValue(orderstring);
boolean isDozen = (orderValue >= 12);

But you have to handle a possible ParseException or something similar.

René Pöpperl
  • 567
  • 5
  • 18
0

I am experiencing problems with making the program aware of what is and isn't a dozen

Well, let's take your code:

String orderString;
orderString = JOptionPane.showInputDialog(null, "How many eggs do you want?", "Order dialog", JOptionPane.INFORMATION_MESSAGE);
boolean isDozen = (orderstring >= 12);

That can'T work since orderString is a String and 12 is an int. You need to parse the string to an int first:

int order = Integer.parseInt(orderString); //Task: what happens is the user enters "a dozen" and how would you prevent that?
boolean isDozen = order > 12;

Is a conditional statement required here?

You don't use it and it's actually not needed. Why? You do not only need whether the order is more than a dozen but actually how many dozens you have - and any additional individuals.

So what you need is:

int dozens = order / 12; //Task for you: find out why 13/12 == 14/12 etc.
int indiv = order % 12; //Task: find out why 25 % 12 works and returns 1

Then you can calculate your final price as well as generate the detailed explanation from those values.

Finally, think about this part of your code:

final double DOZEN = 3.25; final int INDIVIDUAL = 45;

As well as

A farm that sells eggs to customers charges $3.25 for a dozen eggs, or 45 cents for individual eggs

If DOZEN is meant to be the price for a dozen eggs and in dollars, then what does INDIVIDUAL represent? How would you calculate here?

What I'm trying to get at: do not unnecessarily mix units, either use cents or dollars, so the values would either be 325 and 45 (cents) or 3.25 and .45 (dollars). While you could use different units this will make it harder to use and easier to introduce errors.

Thomas
  • 87,414
  • 12
  • 119
  • 157