Created
October 3, 2012 02:23
-
-
Save washingtoneg/3824565 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Java program that creates and displays a list of lists | |
*/ | |
//import java.io.InputStream; | |
import java.util.Scanner; | |
/** | |
* @author elena | |
* | |
*/ | |
public class Foo { | |
private static Scanner scanner = new Scanner( System.in ); // Scanner variable to handle user input | |
private static GList<InnerList> big_list = new GList<InnerList>(); // GList to hold InnerLists | |
private static int value_count = 0; // Count of values to insert into an GList (of an InnerList) | |
private static int list_count = 0; // Count of the number of InnerLists | |
private static String input2; // Another Scanner variable to handle user input | |
public static void main(String[] args) { | |
// Initial call to createList() method | |
createList(); | |
} | |
public static void createList() { | |
// Prompt the user | |
System.out.print( "add a list (y/n)? " ); | |
// Read a line of text from the user. | |
String input = scanner.nextLine(); | |
// Create a new list | |
if (input.compareTo("y") == 0 || input.compareTo("n") == 0) { | |
if (input.compareTo("y") == 0) { | |
System.out.print( "name of list? " ); | |
String list_name = scanner.nextLine(); | |
InnerList inner_list = new InnerList(); // Create a new InnerList | |
GList<Integer> g_list = new GList<Integer>(); // Create a new GList to associate with InnerList | |
inner_list.setName(list_name); | |
insertNumber(inner_list, g_list); // Call insertNumber(), allow user to insert numbers into list | |
} | |
// Print and exit if user doesn't want to create a new list | |
else if (input.compareTo("n") == 0) { | |
if (list_count >= 1) { | |
printList(); // Call print method | |
} | |
//Simply exit if user didn't create a list and notify user | |
System.out.println("Exiting..."); | |
System.exit(0); | |
} | |
} | |
else { | |
// Error check the input | |
System.out.println( "please type \"y\" or \"n\"" ); | |
createList(); | |
} | |
} | |
// D.R.Y method to insert values into an InnerList | |
public static void insertNumber(InnerList inner_list, GList<Integer> g_list) { | |
// Prompt the user | |
System.out.print( "add a number (y/n)? " ); | |
// Read a line of text from the user. | |
input2 = scanner.nextLine(); | |
// Insert value into (the GLists inside) InnerLists | |
if (input2.compareTo("y") == 0 || input2.compareTo("n") == 0) { | |
if (input2.compareTo("y") == 0) { | |
// Prompt the user | |
System.out.print( "value: " ); | |
Scanner v = new Scanner(System.in);//.nextLine(); | |
Integer value = Integer.parseInt(v.nextLine().trim()); | |
// If no values were previously added to g_list, use insertFirst() method to insert into g_list | |
if (value_count == 0) { | |
g_list.insertFirst(value); | |
//System.out.println(g_list.getFirstItem()); | |
inner_list.setInner(g_list); | |
value_count++; | |
} | |
// If values were previously added to g_list, use insertNext() method to insert into g_list | |
else { | |
g_list.insertNext(value); | |
inner_list.setInner(g_list); | |
value_count++; | |
} | |
// Call insertNumber() method again, ask user if they want to insert another list | |
insertNumber(inner_list, g_list); | |
} | |
else if (input2.compareTo("n") == 0) { | |
if (list_count == 0) { | |
big_list.insertFirst(inner_list); // User is done with list, commit InnerList to big list at head | |
} | |
else { | |
big_list.insertNext(inner_list); // User is done with list, commit InnerList to big list after head | |
} | |
list_count++; // increment list count variable | |
value_count = 0; | |
createList(); | |
} | |
} | |
else { | |
System.out.println( "please type \"y\" or \"n\"" ); | |
insertNumber(inner_list, g_list); | |
} | |
} | |
// Print the list(s) | |
public static void printList() { | |
// Print the list count if a list was created | |
if (list_count == 1 ) { | |
System.out.println("You created " + list_count + " list."); | |
} | |
else { | |
System.out.println("You created " + list_count + " lists."); | |
} | |
// Print the list name and values only if a list was created | |
//System.out.println(big_list.); | |
// Print the first InnerList's values if there is a first InnerList | |
if (big_list.getFirstItem() != null) { | |
System.out.println("list name: " + big_list.getFirstItem().getName()); | |
if (big_list.getFirstItem().getInner().getFirstItem() != null) { | |
System.out.println(big_list.getFirstItem().getInner().getFirstItem()); | |
} | |
while (big_list.getFirstItem().getInner().hasNextItem()){ | |
System.out.println(big_list.getFirstItem().getInner().getNextItem()); | |
} | |
while (big_list.hasNextItem()) { | |
System.out.println("list name: " + big_list.getNextItem().getName()); | |
if (big_list.getNextItem().getInner().getFirstItem() != null) { | |
System.out.println(big_list.getNextItem().getInner().getFirstItem()); | |
} | |
//System.out.println(big_list.getFirstItem().getInner().getNextItem()); | |
//if (big_list.getFirstItem().getInner().getFirstItem() !=) { | |
//System.out.println(big_list.getNextItem().getInner().getFirstItem()); | |
//while(big_list.getNextItem().getInner().hasNextItem()) { | |
// System.out.println(big_list.getNextItem().getInner().getNextItem()); | |
//} | |
} | |
/* | |
if (big_list.hasNextItem()) { | |
if (big_list.getNextItem().getInner().getNextItem() == null) { | |
System.out.println("It's null!"); | |
} | |
} | |
// Keep printing InnerList names and values if there are more | |
while (big_list.getNextItem() != null) { | |
System.out.println("list name: " + big_list.getNextItem().getName()); | |
System.out.println(big_list.getNextItem().getInner().getNextItem()); | |
while (big_list.getNextItem().getInner().hasNextItem()){ | |
System.out.println(big_list.getNextItem().getInner().getNextItem()); | |
} | |
} */ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment