Step 1: Given a file of data, read the data and parse it based on a fixed given
ID: 3767944 • Letter: S
Question
Step 1: Given a file of data, read the data and parse it based on a fixed given field headers. To download the file, select the following link: Unit 4 Sample Data.
The file should not be a comma delimited file with the following record structure:
FirstName – Character – Size (12)
LastName – Character – Size (16)
Company – Character – Size (32)
Address – Character – Size (32)
City – Character – Size (24)
County – Character – Size (24)
State – Character – Size (2)
ZIP – Number – Size (5)
Phone – Character – Size (12)
Fax – Character – Size (12)
Email – Character – Size (32)
Web – Character – Size (42)
Step 2: Write a complete C# program in console mode to load the data file as a sequential file using C# System.IO library into an ArrayList data structure (using the System.Collection library) Store each line in the file as a separate record.
Step 3: After the file is loaded into the ArrayList, sort the data in ascending order based on the LastName field and Display the following fields:
First name
Last name
Company
Step 4: Next, sort the data in descending order based on the ZIP field, and display the following fields:
First name
Last name
Company
ZIP
Step 5: Display all the records (and all its fields) for everyone that is in the state "NY."
Step 6: Submit the source code for the solution and the output screenshots for the following list. You can use any appropriate algorithm in the solution.
Sorted data based on last name
Sorted data based on ZIP
Everyone in the state of NY
Please submit your assignment.
Explanation / Answer
Complete Program:
// File: Program.cs
using System;
using System.IO;
using System.Text;
using System.Collections;
class Person : IComparer
{
private string FirstName;
private string LastName;
private string Company;
private string Address;
private string City;
private string County;
private string State;
private int ZIP;
private string Phone;
private string Fax;
private string Email;
private string Web;
public Person(string[] fields)
{
FirstName = fields[0];
LastName = fields[1];
Company = fields[2];
Address = fields[3];
City = fields[4];
County = fields[5];
State = fields[6];
ZIP = int.Parse(fields[7]);
Phone = fields[8];
Fax = fields[9];
Email = fields[10];
Web = fields[11];
}
public string getFirstName()
{
return FirstName;
}
public string getLastName()
{
return LastName;
}
public string getCompany()
{
return Company;
}
public string getAddress()
{
return Address;
}
public string getCity()
{
return City;
}
public string getCounty()
{
return County;
}
public string getState()
{
return State;
}
public int getZIP()
{
return ZIP;
}
public string getPhone()
{
return Phone;
}
public string getFax()
{
return Fax;
}
public string getEmail()
{
return Email;
}
public string getWeb()
{
return Web;
}
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
}
public string About()
{
return string.Format("FirstName: {0} LastName: {1} Company: {2} Address: {3} City: {4} County: {5} State: {6} ZIP: {7} Phone: {8} Fax: {9} Email: {10} Web: {11} ", FirstName, LastName, Company, Address, City, County, State, ZIP, Phone, Fax, Email, Web);
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
string filename = "D:/Sreedhar/workspace/c#_workspace/Test/QA_9614861/QA_9614861/sample_data.csv";
if (!File.Exists(filename))
{
Console.WriteLine("Input file cannot be opened!");
Console.ReadKey();
return;
}
StreamReader reader = new StreamReader(filename);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] fields = line.Split(new char[] { ',' });
Person person = new Person(fields);
list.Add(person);
}
reader.Close();
foreach (Person obj in list)
{
Console.WriteLine(obj.About());
}
Console.ReadKey();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.