/**
* Create class for state controller
*/
public class StateController
{
private State [] myStates;
private int numStates = 0;
private String[] searchKey = new String[14];
private int numKeys;
public int numSwap;
/**
* Creates the Controller and array of state objects
* @param maxSize
*/
public StateController(int maxSize)
{
myStates = new State[maxSize];
}//end StateController
public void loadData(String filename) throws IOException
{
FileInputStream fis1 = new FileInputStream("State.Fall2014.txt");
BufferedReader br1 = new BufferedReader (new InputStreamReader(fis1));
String name,capital,abbrev,region;
int pop,regNum;
String inputString;
inputString = br1.readLine();
System.out.printf("%15s-%15s-%5s-%10s-%20s-%5s","Name","Capital","Abbrev",
"Population","Region","Region Number");
System.out.println("==================================================================================================");
while (inputString != null)
{
name = inputString.substring(0, 15).trim();
capital=inputString.substring(15, 30).trim();
abbrev=inputString.substring(30, 32).trim();
region=inputString.substring(40, 55).trim();
pop = Integer.parseInt(inputString.substring(32,40).trim());
regNum = Integer.parseInt(inputString.substring(55,56).trim());
myStates[numStates] = new State (name, capital, abbrev, region,pop, regNum);
numStates++;
inputString = br1.readLine();
}
br1.close();
}
public void displayStates()
{
int index;
for(index = 0; index < numStates; index++)
{
System.out.println(myStates[index]);
}//end for
}//end displayStates
public void bubbleSort()
{
int out,in;
numSwap = 0;
{
for (out = numStates - 1; out>1; out--)
for (in = 0; in<out; in++)
{
if (myStates[in].getStateName().compareTo(myStates[in +1].getStateName()) < 0)
swap (in, in +1);
}// end if(swap)
}//end inner for loop
System.out.println("Array of State Objects Sorted with A Bubble Sort");
System.out.printf("%15s-%15s-%5s-%10s-%20s-%5s","Name","Capital","Abbrev",
"Population","Region","Region Number");
System.out.println("==================================================================================================");
displayStates();
}//end bubble sort
/**
* //swap method to carry out the swap
* @param one
* @param two
*/
private void swap (int one, int two)
{
State name = myStates[one];
myStates[one] = myStates[two];
myStates[two] = name;
}// end swap(int one, int two)
public void getCount()
{
System.out.println("Number of swaps Using the Bubble sort on the Array of State Objects was: "+numSwap);
}//end getCount()
/**
* reads input file
* @param filename
* @throws IOException
*/
public void loadSearchKeys(String filename) throws IOException
{
FileInputStream fis1 = new FileInputStream("State.Trans.txt");
BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
int end;
String key;
String inputString;
inputString = br1.readLine();
while(inputString !=null)
{
end = Math.min(14, inputString.length());
key = inputString.substring(0,end);
searchKey[numKeys] = key;
numKeys++;
inputString = br1.readLine();
}//end while
br1.close();
}//end load searchkeys
/**
* displays result from search keys
*/
public void displayKeys()
{
for(int index = 0; index < numKeys; index++)
{
System.out.println(searchKey[index]);
}//end for
}//end display keys
/**
* Create a sequential search that uses "State.Trans.txt" file
* @param searchKey
* @return
*/
public String seqSearch(String searchKey) {
int probe;
String answer;
for (probe = 0; probe < numStates; probe++) //for each object in array
{
if (myStates[probe].getName().compareToIgnoreCase(searchKey) == 0) //if found searchKey
{
probe++;
break; //exit before end of loop
}//end if
}//end for
if (probe == numStates) //searched all array elements?
{
probe++;
answer = (" X "); //if yes, can't find
}//end if
else {
probe++;
answer = (" X "); //if no, found searchKey
}//end else
String format = "%1$-15s %2$-20s %3$6d"; //to align under column headers
String result = String.format(format, searchKey, answer, probe);
//format result
return result;
}//end sequential search
/**
* Create Binary search
* @param searchKey
* @return
*/
public String binarySearch(String searchKey) {
int curIn;
int probe = 0, lowerBound = 0, upperBound = numStates - 1;
String format = "%1$-15s %2$-20s %3$6d";
//to align results under column headers
String answer, result;
while (true) {
curIn = (lowerBound + upperBound) / 2;//set curIn to middle of array
if (myStates[curIn].getName().compareToIgnoreCase(searchKey) == 0) //found searchKey?
{
probe++;
answer = (" X ");
result = String.format(format, searchKey, answer, probe);
//format search results
return result;
}//end if
else if (lowerBound > upperBound)//max probes reached?
{
answer = (" X ");//can't find
result = String.format(format, searchKey, answer, probe);
//format search results
return result;
}//end else if
else {
probe++;
if (myStates[curIn].getName().compareToIgnoreCase(searchKey) < 0) //does curIn come before searchKey?
{
lowerBound = curIn + 1;//check upper half
}//end if
else {
upperBound = curIn - 1;//check lower half
}//end else
}//end else
}//end while
}//end binarySearch()
}//end state controller