CDTestDriver

/**

 * Simple test driver for the CD class

 * from ISE 208 Homework 1.

 * 

 * @author Michael S. Tashbook

 * @version 1.0

 */

import java.util.*; // Needed for Scanner

public class CDTestDriver

{

    private static CD disc = null;

    private static String title, artist, genre;

    private static int year, tracks, length;

    

    private static Scanner sc = new Scanner(System.in);

    

    public static void main (String [] args)

    {

        int menuOption = 0;

        

        do

        {

            menuOption = showMenu();

            

            handleSelection (menuOption);

        } while (menuOption != 0);

    }

    

    private static int showMenu ()

    {

        System.out.println();

        System.out.println("1. Create a new CD");

        

        if (disc != null)

        {

            System.out.println("2. Set the genre for the current CD");

            System.out.println("3. Get the number of tracks for this CD");

            System.out.println("4. Get the total length of this CD in seconds");

            System.out.println("5. Get the (formatted) length of this CD");

            System.out.println("6. Add a new track to this CD");

            System.out.println("7. Display a summary of the CD's information");

        }

        

        System.out.println();

        System.out.println("0. Exit the program");

        System.out.println("\n");

        

        System.out.print("Enter your choice: ");

        int choice = sc.nextInt();

        sc.nextLine();

        System.out.println("\n");

        

        return choice;

    }

    

    private static void handleSelection (int choice)

    {

        if (choice == 1)

        {

            title = artist = null;

            genre = "";

            year = length = tracks = 0;

            System.out.print("Enter CD title: ");

            title = sc.nextLine();

            System.out.print("Enter CD artist: ");

            artist = sc.nextLine();

            System.out.print("Enter year of publication: ");

            year = sc.nextInt();

            sc.nextLine();

            

            disc = new CD(title, artist, year);

        }

        else if (disc != null)

        {

            if (choice == 2)

            {

                System.out.print("Enter CD genre: ");

                genre = sc.nextLine();

                disc.setGenre(genre);

            }

            else if (choice == 3)

            {

                System.out.println("Number of tracks: " + disc.getNumberOfTracks() + "(" + tracks + " expected)");

            }

            else if (choice == 4)

            {

                System.out.println("CD duration: " + disc.lengthInSeconds() + " second(s) (" + length + " expected)");

            }

            else if (choice == 5)

            {

                System.out.println("CD duration (MM:SS): " + disc.totalLength() + " (total of " + length + " second(s) expected)");

            }

            else if (choice == 6)

            {

                System.out.print("Enter track length in seconds: ");

                int len = sc.nextInt();

                sc.nextLine();

                

                // Update actual value

                disc.addTrack(len);

                

                // Update expected values

                length = len + 2;

                tracks = tracks + 1;

            }

            else if (choice == 7)

            {

                printExpectedValues();

                

                // Print actual values

                disc.printInfo();

            }

        }

        else

        {

            System.out.println("You must create a CD first!");

        }

    }

    

    private static void printExpectedValues ()

    {

        System.out.println("Expected CD field values");

        System.out.println("------------------------");

        System.out.println("Title: " + title);

        System.out.println("Artist: " + artist);

        System.out.println("Year: " + year);

        System.out.println("Genre: " + genre);

        System.out.println("Tracks: " + tracks);

        System.out.println("Length: " + length + " second(s)");

        System.out.println();

    }

}

This page was last modified on 8/25/09