Cafeteria Prices

  Download Source Code

/*** Cafeteria ***********************************
Ronald works in the school cafeteria. He'd like to have
a program that lets him easily type in the names of food
items, and then display the prices and add them up.
He decided to use a 2-letter abbreviation for each item.
Here is a first version of the program.
**************************************************/
String food, more ;
float price, total;
 
void setup()
{
  do                       // repeat for each new customer
  { total = 0.00;
    do                           
    {                          // repeat for each food item
      food = input("Food item");
      if( food.equals("ha") )       // hamburger
      {  price = 3.50; }
      else if (food.equals("ff"))   // french fries
      {  price = 1.75; }
      else if (food.equals("ap"))   // apple
      {  price = 0.90; }
      else if (food.equals("dr"))   // drink
      {  price = 1.40; }
      else
      {  price = 0.00; }            // unknown item
      println(food + "\t" + price);
      total = total + price;     
    } while(!food.equals(""));  // press ENTER after last item
 
    println("Total = " + total);
    more = input("Next customer (or type 'quit')");
  } while (!more.equals("quit"));
}

public String input(String prompt)
{ return javax.swing.JOptionPane.showInputDialog(null,prompt); }

ha    3.5
ff    1.75
dr    1.4
    0.0
Total = 6.65


String Comparisons

A String variable is an Object.  This makes it different from int and float variables, which are fundamental data types.
A fundamental type only contains data.  An Object contains both data and methods.  Methods provide a behavior that
the Object can perform.  Other methods perform data manipulations. 

Fundamental types are compared by boolean operators :  = = , != ,  > , < ,  >= , <= .  That checks the data values in the same way
that people compare numbers.   So  ( 5 < 25/4 ) is true.

We cannot use = = to compare Objects.  An Object is like a box that contains some data and methods.  When we compare Objects
by = = , we are checking whether two boxes are the same - this does NOT check the contents of the boxes.  So the following
decision does not work sensibly:

      String name = "Farmer";

   String  job = "Farmer";

   if( name == job)
   {  output("That is a coincidence");  }

Checking ( name == job ) will never be true, because they are two different variables - two different boxes.

The program shows the correct way to check for equal Strings (Strings with the same content)

   if( name.equals( job ) )

That is the correct way to check whether two String variables contain the same data.

Case-Sensitive Comparison

Java is case-sensitive.  That means it thinks that capital and small letters are different - not equal.
In most situations, people don't care about capital and small letter differences.  We can do a non-case-sensitive comparison
by using .equalsIgnoreCase, like this:

      String name = "Farmer";

   String  job = "farmer";

   if( name.equalsIgnoreCase(job) )
   {  output("That is a coincidence");  }

In this case, the computer "thinks" that JOB and NAME are equal and displays "That is a coincidence."

Practice