import javax.swing.JOptionPane;
public class Names
{
public static void main (String [] args)
{
//declare variables to store necessary information
int size;
String inputString;
String promptString;
String sortMenu;
String searchMenu;
int choice;
boolean menu=true;
//ask user for the number of names that will be sotred
promptString = new String ("How many names do you want to enter into the program?");
inputString = JOptionPane.showInputDialog(null, promptString,
"names", JOptionPane.QUESTION_MESSAGE);
size = Integer.parseInt(inputString);
//declare variable arrays to store user input
String [] first = new String [size];
String [] last = new String [size];
StringBuffer [] bday = new StringBuffer [size];
String [] m = new String[size];
String [] d = new String[size];
String [] y = new String[size];
String [] bday2 = new String [size];
//ask user for input
for(int x = 0; x < size; x ++)
{
int adjustedX = x+1;
inputString = JOptionPane.showInputDialog(null, "Please enter the first name",
"names", JOptionPane.QUESTION_MESSAGE);
first [x] = inputString;
inputString = JOptionPane.showInputDialog(null, "Please enter the last name",
"names", JOptionPane.QUESTION_MESSAGE);
last [x] = inputString;
inputString = JOptionPane.showInputDialog(null, "Please enter the birthdate(mm/dd/yyyy)",
"names", JOptionPane.QUESTION_MESSAGE);
bday [x] = new StringBuffer(inputString);
//convert input for birthday into a string that can be compared correctly
m[x] = bday[x].substring(0,2);
d[x] = bday[x].substring(3,5);
y[x] = bday[x].substring(6,10);
bday2[x] = y[x] + "/" + m[x] + "/" + d[x];
}
//loop for the sort menu
do
{
promptString = new String ("Please select how you would like to sort your data.\n\n" +
"Please enter 1 to sort by first name.\n" +
"Enter 2 to sort by last name.\n" +
"Enter 3 to sort by birth-date.\n" +
"Enter 4 to go to the search menu.\n" +
"Enter 5 to quit the program: ");
inputString = JOptionPane.showInputDialog(null, promptString,
"sort menu", JOptionPane.QUESTION_MESSAGE);
choice = Integer.parseInt(inputString);
switch(choice)
{
//sort for first name
case 1:
{
boolean swap;
String temp;
String temp2;
StringBuffer temp3;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
int compare;
compare = first[count].compareToIgnoreCase(first[count+1]);
if (compare>0)
{
temp = (first[count]);
first[count] = (first[count + 1]);
first[count + 1] = (temp);
temp2 = (last[count]);
last[count] = (last[count + 1]);
last[count+1] = (temp2);
temp3 = bday[count];
bday[count]=bday[count+1];
bday[count+1] = temp3;
swap = true;
}
}
} while (swap);
System.out.printf("%15s%15s%15s\n", "First name", "Last name", "Birth Date");
for (int count = 0; count < (size); count++)
{
System.out.printf("%15s%15s%15s\n", first[count], last[count], bday[count]);
}
menu = true;
break;
}
//sort for last name
case 2:
{
boolean swap;
String temp;
String temp2;
StringBuffer temp3;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
int compare;
compare = last[count].compareToIgnoreCase(last[count+1]);
if (compare>0)
{
temp = (last[count]);
last[count] = (last[count + 1]);
last[count + 1] = (temp);
temp2 = (first[count]);
first[count] = (first[count + 1]);
first[count+1] = (temp2);
temp3 = bday[count];
bday[count]=bday[count+1];
bday[count+1] = temp3;
swap = true;
}
}
} while (swap);
System.out.printf("%15s%15s%15s\n", "First name", "Last name", "Birth Date");
for (int count = 0; count < (size); count++)
{
System.out.printf("%15s%15s%15s\n", first[count], last[count], bday[count]);
}
menu = true;
break;
}
//sort for birthday
case 3:
{
boolean swap;
String temp;
String temp2;
String temp3;
StringBuffer temp4;
do
{
swap = false;
for (int count = 0; count < (size-1); count++)
{
int compare;
compare = bday2[count].compareToIgnoreCase(bday2[count+1]);
if (compare>0)
{
temp = (last[count]);
last[count] = (last[count + 1]);
last[count + 1] = (temp);
temp2 = (first[count]);
first[count] = (first[count + 1]);
first[count+1] = (temp2);
temp3 = bday2[count];
bday2[count] = bday2[count+1];
bday2[count + 1] = temp3;
temp4 = bday[count];
bday[count] = bday[count+1];
bday[count+1] = temp4;
swap=true;
}
}
} while (swap);
System.out.printf("%15s%15s%15s\n", "First name", "Last name", "Birth Date");
for (int count = 0; count < (size); count++)
{
System.out.printf("%15s%15s%12s\n", first[count], last[count], bday[count]);
}
menu=true;
break;
}
//option to go to the sort menu
case 4:
{
//search menu
menu=true;
int case2;
promptString = new String ("Please enter what you would like to search for: " +
"Please enter 1 to search by first name.\n" +
"Enter 2 to search by last name.\n" +
"Enter 3 to search by birth-date.\n" +
"Enter 4 to back to the sort menu: ");
inputString = JOptionPane.showInputDialog(null, promptString,
"search menu", JOptionPane.QUESTION_MESSAGE);
case2 = Integer.parseInt(inputString);
switch(case2)
{
case 1:
{
String searchTerm = JOptionPane.showInputDialog(null, "Enter the first name you want to search for: ",
"search first name", JOptionPane.QUESTION_MESSAGE);
int compare2;
for (int z = 0; z < size; z++)
{
compare2 = first[z].compareToIgnoreCase(searchTerm);
if(compare2 == 0)
{
System.out.printf("%15s%15s%15s\n", first[z], last[z], bday[z]);
}
}
break;
}
case 2:
{
String searchTerm = JOptionPane.showInputDialog(null, "Enter the last name you want to search for: ",
"search last name", JOptionPane.QUESTION_MESSAGE);
int compare2;
for (int z = 0; z < size; z++)
{
compare2 = last[z].compareToIgnoreCase(searchTerm);
if(compare2==0)
{
System.out.printf("%15s%15s%15s\n", first[z], last[z], bday[z]);
}
}
break;
}
case 3:
{
String searchTerm = JOptionPane.showInputDialog(null, "Enter the birthdate you want to search for(mm/dd/yyyy): ",
"search birthdate", JOptionPane.QUESTION_MESSAGE);
int compare2;
String [] bday3 = new String [size];
for(int s = 0; s < size; s++)
{
bday3[s] = (m[s] + "/" + d[s] + "/" + y[s]);
}
for (int z = 0; z < size; z++)
{
compare2 = bday3[z].compareToIgnoreCase(searchTerm);
if(compare2==0)
{
System.out.printf("%15s%15s%15s\n", first[z], last[z], bday[z]);
}
}
break;
}
default:
{
break;
}
}
}
default:
{
System.out.println("Thank you for using this program");
menu = false;
break;
}
};
} while (menu);
System.exit(0);
}
}