Join 149,595 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,874 people online right now. Registration is fast and FREE... Join Now!
I need to create a DVD inventory that can do the things listed in the code. I think i need to use a linked list for the inventory list, but i don't know how linked lists work. can someone help me?
CODE
import java.util.Scanner; // import Scanner public class DVD {
public static void main(String[] args) { Scanner input = new Scanner( System.in ); // create Scanner String command; String newTitle; int newWant; int newHave; int quit;
System.out.println( "Welcome to this DVD inventory program, please enter your commands." ); System.out.println( "For a list of available commands, press H." ); command = input.nextLine();
if ( command.equals( "H" ) ) { // start H if System.out.println( "H (help), provides a summary of available commands." ); System.out.println( "L (list), list the entire inventory." ); System.out.println( "I<title> (inquire), inventory info. on a specific title." ); System.out.println( "A<title> (add), add a new title to the existing inventory." ); System.out.println( "M<title> (modify), modify the want value of a specific title." ); System.out.println( "S<title> (sell), sell an item, decrease have value of that item by 1." ); System.out.println( "D (delivery), take delivery of shipment(update have values)." ); System.out.println( "O (order), write an order for additional items so have value = want value." ); System.out.println( "R (return), return extras so have value = want value." ); System.out.println( "Q (quit), quit the program." ); } // end H if
if ( command.equals( "A" ) { System.out.println( "Please enter the title that you would like to add and the want value for that item." ); newTitle = input.nextLine(); newWant = input.nextInt();
Lists are made up of nodes, a node holds a piece of information in your case it might be a specific DVD (or a DVD object!), the node also points to the next node in the list (if singly linked list) and previous node (if doubly linked list). Java has a built in list class just so you know, they are documented in the Java API.
Lists are made up of nodes, a node holds a piece of information in your case it might be a specific DVD (or a DVD object!), the node also points to the next node in the list (if singly linked list) and previous node (if doubly linked list). Java has a built in list class just so you know, they are documented in the Java API.
Wait, so how do i use this built-in class? How do i put information in the nodes once i created it?
Thanks you, that was quite helpful, although i still need more help, i ma going to write more code now, and if i encounter more problems, i will ask again.
import java.util.List; import java.util.LinkedList; import java.util.ListIterator; import java.util.Scanner; // import Scanner public class DVD {
public static void main(String[] args) { LinkedList< String > DVD = new LinkedList< String >(); Scanner input = new Scanner( System.in ); // create Scanner String command; String newTitle; int newWant; int inquire;
do { System.out.println( "Welcome to this DVD inventory program, please enter your commands." ); System.out.println( "For a list of available commands, press H." ); command = input.nextLine();
if ( command.equals( "H" ) ) { // start H if System.out.println( "H (help), provides a summary of available commands." ); System.out.println( "L (list), list the entire inventory." ); System.out.println( "I<title> (inquire), inventory info. on a specific title." ); System.out.println( "A<title> (add), add a new title to the existing inventory." ); System.out.println( "M<title> (modify), modify the want value of a specific title." ); System.out.println( "S<title> (sell), sell an item, decrease have value of that item by 1." ); System.out.println( "D (delivery), take delivery of shipment(update have values)." ); System.out.println( "O (order), write an order for additional items so have value = want value." ); System.out.println( "R (return), return extras so have value = want value." ); System.out.println( "Q (quit), quit the program." ); } // end H if
if ( command.equals( "A" ) ) { System.out.println( "Please enter the title that you would like to add and the want value for that item." ); newTitle = input.nextLine(); DVD.addLast( newTitle );
}
if ( command.equals( "I" ) ) { System.out.println( "Please enter the title position that you would like to inquire." ); inquire = input.nextInt(); DVD.get( inquire ); }
} while ( !command.equals( "Q" ) );
} // end method main
} // end class DVD
If i use A command, everything is fine and i enter a name. If i use the I part and enters "0" for the integer inquire, instead of getting what i entered in the A command, i get this message twice: Welcome to this DVD inventory program, please enter your commands. For a list of available commands, press H. Welcome to this DVD inventory program, please enter your commands. For a list of available commands, press H.
System.out.println( "Welcome to this DVD inventory program, please enter your commands." ); System.out.println( "For a list of available commands, press H." );
inside your do while loop so every time the do while executes its going to say that...
System.out.println( "Welcome to this DVD inventory program, please enter your commands." ); System.out.println( "For a list of available commands, press H." );
inside your do while loop so every time the do while executes its going to say that...
yes, but why does it say that twice? Also, i am almost done with my entire program, just need to change one more thing. And i dont' underdstand hwo to do it.
CODE
import java.util.List; import java.util.LinkedList; import java.util.ListIterator; import java.util.Scanner; // import Scanner public class DVD {
public static void main(String[] args) { LinkedList< String > DVDName = new LinkedList< String >(); LinkedList< Integer > DVDWantValue = new LinkedList< Integer >(); LinkedList< Integer > DVDHaveValue = new LinkedList< Integer >(); LinkedList< String > CustomerName = new LinkedList< String >(); LinkedList< String > CustomerAddress = new LinkedList< String >(); LinkedList< Long > CustomerPhone = new LinkedList< Long >(); Scanner input = new Scanner( System.in ); // create Scanner String command; String newTitle; int newWant; int inquire; int newHave;
do { System.out.println(); System.out.println( "Welcome to this DVD inventory program, please enter your commands." ); System.out.println( "For a list of available commands, press H." ); command = input.nextLine();
if ( command.equalsIgnoreCase( "H" ) ) { // start H if System.out.println( "H (help), provides a summary of available commands." ); System.out.println( "L (list), list the entire inventory." ); System.out.println( "I<title> (inquire), inventory info. on a specific title." ); System.out.println( "A<title> (add), add a new title to the existing inventory." ); System.out.println( "M<title> (modify), modify the want value of a specific title." ); System.out.println( "S<title> (sell), sell an item, decrease have value of that item by 1." ); System.out.println( "D (delivery), take delivery of shipment(update have values)." ); System.out.println( "O (order), write an order for additional items so have value = want value." ); System.out.println( "R (return), return extras so have value = want value." ); System.out.println( "W (wait list), check the wait list." ); System.out.println( "Q (quit), quit the program." ); } // end H if
if ( command.equalsIgnoreCase( "A" ) ) { System.out.println( "Please enter the title that you would like to add." ); newTitle = input.nextLine(); System.out.println( "Please enter the want value of the item that you would like to add." ); newWant = input.nextInt(); System.out.println( "Please enter the have value of the item that you would like to add." ); newHave = input.nextInt(); DVDName.addLast( newTitle ); DVDWantValue.addLast( newWant ); DVDHaveValue.addLast( newHave ); System.out.println( "The item " + newTitle + " and its relevant information has been added to the list." ); System.out.println( "Name: " + newTitle ); System.out.println( "Want value: " + newWant ); System.out.println( "Have value: " + newHave );
}
if ( command.equalsIgnoreCase( "S" ) ) { System.out.println( "Please enter the title position that you would like to sell." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); if ( dvdhaveValue == 0 ) { System.out.println( "We currently do not possess any more of " + dvdName + " in store." ); System.out.println( "Please place a name on the wait list." ); String personName = input.nextLine(); System.out.println( "Please enter the customer's address." ); String personAddress = input.nextLine(); System.out.println( "Please enter the customer's phone number." ); long personPhone = input.nextLong();
if ( command.equalsIgnoreCase( "I" ) ) { System.out.println( "Please enter the title position that you would like to inquire." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "Name: " + dvdName ); System.out.println( "Want value: " + dvdwantValue ); System.out.println( "Have value: " + dvdhaveValue ); }
if ( command.equalsIgnoreCase( "M" ) ) { System.out.println( "Please enter the title position of the item that you would like to change." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "Name: " + dvdName ); System.out.println( "Want value: " + dvdwantValue ); System.out.println( "Have value: " + dvdhaveValue ); System.out.println( "Please enter the new want value of the item." ); newWant = input.nextInt(); System.out.println( "Please enter the new have value of the item." ); newHave = input.nextInt(); DVDWantValue.set( inquire, newWant ); dvdwantValue = DVDWantValue.get( inquire ); DVDHaveValue.set( inquire, newHave ); dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "The new want value of " + dvdName +" is: " + dvdwantValue ); System.out.println( "The new have value of " + dvdName +" is: " + dvdhaveValue );
}
if ( command.equalsIgnoreCase( "O" ) ) { System.out.println( "Please enter the title position that you would like to order." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "Name: " + dvdName ); System.out.println( "Want value: " + dvdwantValue ); System.out.println( "Have value: " + dvdhaveValue ); if ( (dvdhaveValue == dvdwantValue) || ( dvdhaveValue >= dvdwantValue ) ) { System.out.println( "This item is not in overdemand, no need to order it!" ); } else { int orderValue = dvdwantValue - dvdhaveValue; int theoreticalHaveValue = orderValue + dvdhaveValue; System.out.println( "An purchase order for the item " + dvdName + " has been sent." ); System.out.println( "After delievery, the have value for " + dvdName + " will be: " + theoreticalHaveValue ); }
}
if ( command.equalsIgnoreCase( "D" ) ) { System.out.println( "Please enter the title position that you would like to recieve delievery on." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "Name: " + dvdName ); System.out.println( "Want value: " + dvdwantValue ); System.out.println( "Have value: " + dvdhaveValue ); if ( (dvdhaveValue == dvdwantValue) || ( dvdhaveValue >= dvdwantValue ) ) { System.out.println( "This item was not ordered!" ); } else { dvdhaveValue = dvdwantValue; DVDHaveValue.set( inquire, dvdhaveValue); System.out.println( "This item has been ordered, the new have value will be after delievery: " + dvdhaveValue ); } }
if ( command.equalsIgnoreCase( "R" ) ) { System.out.println( "Please enter the title position that you would like to return." ); inquire = input.nextInt(); String dvdName = DVDName.get( inquire ); int dvdwantValue = DVDWantValue.get( inquire ); int dvdhaveValue = DVDHaveValue.get( inquire ); System.out.println( "Name: " + dvdName ); System.out.println( "Want value: " + dvdwantValue ); System.out.println( "Have value: " + dvdhaveValue ); if ( (dvdhaveValue == dvdwantValue) || ( dvdhaveValue <= dvdwantValue ) ) { System.out.println( "We do not possess this item in excess, no need to return any of it!" ); } else { dvdhaveValue = dvdhaveValue - dvdwantValue; DVDHaveValue.set( inquire, dvdhaveValue); System.out.println( "After return, the have value for " + dvdName + " is: " + dvdhaveValue ); }
}
if ( command.equalsIgnoreCase( "L" ) ) { System.out.println( "The entire inventory is: " ); for ( String namesAgain : DVDName ) { System.out.printf( "\n%s\n", namesAgain ); } System.out.println(); }
if ( command.equalsIgnoreCase( "W" ) ); { System.out.println( "The wait list is: " ); for ( String namescustomerAgain : CustomerName ) { System.out.printf( "\n%s\n", namescustomerAgain ); } System.out.println(); }
} while ( !command.equalsIgnoreCase( "Q" ) );
} // end method main
} // end class DVD
now i need to do one more thing, i want to make it so that instead of typing in the index position of the item everytime i need it, all i have to do is type in the content of the string. ex. instead of typing "0" to get the first element, type in "Jaws" instead.