By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

add or delete a record

100+
P: 105
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
----------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. /**
  2. * Beginning of the DVD class
  3. */
  4. public class DVD
  5. {
  6.     public static final int MAX_ARRAY_SIZE = 15; // Final variable for the maximum array size
  7.  
  8.     public static final int REGION_FREE = 0; // Final variable for the DVD region_free code
  9.     public static final int NTSC_FORMAT = 1; // Final variable for the DVD NTSC_format
  10.     public static final int PAL_FORMAT = 2; // Final variable for the DVD PAL_format
  11.     public static final int DVD_SECTOR_SIZE = 2048; // Final variable for the DVD sector size
  12.     public static final int DATA_RATE = 4096 * 1000; // Final variable for the DVD sector size in bytes
  13.  
  14.     public static int serialNumberDVD = 0; // int static variable for the DVD serialNumber
  15.  
  16.     public static String title; // String variable for the DVD title
  17.     public static int region; // int variable for the DVD region
  18.     public static double length = 0.0; // double variable for the DVD length in minutes
  19.     public static int format; // int variable for the DVD format
  20.     private int sectorNumber = 0; // int variable for the DVD length in minutes
  21.     private String[] dvdArray = new String[MAX_ARRAY_SIZE]; // string array for a new DVD
  22.     private int dvdCounter = 0; // int counter variable
  23.  
  24.     /**
  25.     * Constructor: builds the DVD object
  26.     */
  27.     public DVD(String title, int region, int format, double length)
  28.     {
  29.         this.title = title; // initialization of title
  30.         this.region = region; // initialization of region
  31.         this.format = format; // initialization of format
  32.         this.length = length; // initialization of length
  33.  
  34.         serialNumberDVD = serialNumberDVD + 1; // increments the serial number
  35.  
  36.         for(int i = 0; i < (MAX_ARRAY_SIZE - 1); i++)// loop to initialize the entire DVD array to the null string character
  37.             dvdArray[i] = ""; // null
  38.     }    
  39.  
  40.     //Queries:-----------------------------------------
  41.  
  42.     /**
  43.     * Query: getClassAuthor
  44.     */ 
  45.     public static String getClassAuthor() // returns name of class’ author (my name)
  46.     {  
  47.       return "Joseph D. Marcrum, III" + "\n"; // returns my name
  48.     }
  49.  
  50.     /**
  51.     * Query: returns the DVD sector number
  52.     */
  53.     public long getSectorNumber(double minutes)
  54.     {
  55.       long bytesToGoThrough = (long)(minutes * 60 * DATA_RATE) / 8; // converts to bits
  56.       long sectorNumber = bytesToGoThrough / DVD_SECTOR_SIZE + 1024; // computes the sector size of the DVD
  57.       return sectorNumber; // returns the sector number
  58.     }
  59.  
  60.     /**
  61.     * Query: returns the DVD serial number
  62.     */
  63.     public static int getSerialNumberDVD()
  64.     {
  65.       return serialNumberDVD; // returns the serial number
  66.     }
  67.  
  68.     /**
  69.     * Query: returns the DVD title
  70.     */
  71.     public static String getTitle()
  72.     {
  73.       if (region >= 0 && region <= 8)
  74.         {
  75.           return title;
  76.         }
  77.         else
  78.         {
  79.           return "Bad Region Code";
  80.         }
  81.     }
  82.  
  83.     /**
  84.     * Query: returns the DVD region
  85.     */
  86.     public int getRegion()
  87.     {
  88.       return region; // returns the region
  89.     }
  90.  
  91.     /**
  92.     * Query: returns the DVD format
  93.     */
  94.     public int getFormat()
  95.     {
  96.       return format; // returns the format
  97.     }
  98.  
  99.     /**
  100.     * Query: returns the DVD length
  101.     */
  102.     public double getLength()
  103.     {
  104.       return length; // returns the length
  105.     }
  106.  
  107.     //Commands:--------------------------------------------
  108.  
  109.     /**
  110.      * Command: addDVD
  111.      */
  112.     public int addDVD(String dvd_title, int dvd_region, int dvd_format, double dvd_length)
  113.     { // allows a DVD (title, region, format, and length) to be added to the DVD "database"
  114.         /*dvdArray[dvdCounter] = dvd_title + ", " + dvd_region + ", " + dvd_format + "," + dvd_length; // sets up the array with the desired string
  115.         dvdCounter++; // increment of dvdCounter
  116.         return serialNumberDVD; // return the serialNumber of the DVD
  117.         */
  118.       this.title = dvd_title;
  119.       this.region = dvd_region;
  120.       this.format = dvd_format;
  121.       this.length = dvd_length;
  122.     } 
  123.  
  124.     /**
  125.      * Command: deleteDVD
  126.      */
  127.     public int deleteDVD(int dvd_serial) //, String dvd_title, int dvd_region, int dvd_format, double dvd_length)
  128.     { // allows a DVD to be deleted from the DVD "database"; return value must be >= 0.
  129.         int i = 0; // counter variable for the DVD array
  130.         int flag = 0;
  131.  
  132.         if(serialNumberDVD == 0) 
  133.         { // conditional to not allow a delete if there are no DVDs
  134.           System.out.println("You have no DVD's to delete."); // error message
  135.         }
  136.         else
  137.         {
  138.           while(dvdArray[i].equals(dvd_serial) == false)
  139.           { // loop to check if the desired delete DVD matches any DVD's
  140.             i++; // increment of counter
  141.  
  142.             if(i > 12)
  143.             { // conditional to check if the course has been added
  144.               System.out.println("You do not have that DVD to delete."); // error message
  145.               flag++;
  146.               break; // break out of loop if not
  147.             }
  148.           }
  149.  
  150.           if(i >= 0 && flag == 0)
  151.           { // conditional to check if there are any DVD's to drop
  152.             dvdArray[i] = ""; // sets the desired "DVD delete" to null
  153.             while(dvdArray[i + 1].equals("") == false)
  154.             { // loop to re-fill the array 
  155.               dvdArray[i] = dvdArray[i + 1]; // re-fills the array
  156.               i++; // increment of counter
  157.               dvdArray[i] = ""; // sets the replaced "DVD delete" to null
  158.             } 
  159.             dvdCounter--; // decrement of dvdCounter for a deleted DVD
  160.           }
  161.         }
  162.         return serialNumberDVD; // return the serialNumber of the DVD
  163.     } 
  164.  
  165.     /**
  166.     * Command: toString
  167.     */
  168.     public String toString() // returns a text representation of all the data pertaining to a given DVD: "serial number, title, region, format, length."
  169.     {                             
  170.       // return of the full DVD printable object - formatted as a form
  171.       // (properly formatted for display, and without the quotes).
  172.       return "DVD: " + getSerialNumberDVD() + ", " + getTitle() + ", " + getRegion() + ", " + getFormat() + ", " + getLength() + "\n";
  173.     }
  174. }
