I know... out of my norm, but check out my 'test.'
Here's the code I was given...
And here's what I turned in as a 'restructure.'
Pretty excited to see how it turns out.
Edit: How the interview turns out... the code works
Here's the code I was given...
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
//Sample Code
//C# Programmer Question
//Below is a class with one function that uses a pipe-delimited file that is updated once a week. The class compiles and works. In fact, it has been in production for several weeks, is used by other applications, and no one has complained. However, your manager has asked you to modify the code to make it more maintainable / readable.
//Pipe-delimited file sample:
//vaughan|life by the drop
//hendrix|red house
//lennon|let it be
//clapton|layla
//jagger|rocks off
//cobain|all apologies
//simon|me and julio down by the school yard
//morrison|
//joplin|me and bobby mcgee
using System.IO;
static class Module1 {
public static void Main() {
OutputSongFromDeadArtistsAlphabetically();
}
public static void OutputSongFromDeadArtistsAlphabetically() {
string results = null;
try {
StreamReader sr = new StreamReader(File.Open("..\\..\\input.txt", System.IO.FileMode.Open));
string y = null;
ArrayList z = new ArrayList();
string leftHalf = null;
string rightHalf = null;
int pos = 0;
while (!sr.EndOfStream) {
y = sr.ReadLine();
pos = y.IndexOf("|");
leftHalf = y.Substring(0, pos);
rightHalf = y.Substring(pos + 1, y.Length - pos - 1);
if (leftHalf == "vaughan") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
if (leftHalf == "hendrix") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
if (leftHalf == "lennon") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
if (leftHalf == "cobain") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
if (leftHalf == "joplin") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
if (leftHalf == "morrison") {
if (!string.IsNullOrEmpty(rightHalf)) {
z.Add(rightHalf);
}
}
}
//sort the list
string temp = null;
for (int i = 0; i <= z.Count - 1; i++) {
for (int j = z.Count - 1; j >= i + 1; j += -1) {
if (String.Compare(z[j].ToString(), z[j - 1].ToString(), true) < 0) {
temp = z[j].ToString();
z[j] = z[j - 1];
z[j - 1] = temp;
}
}
}
//print the sorted output
foreach (string c in z) {
Console.WriteLine(c);
}
sr.Close();
}
catch (Exception e) {
throw e;
}
}
}
And here's what I turned in as a 'restructure.'
Code:
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
static class Module1
{
public static void Main()
{
string[] Artist = { "vaughan", "hendrix", "lennon", "clapton", "jagger", "cobain", "simon", "morrison", "joplin" };
ArrayList Songs = new ArrayList();
DataTable Tbl = new DataTable();
Tbl.Columns.Add("Artist");
Tbl.Columns.Add("Song");
try
{
StreamReader SR = new StreamReader("DeadArtists.txt");
for (int i = 0; !SR.EndOfStream; i++)
{
string[] cells = SR.ReadLine().Split('|');
Tbl.Rows.Add(cells);
}
}
catch (Exception e)
{ throw e; }
for (int i = 0; i < Tbl.Rows.Count; i++)
{
object o = Tbl.Rows[i]["Artist"];
string temp = o.ToString();
if (Artist.Contains(temp) == true)
{
object s = Tbl.Rows[i]["Song"];
if (s.ToString() != "")
{
Songs.Add(s.ToString());
}
}
}
Songs.Sort();
foreach (string c in Songs)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
Pretty excited to see how it turns out.
Edit: How the interview turns out... the code works