473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add or delete a record

105 New Member
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 2328
Laharl
849 Recognized Expert Contributor
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
4440
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 overwrite the record I want to delete with the last record in the file, then actually delete that last record in the file. the problem Im having is...
9
10617
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 table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this...
8
4042
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: DoCmd.SetWarnings False DoCmd.RunCommand acCmdDeleteRecord DoCmd.SetWarnings True End If ExitHere: Me!SubName.SetFocus
3
3959
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 delete a record in this subform. When I switched modal off and tried to delete a record from the list, I deleted a record on another form (below the...
3
7809
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 acCmdDelete Record? Thanks! Maria
4
7838
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 event routine shows that AllowDeletions is TRUE. When the Delete button is clicked (without TRACE ON), I get a 'beep', the recordselector (vertical...
5
4131
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 delete a row, only the child record is deleted from the source tables; the parent record is still there...which is what I wanted. Now display fields...
4
2555
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 the position of the record u want to delete. If you then choose to not to delete the record it looks to a user as if it has deleted every record before...
5
3906
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 particular record using the same C# code. Here is what I am using to delete the records: public void DeleteRecord(string domain, string recordName,...
10
7150
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 have a command button, called cmdDelete, that launches a dialog form, called frmDeleteCurrentRecord, where the user has to enter their name and a...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.