Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C#: I need to fix the following code and I need help fixing it. I will post what

ID: 3805088 • Letter: C

Question

C#:

I need to fix the following code and I need help fixing it. I will post what is wrong with it:

Here is what is said to be wrong with my code:

First, the program doesn't load any Patients. There are a couple of reasons for this. These particular problems are concentrated in the AddPatient method. There, you compare the ID to the length of your array. This will always be false. Instead, you must get the index into the array by modding (%) the id by the size of the array. This means that the index will be in the range of the array. The index cannot be used as the index into the array; again you must use %. Although you correctly create a SimpleLinkedList when there is not one at the index, you never add the newly instantiated list to your array. This means that you always create a new one. Nothing in the code requires you to add the Patient at the end of the linked list; just always call AddAtHead.

Your FindPatientByName method has similar problems using the patient id as an index. You need to fix this as discussed above. You also compare it against list.Length; you should not. The SimpleLinkedList method has a Find method; there is no reason for you to duplicate that code here. Just create a new patient using the first and last names and call patientList.Find. Make certain you check whether patientList is null.

I strongly encourage you to fix this program and resubmit. Do this right away so that you do not get further behind.

Your remove method doesn't remove the patient object, though it may find it. You should look at what is provided by SimpleLinkedList rather than rewriting code that is already there.

Here is the code for 5 of the classes. I will put which one's I need to fix and the ones that don't need anything done to them.

ListNode.cs:(NOTHING TO BE CHANGED IN THIS CLASS)

using System;

namespace SimpleLinkedList
{
public class ListNode<T>
{
/// <summary>
/// The field that hold the data for the node.
/// </summary>
public T Data { get; set; }

/// <summary>
/// Pointer to the next node in the list, if applicable.
/// </summary>
public ListNode<T> Next { get; set; }

/// <summary>
/// noarg constructor
/// </summary>
public ListNode(){}

/// <summary>
/// Constructor sets the Data property from its argument.
/// </summary>
/// <param name="data"></param> Data for this node.
public ListNode(T data)
{
Data = data;
}
}
}

Patient.cs:(NOTHING NEEDS TO BE DONE IN THIS CLASS)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatentData
{
public class Patient : IComparable<Patient>
{
static public int Comparisons { get; set; }

public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double ProcedureCost { get; }

public Patient(string first, string last, double cost, int id)
{
FirstName = first;
LastName = last;
ProcedureCost = cost;
Id = id;
}

public Patient(string first, string last, double cost) : this(first, last, cost, -1 )
{
Id = genPatientId(first, last);
}

public static int genPatientId(string first, string last)
{
int id = Math.Abs((last.ToUpper() + first.ToUpper()).GetHashCode())%799993;
if (id < 100000)
id += 99997;

return id;
}

public int CompareTo(Patient other)
{
Comparisons++;
return Id - other.Id;
}
}
}

PatientData.cs:(THIS IS A CLASS THAT NEEDS TO BE FIXED I BELIEVE)

using SimpleLinkedList;
using System;
using System.IO;
namespace PatentData
{
class PatientData
{
private SimpleLinkedList<Patient>[] list;

public PatientData(int size)
{
list = new SimpleLinkedList<Patient>[size];
}

public void LoadPatients(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader input = new StreamReader(fs);
while (!input.EndOfStream)
{
string line = input.ReadLine();

string[] fields = line.Split(",".ToCharArray());

Patient newPatient = CreateNewPatient(fields);

AddPatient(newPatient);
}
}

private Patient CreateNewPatient(string[] fields)
{
if (fields.Length < 3) return null;

return new Patient(fields[0], fields[1], Convert.ToDouble(fields[2]));
}

public void AddPatient(Patient patient)
{
if (patient == null || patient.Id < 0 || patient.Id >= list.Length) return;

var patientList = list[patient.Id];
if (patientList == null)
{
SimpleLinkedList<Patient> newPatient = new SimpleLinkedList<Patient>();
newPatient.AddAtHead(patient);
}
else
{
ListNode<Patient> head = patientList.GetHead();
while (head.Next != null)
{
head = head.Next;
}
ListNode<Patient> newPatient = new ListNode<Patient>(patient);
head.Next = newPatient;
}
}
public Patient FindPatientByName(string first, string last)
{
Patient pat = null;

int id = Patient.genPatientId(first, last);
if (id < 0 || id >= list.Length) return pat;

SimpleLinkedList<Patient> patientList = list[id];
ListNode<Patient> headNode = patientList.GetHead();

while (headNode != null)
{
if (headNode.Data.FirstName == first && headNode.Data.LastName == last)
{
pat = headNode.Data;
break;
}
headNode = headNode.Next;
}

return pat;
}

public int FindLongestLength()
{
int max = 0;
foreach (var listIterator in list)
{
if (listIterator == null) continue;
int count = 1;
ListNode<Patient> head = listIterator.GetHead();
while (head.Next != null)
{
count++;
head = head.Next;
}
if (count > max)
max = count;
}
return max;
}
public Patient RemovePatientById(int id)
{
Patient pat = null;
if (id < 0 || id >= list.Length) return pat;

SimpleLinkedList<Patient> patientList = list[id];
if (patientList == null) return pat;
ListNode<Patient> head = patientList.GetHead();
while (head != null)
{
if (head.Data.Id == id)
{
pat = head.Data;
break;
}
head = head.Next;
}
return pat;
}
}
}

