

import java.util.*;


/**
 * This class is the driver (client class) for Assignment #3,
 * and it allows the user to create and list Musician objects.
 */
class a3
{
   static public void main(String[] args)
   {
      // For aquiring input
      Scanner in = new Scanner(System.in);

      // Somewhere in which to remember the musicians
      LinkedList<Musician> list = new LinkedList<Musician>();

      while(true)
      {
         // Output the command prompt
         System.out.println("Type:");
         System.out.println("   'new' to make a new musician,");
         System.out.println("   'list' to list the musicians,");
         System.out.println("   'test' to run some extra tests,");
         System.out.println("   'exit' to exit:");
         System.out.print("> ");

         String command = in.nextLine();

         // Handle the 'new' command
         if(command.equalsIgnoreCase("new"))
         {
            System.out.println("Creating a new musician");

            // get the new musician's name
            System.out.print("  Name: ");
            String name = in.nextLine();

            // get the new musician's instrument
            System.out.print("  Instrument: ");
            String instrument = in.nextLine();

            // get the new musician's office number
            System.out.print("  Office: ");
            String office = in.nextLine();

            // get the new musician's symphony section
            // we have put this in a loop so we can give the user
            // a second chance in case they make a mistake
            SymphonySection section = null;
            do
            {
               // list the section numbers and names
               SymphonySection.listSections();
               System.out.print("  Enter Section Number: ");

               // read a line of text from the user, and try to
               // interpret it as an integer - watch out for
               // a thrown exception, which indicates that the
               // user didn't type a number
               int sectionnumber = -1;
               try {
                  sectionnumber = Integer.parseInt(in.nextLine());
               } catch(Exception e) {
                  sectionnumber = -1;    // in case of bad input
               }

               // convert the number entered into a SyphonySection
               // this returns 'null' if the number is out of range
               section = SymphonySection.findSectionFromEnumeration(sectionnumber);

            } while(section == null);    // bad input? try again

            // get the new musician's Principle status
            // we have put this in a loop so we can give the user
            // a second chance in case they make a mistake
            int principle_int = -1;
            do
            {
               System.out.print("  Is this musician the Section Principle (0 = no, 1 = yes): ");

               // read a line of text from the user, and try to
               // interpret it as an integer - watch out for
               // a thrown exception, which indicates that the
               // user didn't type a number
               try {
                  principle_int = Integer.parseInt(in.nextLine());
               } catch(Exception e) {
                  principle_int = -1;    // in case of bad input
               }

               // double-check that the user entered either 0 or 1
               if(principle_int < 0 || principle_int > 1)
                  principle_int = -1;

            } while(principle_int == -1);    // bad input? try again

            // convert the 0 or 1 to a boolean value
            boolean principle = (principle_int == 1 ? true : false);

            // create the new musician object, and remember it
            Musician m = new Musician(name, instrument, office, section, principle);
            list.add(m);

            System.out.println("New musician created:");
            System.out.println(m);
         }

         // Handle the 'list' command
         if(command.equalsIgnoreCase("list"))
         {
            // list the musicians
            System.out.println("Listing the musicians:");
            for(Musician m : list)
               System.out.println("  " + m);

            // list the section counts
            System.out.println("Section Counts:");
            System.out.println("  Strings:    " + Musician.getNumberInSection(SymphonySection.Strings));
            System.out.println("  Brass:      " + Musician.getNumberInSection(SymphonySection.Brass));
            System.out.println("  WoodWinds:  " + Musician.getNumberInSection(SymphonySection.WoodWinds));
            System.out.println("  Percussion: " + Musician.getNumberInSection(SymphonySection.Percussion));
            System.out.println("  Conductor:  " + Musician.getNumberInSection(SymphonySection.Conductor));
            System.out.println("  Total:      " + Musician.getNumberOfMusicians());
         }

         // Handle the 'test' command
skip_test_command:
         if(command.equalsIgnoreCase("test"))
         {
            // we need an empty list so we don't have funny counts
            // so just in case we've been creating our own objects...
            if(list.size() != 0)
            {
               System.out.println("Please exit and restart a3.java program "
                     + "before running the tests - Thanks!");
               break skip_test_command;
            }

            // test the constructor
            System.out.print("Constructor Test: ");
            Musician m1 = new Musician("testname",
                                       "testinst",
                                       "testoffice",
                                       SymphonySection.Strings,
                                       true);
            list.add(m1);
            if(Musician.getNumberInSection(SymphonySection.Strings)    == 1
            && Musician.getNumberInSection(SymphonySection.Brass)      == 0
            && Musician.getNumberInSection(SymphonySection.WoodWinds)  == 0
            && Musician.getNumberInSection(SymphonySection.Percussion) == 0
            && Musician.getNumberInSection(SymphonySection.Conductor)  == 0
            && Musician.getNumberOfMusicians() == 1)
            {
               System.out.println("PASSED");
            }
            else
            {
               System.out.println("FAIL");
            }

            // test copy constructor
            System.out.print("Copy Constructor Test: ");
            Musician m2 = new Musician(m1);
            list.add(m2);
            if(Musician.getNumberInSection(SymphonySection.Strings)    == 2
            && Musician.getNumberInSection(SymphonySection.Brass)      == 0
            && Musician.getNumberInSection(SymphonySection.WoodWinds)  == 0
            && Musician.getNumberInSection(SymphonySection.Percussion) == 0
            && Musician.getNumberInSection(SymphonySection.Conductor)  == 0
            && Musician.getNumberOfMusicians() == 2)
            {
               System.out.println("PASSED");
            }
            else
            {
               System.out.println("FAIL");
            }

            // test setSection
            System.out.print("setSection Test (Part 1): ");
            m1.setSection(SymphonySection.Brass);
            m2.setSection(SymphonySection.WoodWinds);
            if(Musician.getNumberInSection(SymphonySection.Strings)    == 0
            && Musician.getNumberInSection(SymphonySection.Brass)      == 1
            && Musician.getNumberInSection(SymphonySection.WoodWinds)  == 1
            && Musician.getNumberInSection(SymphonySection.Percussion) == 0
            && Musician.getNumberInSection(SymphonySection.Conductor)  == 0
            && Musician.getNumberOfMusicians() == 2)
            {
               System.out.println("PASSED");
            }
            else
            {
               System.out.println("FAIL");
            }

            // test setSection - make sure last two sections work
            System.out.print("setSection Test (Part 2): ");
            // m1 should already be in the Brass section
            // m2 should already be in the WoodWinds section
            m1.setSection(SymphonySection.Percussion);
            m2.setSection(SymphonySection.Conductor);
            if(Musician.getNumberInSection(SymphonySection.Strings)    == 0
            && Musician.getNumberInSection(SymphonySection.Brass)      == 0
            && Musician.getNumberInSection(SymphonySection.WoodWinds)  == 0
            && Musician.getNumberInSection(SymphonySection.Percussion) == 1
            && Musician.getNumberInSection(SymphonySection.Conductor)  == 1
            && Musician.getNumberOfMusicians() == 2)
            {
               System.out.println("PASSED");
            }
            else
            {
               System.out.println("FAIL");
            }

            // test setSection - sneaky test this time (sorry guys...)
            System.out.print("setSection Test (Part 3): ");
            // m1 should already be in the Percussion section, so what
            // happens if we do this:
            m1.setSection(SymphonySection.Percussion);
            if(Musician.getNumberInSection(SymphonySection.Strings)    == 0
            && Musician.getNumberInSection(SymphonySection.Brass)      == 0
            && Musician.getNumberInSection(SymphonySection.WoodWinds)  == 0
            && Musician.getNumberInSection(SymphonySection.Percussion) == 1
            && Musician.getNumberInSection(SymphonySection.Conductor)  == 1
            && Musician.getNumberOfMusicians() == 2)
            {
               System.out.println("PASSED");
            }
            else
            {
               System.out.println("FAIL");
            }
         }

         // Handle the 'exit' command
         if(command.equalsIgnoreCase("exit"))
         {
            return;
         }

         System.out.println("");
      }
   }
}



