- #1
iamjon.smith
- 117
- 3
You have been tasked to develop an application that reads customer information from a file to determine in which states you have customers.
The file contains records with customer first name, customer last name, street address, city, and state.
The file could contain many customers living in the same state. You need to read all of the records from the file and store only the state information into a data structure without duplicates. The states need to be sorted alphabetically.
Main Class:
and my CustRecord Class:
All I have to do now is to take the state from the CustRecord constructor and place it into a collection. My instructor has STRONGLY recommended using a tree set since it contains no duplicates by default. I also need the states to be sorted alphabetically. There is no requirements as to output so I just intend to output the alphabetized collection to a generic output file (output.txt). I am a little confused as to how to build the collection and get the data from my constructor to the collection.
The file contains records with customer first name, customer last name, street address, city, and state.
The file could contain many customers living in the same state. You need to read all of the records from the file and store only the state information into a data structure without duplicates. The states need to be sorted alphabetically.
Main Class:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author Jonathan Smith
*/
public class CustomerRecordTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = null;
String custFirst = "";
String custLast = "";
int streetNum = 0;
String streetName = "";
String streetType = "";
String City = "";
String State = "";
CustRecord customer = new CustRecord( custFirst, custLast, streetNum, streetName, streetType, City, State);
try{
scanner = new Scanner(new File("CustomerRecord.txt")); // Scanner construct that reads the input file
while (scanner.hasNextLine())
{ //Checks if there's any more lines
custFirst = scanner.next(); // reads to first space in line, sets the First Name
custLast = scanner.next(); // reads to second space in line, sets the Last Name
streetNum = scanner.nextInt(); // reads to the next space in line, sets Street Number
streetName = scanner.next(); // reads to next space sets street Name
streetType = scanner.next(); // reads to next space sets the street type (rd, circle, blvd, etc.)
City = scanner.next(); // reads to next space sets the City
State = scanner.next(); // reads to end of line sets the State
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
}
}
and my CustRecord Class:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jon and Jessica
*/
public class CustRecord {
private String custFirst;
private String custLast;
private int streetNum;
private String streetName;
private String streetType;
private String City;
private String State;
// no-argument constructor calls other constructor with default values
public CustRecord()
{
this( "", "", 0, "","", "","" ); // call six-argument constructor
} // end no-argument CityRecord constructor
// initialize a record
public CustRecord( String custFirst, String custLast, int streetNum, String streetName, String streetType, String City, String State )
{
setCustFirst( custFirst );
setCustLast( custLast );
setStreetNum( streetNum );
setStreetName( streetName );
setCity( City );
setState( State );
} // end 6-argument CityRecord constructor
public String getStreetType() {
return streetType;
}
public void setStreetType(String streetType) {
this.streetType = streetType;
}
public String getCustFirst() {
return custFirst;
}
public void setCustFirst(String custFirst) {
this.custFirst = custFirst;
}
public String getCustLast() {
return custLast;
}
public void setCustLast(String custLast) {
this.custLast = custLast;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public int getStreetNum() {
return streetNum;
}
public void setStreetNum(int streetNum) {
this.streetNum = streetNum;
}
// set city name
public void setCity( String cityName )
{
City = cityName;
} // end method setCity
// get city name
public String getCity()
{
return City;
} // end method getCity
// set state name
public void setState( String stateName )
{
State = stateName;
} // end method setState
// get state name
public String getState()
{
return State;
} // end method getState
} // end class CityRecord
All I have to do now is to take the state from the CustRecord constructor and place it into a collection. My instructor has STRONGLY recommended using a tree set since it contains no duplicates by default. I also need the states to be sorted alphabetically. There is no requirements as to output so I just intend to output the alphabetized collection to a generic output file (output.txt). I am a little confused as to how to build the collection and get the data from my constructor to the collection.