Program.cs:(I BELIEVE THIS CLASS NEEDS TO BE FIXED)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatentData
{
class Program
{
private const int LIST_SIZE = 3;

static void showPatient(Patient p)
{
if (p == null) return;

Console.WriteLine("Patient Id : ", p.Id);
Console.WriteLine("Patient Name : ", p.FirstName + " " + p.LastName);
Console.WriteLine("Procedure Cost : ", p.ProcedureCost);
}

static void Main(string[] args)
{
PatientData data = new PatientData(LIST_SIZE);
data.LoadPatients("c:/patientList.csv");
Console.WriteLine("Enter first and last name:");
string name = Console.ReadLine();
while (!name.ToLower().StartsWith("quit"))
{
String[] names = name.Split();
if (names.Length >= 2)
{
Patient.Comparisons = 0;
Patient pat = data.FindPatientByName(names[0], names[1]);
if (pat != null)
{
showPatient(pat);
}
else
{
Console.WriteLine("Patient not found");
}
Console.WriteLine("Comparisions: {0}", Patient.Comparisons);
}
else
{
Console.WriteLine("Please provide valid First Name and Last name seperated by space");
}
Console.WriteLine("Enter a name:");
name = Console.ReadLine();
}
Console.ReadLine();
}
}
}

SimpleLinkedList.cs:(THIS IS A CLASS THAT NEEDS TO BE FIXED AS WELL)

using System;
using System.Text;
using System;
using System.Text;
namespace SimpleLinkedList
{
public class SimpleLinkedList<T> where T : IComparable<T>
{
private ListNode<T> head;
public int Count { get; private set; }

public SimpleLinkedList()
{
head = new ListNode<T>();
}

public void AddAtHead(T element)
{
ListNode<T> node = new ListNode<T>();
node.Data = element;
node.Next = head.Next;
head.Next = node;
Count++;
}

public ListNode<T> GetHead()
{
return head;
}

public T RemoveFromHead()
{
ListNode<T> node = head.Next;
T data;
if (head.Next != null)
{
head.Next = head.Next.Next;
}
Count--;
data = node.Data;
node.Data = default(T); // probably not needed since result is no longer accessible
return data;
}

private ListNode<T> FindNodeBefore(T target)
{
ListNode<T> prev = null;
prev = head;
while (prev.Next != null)
{
if (prev.Next.Data.CompareTo(target) == 0)
{
return prev;
}
prev = prev.Next;
}
// never found
return null;
}

public T Find(T target)
{
ListNode<T> result = FindNodeBefore(target);
if (result == null)
return default(T);
// advance result to the one we are looking for.
result = result.Next;
return result.Data;
}

public T Remove(T target)
{
T data = default(T);
ListNode<T> temp = null, prev = null;
temp = head;
if (temp.Data.CompareTo(target) == 0)
{
data = temp.Data;
if (temp.Next != null)
{
head = temp.Next;
}
else
{
head = null;
}
return data;
}

prev = temp;
temp = temp.Next;

while (temp != null)
{
if (temp.Data.CompareTo(target) == 0)
{
data = temp.Data;
prev.Next = temp.Next;
return data;
}
temp = temp.Next;
prev = prev.Next;
}

return data;
}
}
}

HERE IS THE ORIGINAL CODE FOR THE SIMPLELINKEDLIST.CS:

namespace SimpleLinkedList
{
public class SimpleLinkedList<T> where T : IComparable<T>
{
private ListNode<T> head;
public int Count { get; private set; }

/// <summary>
/// Constructor simply creates the sentinel node.
/// </summary>
public SimpleLinkedList()
{
head = new ListNode<T>();
}

/// <summary>
/// Adds an entry to the head of the list. A node is created with the object passed in as its data.
/// </summary>
/// <param name="element">The data to be added.</param>
public void AddAtHead(T element)
{
ListNode<T> node = new ListNode<T>();
node.Data = element;
node.Next = head.Next;
head.Next = node;
Count++;
}

/// <summary>
/// Remove the first entry in the list and return its data.
/// </summary>
/// <returns></returns>
public T RemoveFromHead()
{
ListNode<T> node = head.Next;
T data;
if (head.Next != null) {
head.Next = head.Next.Next;
}
Count--;

data = node.Data;
node.Data = default(T); // probably not needed since result is no longer accessible

return data;
}

/// <summary>
/// Walks through the list looking for the node that preceeds the node containing the target.
/// The object's CompareTo method is used to determine equality.
/// </summary>
/// <param name="target">The object to be found.</param>
/// <returns>The node prior to the node containing the target, or null if it is not found.</returns>
private ListNode<T> FindNodeBefore(T target)
{
ListNode<T> prev = null;
prev = head;
while (prev.Next != null) {
if (prev.Next.Data.CompareTo(target) == 0) {
return prev;
}
prev = prev.Next;
}

// never found
return null;
}

/// <summary>
/// Find the specified target. The object is considered 'found' if the object's CompareTo method returns zero.
/// </summary>
/// <param name="target">The object to be found.</param>
/// <returns></returns>
public T Find(T target)
{
ListNode<T> result = FindNodeBefore(target);
if (result == null)
return default(T);

// advance result to the one we are looking for.
result = result.Next;
return result.Data;
}

/// <summary>
/// Removes the specified target if the target's CompareTo method returns zero.
/// </summary>
/// <param name="target">The object to be removed.</param>
/// <returns></returns>
public T Remove(T target)
{
T data = default(T);
// TODO needed for 'A' work
return data;
}
}
}

PLEASE HELP I NEED TO GET A HIGHER GRADE ON THIS, AS SOON AS POSSIBLE!!!

Explanation / Answer

NOTE :- firstnode is the first item in linked list and lastnode is the last item in linked list.

The program is not able to load patient because the code written as follows:-

public void LoadPatients(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader input = new StreamReader(fs);
while (!input.EndOfStream)
{
string line = input.ReadLine();

string[] fields = line.Split(",".ToCharArray());

Patient newPatient = CreateNewPatient(fields); - > this line should not be included in this method as this method should not new patient.

AddPatient(newPatient);
}
}