----------------------------------------------------------------

ManageDVD class
----------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; // import of the Scanner class for use with system input
  2.  
  3. /**
  4.  * Beginning of the ManageDVD class
  5.  */ 
  6. public class ManageDVD{
  7.     // Main of the ManageDVD class
  8.     public static void main (String[] args){
  9.         String firstInput = ""; // String variable for holding user input 
  10.         String secondInput = ""; // String variable for holding user input 
  11.         String thirdInput = ""; // String variable for holding user input 
  12.         String fourthInput = ""; // String variable for holding user input 
  13.         String fifthInput = ""; // String variable for holding user input
  14.         String firstInputLower = ""; // String variable for the user input (to lower for comparison)
  15.         String help = ""; // String variable to hold the commands chart
  16.         int dvd_region = 0; // int variable for DVD's region
  17.         int dvd_format = 0; // int variable for DVD's format
  18.         int dvd_length = 0; // int variable for DVD's length
  19.         int dvd_serial = 0; // int variable for DVD's serialNumber
  20.         boolean endSim = false; // end of sim flag
  21.  
  22.         // Creation of DVD 
  23.         DVD newdvd = new DVD( "Shrek II", 1, DVD.NTSC_FORMAT, 105.); // creates the DVD object
  24.         //newDVD.setDVDRegion(DVD.REGION_FREE); // sets the DVD's region
  25.         //newDVD.setDVDFormat(DVD.NTSC_FORMAT); // sets the DVD's format
  26.         //newDVD.setDVDLength(210.); // sets the DVD's length
  27.  
  28.         // command chart listing all possible commands
  29.         help = ("<-------- Commands listing -------->\n" + 
  30.                            "Type any of the following commands and press [Enter] to apply the desired effect(s):\n\n" + 
  31.                            "  author : returns the author of the DVD class \n" + 
  32.                            "  addDVD <title  region  format  length> : adds a new DVD item\n" + 
  33.                            "            -Note: do not include the < >'s and keep the input on the same line\n" + 
  34.                            "  deleteDVD <serialNumber> : deletes an existing DVD item based on the serialNumber\n" + 
  35.                            "            -Note: do not include the < >'s and keep the input on the same line\n" + 
  36.                            "  showallDVD : returns a complete list of all the DVDs\n" + 
  37.                            "  showoneDVD <serialNumber> : returns one DVD item based on its serialNumber\n" + 
  38.                            "            -Note: do not include the < >'s and keep the input on the same line\n" + 
  39.                            "  help : returns a title and a list of the recognized commands for ManageDVD\n" + 
  40.                            "  endsim : Optional command to exit the program\n");
  41.         System.out.println(help); // print of commands for the user
  42.  
  43.  
  44.         Scanner input = new Scanner(System.in); // Scanner object creation
  45.  
  46.         do{ // loop on user's input
  47.             firstInput = input.next(); // grabs the first input from the user
  48.             firstInputLower = firstInput.toLowerCase(); // converts the user's input to all lowercase
  49.  
  50.             if(firstInputLower.equals("addDVD") == true){ // conditional to check if they entered addDVD
  51.                 secondInput = input.next(); // first parameter for the addDVD
  52.                 thirdInput = input.next(); // second parameter for the addDVD
  53.                 fourthInput = input.next(); // third parameter for the addDVD
  54.                 fifthInput = input.next(); // fourth parameter for the addDVD
  55.  
  56.                 dvd_region = Integer.parseInt(thirdInput); // converts the region input as a string to int 
  57.                 dvd_format = Integer.parseInt(fourthInput); // converts the format input as a string to int 
  58.                 dvd_length = Integer.parseInt(fifthInput); // converts the length input as a string to int 
  59.             }       
  60.  
  61.             if (firstInputLower.equals("deleteDVD") == true){ // conditional to check if they entered deleteDVD
  62.                 secondInput = input.next(); // first parameter for the deleteDVD
  63.  
  64.                 dvd_serial = Integer.parseInt(secondInput); // converts the serialNumber input as a string to int
  65.             }
  66.  
  67.             if(firstInputLower.equals("author") == true){ // conditional to check if input is author
  68.                 System.out.println("The author of this program is: " + newdvd.getClassAuthor()); // print for author
  69.             }
  70.  
  71.             else if(firstInputLower.equals("showallDVD") == true){ // conditional to check if input is showallDVD
  72.                 System.out.println(newdvd.toString()); // print of full DVD list
  73.             }
  74.  
  75.             else if(firstInputLower.equals("showoneDVD") == true){ // conditional to check if input is showoneDVD
  76.                 System.out.println(newdvd.getTitle()); // print of DVD title based on serialNumber
  77.             }
  78.  
  79.             else if(firstInputLower.equals("addDVD") == true){ // conditional to check if input is addDVD
  80.                 newdvd.addDVD(secondInput, dvd_region, dvd_format, dvd_length);  // adds DVD
  81.             }
  82.  
  83.             else if(firstInputLower.equals("deleteDVD") == true){ // conditional to check if input is deleteDVD
  84.                 newdvd.deleteDVD(dvd_serial); // deletes DVD
  85.             }
  86.             else if(firstInputLower.equals("help") == true){// conditional to check if input is help
  87.                 System.out.println(help); // print of command listing/chart
  88.             }
  89.  
  90.             else if(firstInputLower.equals("endsim") == true){// conditional to check if input is endsim
  91.                 endSim = true; // sets the endsim to true - will break loop
  92.             }
  93.  
  94.             else{ // conditional to check if input is valid
  95.                 System.out.println("Invalid input. Please try again.");
  96.             }
  97.  
  98.         } while(endSim == false); //loop until endSim is true        
  99.     } 
  100. }
----------------------------------------------------------------

thank you in advance for your help!
Feb 16 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 849
For your dynamic allocation needs, look at ArrayList in the API.
Feb 16 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.