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

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 { ///

/// The field that hold the data for the node. ///

public T Data { get; set; }

///

/// Pointer to the next node in the list, if applicable. ///

public ListNode Next { get; set; }

///

/// noarg constructor /// public ListNode(){}

///

/// Constructor sets the Data property from its argument. /// /// 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 { 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[] list;

public PatientData(int size) { list = new SimpleLinkedList[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 newPatient = new SimpleLinkedList(); newPatient.AddAtHead(patient); } else { ListNode head = patientList.GetHead(); while (head.Next != null) { head = head.Next; } ListNode newPatient = new ListNode(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 patientList = list[id]; ListNode 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 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 patientList = list[id]; if (patientList == null) return pat; ListNode 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 where T : IComparable { private ListNode head; public int Count { get; private set; }

public SimpleLinkedList() { head = new ListNode(); }

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

public ListNode GetHead() { return head; }

public T RemoveFromHead() { ListNode 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 FindNodeBefore(T target) { ListNode 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 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 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 where T : IComparable { private ListNode head; public int Count { get; private set; }

///

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

///

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

///

/// Remove the first entry in the list and return its data. /// /// public T RemoveFromHead() { ListNode 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; }

///

/// 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. /// ///The object to be found. /// The node prior to the node containing the target, or null if it is not found. private ListNode FindNodeBefore(T target) { ListNode prev = null; prev = head; while (prev.Next != null) { if (prev.Next.Data.CompareTo(target) == 0) { return prev; } prev = prev.Next; }

// never found return null; }

///

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

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

///

/// Removes the specified target if the target's CompareTo method returns zero. /// ///The object to be removed. /// 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!!!

Here are the original Instructions for this program as well. If I put my SimpleLinkedList.cs class back to the way it was, it brings up 4 errors in the PatientData.cs class where the .GetHead is at in some of the methods, so this can't be right. Also, there is a //TODO in the original SimpleLinkedList that needs to be done without changing any of the other information.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!