Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

Checkout BlueJ Project

Click here to download a BlueJ project for Checkout.


Checkout Source Code

The Java source code for Checkout is below. Click on a file name to download it.


Checkout.java

/*
 * Example Checkout: compute a running total of a cost as item prices
 * are entered, ending with an item price of 0.  Demonstrates Java's
 * do-while construct.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: Checkout.java 2228 2013-10-24 05:42:15Z terescoj $
 */

import java.text.DecimalFormat;
import java.util.Scanner;

public class Checkout {

    public static void main(String[] args) {
        
        // set up for keyboard input
        Scanner keyboard = new Scanner(System.in);
        
        // initialize our running total
        double total = 0.0;
        
        // declare the variable that we'll use for item prices
        double itemPrice;
        
        do {
            // get next item price
            System.out.print("Enter next item price, 0 to end: ");
            itemPrice = keyboard.nextDouble();
            
            // add to running total (if positive)
            if (itemPrice > 0.0) {
                // this is a shorthand notation for:
                // total = total + itemPrice;
                total += itemPrice;
            }
        } while (itemPrice > 0.0);
        
        // print our result
        DecimalFormat currency = new DecimalFormat("$#,##0.00");
        System.out.println("Total cost: " + currency.format(total));
    }
}