/* * Computes busfares for the following categories: * Normal adult fare = $1 * Age < 3: fare is zero * Age from 4 to 13: half the adult fare * Age 65 or older: 80% of the adult fare * * Uses an enumerated type for the age categories * * Author: Don Chamberlin, Feb. 2009 */ import java.util.*; class Busfare { enum Category {INFANT, CHILD, ADULT, SENIOR}; /* * This driver program reads ages from the keyboard, * calls getFare(), and prints the age category and fare. */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter your age, or 0 to stop"); int age = scan.nextInt(); Category c; double fare; while (age > 0) { if (age < 3) c = Category.INFANT; else if (age < 14) c = Category.CHILD; else if (age > 64) c = Category.SENIOR; else c = Category.ADULT; fare = getFare(c); System.out.println("Your category is " + c); System.out.printf("Your fare is $%4.2f\n", fare); System.out.println(); System.out.println("Enter your age, or 0 to stop"); age = scan.nextInt(); } } // end of main() /* * This method returns the busfare for a given category */ static double getFare(Category c) { final double BASEFARE = 1.0; double fare = BASEFARE; switch (c) { case INFANT: fare = 0.0; break; case CHILD: fare = BASEFARE * 0.5; break; case SENIOR: fare = BASEFARE * 0.8; } return fare; } } // end of class Busfare