Computer Science 523
Advanced Programming
Summer 2014, 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.
/*
* 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 2366 2014-05-20 02:33:22Z 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));
}
}