Lesson 12 – On Conditionals
Student Handout: Java Programming
Error Checking With Conditionals
One problem with the basic VendingMachine class as illustrated in the addendum is that it lacks any error checking. That is, the user can enter a negative dollar amount or get a product before inserting enough money. This unit will add error checking to prevent these and other error conditions from occurring. To do this, students will add conditional statements. The basic format of a conditional is as follows:
if (some_expression_is_true)
{
// execute the appropriate commands
}
The method will execute the commands that exist between the brackets { } only when the expression on the if line is true. When the expression is false, those commands will be ignored. You can extend conditionals in several ways. You can also check more than one expression at a time. Use && (“and”) to see if both expressions are true and ||(“or”) to see if any one of the expressions is true.
if (first_expression_is_true && another_expression_is_true)
{
// execute the appropriate commands when both expressions are true}
}
if (next_expression_is_true || last_expression_is_true)
{
// execute the commands when at least one expression is true
}
You can check for multiple independent (mutually exclusive) conditions and provide a default condition using else if and else options as follows:
if (first_expression_is_true)
{
// execute the appropriate commands for this condition
}
else if (next_expression_is_true)
{
// execute the commands related to this condition
}
…
Use your VendingMachine project and class in the editor and modify it as noted as follows. Try to compile your class as you go along. Create a new instance of the VendingMachine class and test all methods. Ensure your error checking is working correctly.
YOUR JOB:
Complete for
part one of the Vending Machine Project
In main
·
Call setType(String type)
·
Call productType = getType() and in main and print out productType
·
Create a
get and set method for productType
·
Create a totalMoney method that sets and
gets the instance variable total money
// Remember to use the appropriate input
and return arguments
For part Two
add
·
Create
a constructor that initializes your instance variable
·
Enhance the VendingMachine class with conditionals to
include error checking. Users should not be able to do the following:
o Add a negative dollar amount
o Add an amount exceeding some predetermined limit
o Get a product until enough money has been inserted
o Insert money if no products are available
·
Comment
everything
·
Save, compile,
and test OFTEN!
·
Make sure your code is indent properly
EXTRAS YOU CAN ADD:
· Get creative and make the program better. Add more error checking, offer more explicit error messages, and display more information for the user. If you’re really adventurous go to the Sun tutorials and see if you can add an Exception.
VendingMachine Class Without Error Checking
/**
* VendingMachine.java
* This class simulates a vending machine. Each machine
can sell
* one type of product at a fixed price.
*
* @author (Your name here)
* @version (Today's date here)
*/
public
class VendingMachine
{
// instance variables
private String productType; // type of product
private int productPrice; // product's price
private int numberSold; // total number of units sold
private int balance; // amount of money buyer inserted
private int totalMoney; // total $ taken by this machine
//constructor
public VendingMachine(){
// This is where instance variables are initialized
}
//the public methods:
/**
* Method to set this machine's type of product
*/
public void setType(String type)
{
productType = type;
} // end of setType()
/**
* Return this machine's type of product
*/
public String getType()
{
return productType;
} // end of getType()
/**
* Allow buyer to insert money
*/
public void insertMoney(int money)
{
balance += money;
} // end of insertMoney()
/**
* give the buyer the product
*/
public void giveProduct()
{
System.out.println("*--------------*");
System.out.println("| You bought |");
System.out.println("| |");
System.out.println("| " + productType);
System.out.println("|--------------|");
numberSold ++; // increase number sold by one
} // end of giveProduct()
/**
* Give the buyer's change back after giving product
*/
public void giveChange()
{
balance -= productPrice; ///how much to give back
System.out.println("Your change is: " + balance);
balance = 0; // clear balance
} // end of giveChange()
} // end of VendingMachine class