add or delete a record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmarcrum
    New Member
    • Oct 2007
    • 105

    add or delete a record

    Hello all!

    i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After execution or error message, the program puts the cursor on the next line, and waits for the next command.

    i have two commands that I CANNOT SEEM TO GET TO WORK...addDVD and deleteDVD. I just learned that I can't use an array, because i "must" have a limitless capacity for entering "new" DVD's.

    can anyone help me...i've commented out all of my "madness" coding that i've tried to get to "work."

    DVD class
    ----------------------------------------------------------------
    Code:
    /**
    * Beginning of the DVD class
    */
    public class DVD
    {
        public static final int MAX_ARRAY_SIZE = 15; // Final variable for the maximum array size
        
        public static final int REGION_FREE = 0; // Final variable for the DVD region_free code
        public static final int NTSC_FORMAT = 1; // Final variable for the DVD NTSC_format
        public static final int PAL_FORMAT = 2; // Final variable for the DVD PAL_format
        public static final int DVD_SECTOR_SIZE = 2048; // Final variable for the DVD sector size
        public static final int DATA_RATE = 4096 * 1000; // Final variable for the DVD sector size in bytes
        
        public static int serialNumberDVD = 0; // int static variable for the DVD serialNumber
       
        public static String title; // String variable for the DVD title
        public static int region; // int variable for the DVD region
        public static double length = 0.0; // double variable for the DVD length in minutes
        public static int format; // int variable for the DVD format
        private int sectorNumber = 0; // int variable for the DVD length in minutes
        private String[] dvdArray = new String[MAX_ARRAY_SIZE]; // string array for a new DVD
        private int dvdCounter = 0; // int counter variable
        
        /**
        * Constructor: builds the DVD object
        */
        public DVD(String title, int region, int format, double length)
        {
            this.title = title; // initialization of title
            this.region = region; // initialization of region
            this.format = format; // initialization of format
            this.length = length; // initialization of length
            
            serialNumberDVD = serialNumberDVD + 1; // increments the serial number
            
            for(int i = 0; i < (MAX_ARRAY_SIZE - 1); i++)// loop to initialize the entire DVD array to the null string character
                dvdArray[i] = ""; // null
        }    
        
        //Queries:-----------------------------------------
        
        /**
        * Query: getClassAuthor
        */ 
        public static String getClassAuthor() // returns name of class’ author (my name)
        {  
          return "Joseph D. Marcrum, III" + "\n"; // returns my name
        }
        
        /**
        * Query: returns the DVD sector number
        */
        public long getSectorNumber(double minutes)
        {
          long bytesToGoThrough = (long)(minutes * 60 * DATA_RATE) / 8; // converts to bits
          long sectorNumber = bytesToGoThrough / DVD_SECTOR_SIZE + 1024; // computes the sector size of the DVD
          return sectorNumber; // returns the sector number
        }
        
        /**
        * Query: returns the DVD serial number
        */
        public static int getSerialNumberDVD()
        {
          return serialNumberDVD; // returns the serial number
        }
    
        /**
        * Query: returns the DVD title
        */
        public static String getTitle()
        {
          if (region >= 0 && region <= 8)
            {
              return title;
            }
            else
            {
              return "Bad Region Code";
            }
        }
        
        /**
        * Query: returns the DVD region
        */
        public int getRegion()
        {
          return region; // returns the region
        }
        
        /**
        * Query: returns the DVD format
        */
        public int getFormat()
        {
          return format; // returns the format
        }
        
        /**
        * Query: returns the DVD length
        */
        public double getLength()
        {
          return length; // returns the length
        }
        
        //Commands:--------------------------------------------
         
        /**
         * Command: addDVD
         */
        public int addDVD(String dvd_title, int dvd_region, int dvd_format, double dvd_length)
        { // allows a DVD (title, region, format, and length) to be added to the DVD "database"
            /*dvdArray[dvdCounter] = dvd_title + ", " + dvd_region + ", " + dvd_format + "," + dvd_length; // sets up the array with the desired string
            dvdCounter++; // increment of dvdCounter
            return serialNumberDVD; // return the serialNumber of the DVD
            */
          this.title = dvd_title;
          this.region = dvd_region;
          this.format = dvd_format;
          this.length = dvd_length;
        } 
    
        /**
         * Command: deleteDVD
         */
        public int deleteDVD(int dvd_serial) //, String dvd_title, int dvd_region, int dvd_format, double dvd_length)
        { // allows a DVD to be deleted from the DVD "database"; return value must be >= 0.
            int i = 0; // counter variable for the DVD array
            int flag = 0;
            
            if(serialNumberDVD == 0) 
            { // conditional to not allow a delete if there are no DVDs
              System.out.println("You have no DVD's to delete."); // error message
            }
            else
            {
              while(dvdArray[i].equals(dvd_serial) == false)
              { // loop to check if the desired delete DVD matches any DVD's
                i++; // increment of counter
                 
                if(i > 12)
                { // conditional to check if the course has been added
                  System.out.println("You do not have that DVD to delete."); // error message
                  flag++;
                  break; // break out of loop if not
                }
              }
                
              if(i >= 0 && flag == 0)
              { // conditional to check if there are any DVD's to drop
                dvdArray[i] = ""; // sets the desired "DVD delete" to null
                while(dvdArray[i + 1].equals("") == false)
                { // loop to re-fill the array 
                  dvdArray[i] = dvdArray[i + 1]; // re-fills the array
                  i++; // increment of counter
                  dvdArray[i] = ""; // sets the replaced "DVD delete" to null
                } 
                dvdCounter--; // decrement of dvdCounter for a deleted DVD
              }
            }
            return serialNumberDVD; // return the serialNumber of the DVD
        } 
        
        /**
        * Command: toString
        */
        public String toString() // returns a text representation of all the data pertaining to a given DVD: "serial number, title, region, format, length."
        {                             
          // return of the full DVD printable object - formatted as a form
          // (properly formatted for display, and without the quotes).
          return "DVD: " + getSerialNumberDVD() + ", " + getTitle() + ", " + getRegion() + ", " + getFormat() + ", " + getLength() + "\n";
        }
    }
    ----------------------------------------------------------------

    ManageDVD class
    ----------------------------------------------------------------
    Code:
    import java.util.Scanner; // import of the Scanner class for use with system input
    
    /**
     * Beginning of the ManageDVD class
     */ 
    public class ManageDVD{
        // Main of the ManageDVD class
        public static void main (String[] args){
            String firstInput = ""; // String variable for holding user input 
            String secondInput = ""; // String variable for holding user input 
            String thirdInput = ""; // String variable for holding user input 
            String fourthInput = ""; // String variable for holding user input 
            String fifthInput = ""; // String variable for holding user input
            String firstInputLower = ""; // String variable for the user input (to lower for comparison)
            String help = ""; // String variable to hold the commands chart
            int dvd_region = 0; // int variable for DVD's region
            int dvd_format = 0; // int variable for DVD's format
            int dvd_length = 0; // int variable for DVD's length
            int dvd_serial = 0; // int variable for DVD's serialNumber
            boolean endSim = false; // end of sim flag
            
            // Creation of DVD 
            DVD newdvd = new DVD( "Shrek II", 1, DVD.NTSC_FORMAT, 105.); // creates the DVD object
            //newDVD.setDVDRegion(DVD.REGION_FREE); // sets the DVD's region
            //newDVD.setDVDFormat(DVD.NTSC_FORMAT); // sets the DVD's format
            //newDVD.setDVDLength(210.); // sets the DVD's length
           
            // command chart listing all possible commands
            help = ("<-------- Commands listing -------->\n" + 
                               "Type any of the following commands and press [Enter] to apply the desired effect(s):\n\n" + 
                               "  author : returns the author of the DVD class \n" + 
                               "  addDVD <title  region  format  length> : adds a new DVD item\n" + 
                               "            -Note: do not include the < >'s and keep the input on the same line\n" + 
                               "  deleteDVD <serialNumber> : deletes an existing DVD item based on the serialNumber\n" + 
                               "            -Note: do not include the < >'s and keep the input on the same line\n" + 
                               "  showallDVD : returns a complete list of all the DVDs\n" + 
                               "  showoneDVD <serialNumber> : returns one DVD item based on its serialNumber\n" + 
                               "            -Note: do not include the < >'s and keep the input on the same line\n" + 
                               "  help : returns a title and a list of the recognized commands for ManageDVD\n" + 
                               "  endsim : Optional command to exit the program\n");
            System.out.println(help); // print of commands for the user
                               
            
            Scanner input = new Scanner(System.in); // Scanner object creation
            
            do{ // loop on user's input
                firstInput = input.next(); // grabs the first input from the user
                firstInputLower = firstInput.toLowerCase(); // converts the user's input to all lowercase
              
                if(firstInputLower.equals("addDVD") == true){ // conditional to check if they entered addDVD
                    secondInput = input.next(); // first parameter for the addDVD
                    thirdInput = input.next(); // second parameter for the addDVD
                    fourthInput = input.next(); // third parameter for the addDVD
                    fifthInput = input.next(); // fourth parameter for the addDVD
                       
                    dvd_region = Integer.parseInt(thirdInput); // converts the region input as a string to int 
                    dvd_format = Integer.parseInt(fourthInput); // converts the format input as a string to int 
                    dvd_length = Integer.parseInt(fifthInput); // converts the length input as a string to int 
                }       
                
                if (firstInputLower.equals("deleteDVD") == true){ // conditional to check if they entered deleteDVD
                    secondInput = input.next(); // first parameter for the deleteDVD
                    
                    dvd_serial = Integer.parseInt(secondInput); // converts the serialNumber input as a string to int
                }
                
                if(firstInputLower.equals("author") == true){ // conditional to check if input is author
                    System.out.println("The author of this program is: " + newdvd.getClassAuthor()); // print for author
                }
                
                else if(firstInputLower.equals("showallDVD") == true){ // conditional to check if input is showallDVD
                    System.out.println(newdvd.toString()); // print of full DVD list
                }
                
                else if(firstInputLower.equals("showoneDVD") == true){ // conditional to check if input is showoneDVD
                    System.out.println(newdvd.getTitle()); // print of DVD title based on serialNumber
                }
                
                else if(firstInputLower.equals("addDVD") == true){ // conditional to check if input is addDVD
                    newdvd.addDVD(secondInput, dvd_region, dvd_format, dvd_length);  // adds DVD
                }
                
                else if(firstInputLower.equals("deleteDVD") == true){ // conditional to check if input is deleteDVD
                    newdvd.deleteDVD(dvd_serial); // deletes DVD
                }
                else if(firstInputLower.equals("help") == true){// conditional to check if input is help
                    System.out.println(help); // print of command listing/chart
                }
                
                else if(firstInputLower.equals("endsim") == true){// conditional to check if input is endsim
                    endSim = true; // sets the endsim to true - will break loop
                }
                
                else{ // conditional to check if input is valid
                    System.out.println("Invalid input. Please try again.");
                }
                
            } while(endSim == false); //loop until endSim is true        
        } 
    }
    ----------------------------------------------------------------

    thank you in advance for your help!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    For your dynamic allocation needs, look at ArrayList in the API.

    Comment

    Working...