Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Checkout BlueJ Project

Click here to download a BlueJ project for Checkout.


Download file: Checkout.vls


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 1937 2012-10-08 18:06:13Z 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));
    }
}