CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Control

Control Statements
if/else and switch
Application

The console for the Bill for a cellular telephone company application

The code for the Bill for a cellular telephone company application

import java.util.*;

public class BillCell
{
    static final double R_SERVICE_CHARGE = 10.00;
    static final int R_FREE_MINUTES = 50;
    static final double R_RATE_OVER_50 = 0.20;

    static final double P_SERVICE_CHARGE = 25.00;
    static final int P_FREE_DAY = 75;
    static final double P_DAY_RATE_OVER_75 = 0.10;

    static final int P_FREE_NIGHT = 100;
    static final double P_NIGHT_RATE_OVER_100 = 0.05;

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
        String accountNo;
        char serviceType;

        int minutesUsed;
        int minutesUsedPN;

        double amountDue;

        System.out.print("Enter the account number: ");
        accountNo = console.next();
        System.out.println();

        System.out.print("Enter the service type: (r or R) for regular " +
                         "(p or P) for premium service: ");
        serviceType = console.next().charAt(0);
        System.out.println();

        switch (serviceType)
        {
        case 'r':
        case 'R':
            System.out.print("Enter the number of minutes service used: ");
            minutesUsed = console.nextInt();
            System.out.println();

            if (minutesUsed <= R_FREE_MINUTES)
                amountDue = R_SERVICE_CHARGE;
            else
                amountDue = R_SERVICE_CHARGE +
                            (minutesUsed - R_FREE_MINUTES) * R_RATE_OVER_50;

            System.out.println("Account Number: " + accountNo);
            System.out.println("Serice Type: Regular");
            System.out.println("Minutes Service Used: " + minutesUsed);
            System.out.printf("Amount Due: $%.2f %n", amountDue);
            break;

        case 'p':
        case 'P':
            System.out.print("Enter the day time minutes used: ");
            minutesUsed = console.nextInt();
            System.out.println();

            System.out.print("Enter the night time minutes used: ");
            minutesUsedPN = console.nextInt();
            System.out.println();

            amountDue = P_SERVICE_CHARGE;

            if (minutesUsed > P_FREE_DAY)
                amountDue = amountDue +
                            (minutesUsed - P_FREE_DAY) * P_DAY_RATE_OVER_75;

            if (minutesUsedPN > P_FREE_NIGHT)
                amountDue = amountDue +
                           (minutesUsedPN - P_FREE_NIGHT) * P_NIGHT_RATE_OVER_100;

            System.out.println("Account Number: " + accountNo);
            System.out.println("Serice Type: Premium");
            System.out.println("Minutes Service Used (Day): " +  minutesUsed);
            System.out.println("Minutes Service Used (Night): "
                               + minutesUsedPN);
            System.out.printf("Amount Due: $%.2f %n", amountDue);
            break;

        default:
            System.out.println("Invalid Service Type.");
        }//end switch
    }
}
Previous | if/else and switch | if/else | switch | Application | Next