473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

add or delete a record

105 100+
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
1 2316
Laharl
849 Expert 512MB
For your dynamic allocation needs, look at ArrayList in the API.
Feb 16 '08 #2

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

Similar topics

1
by: Someonekicked | last post by:
I have a binary file, and I need to delete a specific number of characters in it. The file contains records, each record has a specific length. So the way I wanna handle deleting is that I will...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
3
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to...
3
by: Maria | last post by:
Is there another way to delete the current record in a subform from the main form, another subform or a sub-subform other than setting focus on a field in the subform and using run command...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
5
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I...
4
by: Craggy | last post by:
Hi, I am trying to pop up a yes/no message box so that a user can delete a record in a continuous form. The default delete message is a bit sloppy because it seems to move the continuous form to...
5
by: sh26 | last post by:
I can Add and Delete (A, CName, MX and TXT) Dns records on the Dns Server using C# code. The problem I am having is if someone manaully creates a TXT record on the Dns Server, I cannot delete that...
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form with a (continuous) subform, and on the subform I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.