This question follow the ones last posted:
Open and edit your console application to use a List Class instead (called RecipientList). Update the Recipient class to include the following method: public void updateHours(int H) //update the hours left – //If a student has worked H hours in the department, the number of hours should decrease by H //It is also possible that number of hours can be increased The List Class should declare the list as an array of 10 Recipient objects, and should contain the following methods: public void Add(Recipient addNew) //adds a new recipient to the list (if list is full, an error message is displayed public int Count() //returns the number of objects in the list public void Display() //Displays all the recipients in the list public int Find(string Wanted) //returns the position of the wanted element in the list private int LinearSearch(string Wanted)
using System;
using System.Collections.Generic;
namespace RecipientApp
{
class Recipient
{
private string Name;
private int Hours;
public Recipient(string name)
{
this.Name = name;
this.Hours = 90;
}
public Recipient(string name, int рours)
{
this.Name = name;
this.Hours = рours;
}
public void setHours(int H)
{
this.Hours = H;
}
public string getName()
{
return Name;
}
public int getHours()
{
return Hours;
}
/// <summary>
/// update the hours left –If a student has worked H hours in the department,
/// the number of hours should decrease by H
/// It is also possible that number of hours can be increased
/// </summary>
/// <param name="H"></param>
public void updateHours(int H)
{
this.Hours += H;
}
}
class RecipientList
{
// The List Class should declare the list as an array of 10 Recipient objects
private List<Recipient> recipients = new List<Recipient>(10);
/// <summary>
/// adds a new recipient to the list (if list is full, an error message is displayed
/// </summary>
/// <param name="addNew"></param>
public void Add(Recipient addNew)
{
if (recipients.Count <= 10)
{
recipients.Add(addNew);
}
}
/// <summary>
/// returns the number of objects in the list
/// </summary>
/// <returns></returns>
public int Count()
{
return recipients.Count;
}
/// <summary>
/// Displays all the recipients in the list
/// </summary>
public void Display()
{
for (int i = 0; i < Count(); i++)
{
Console.WriteLine("Name: " + recipients[i].getName() + "\n" + "Hours: " + recipients[i].getHours() + "\n");
}
}
/// <summary>
/// returns the position of the wanted element in the list
/// </summary>
/// <param name="Wanted"></param>
/// <returns></returns>
public int Find(string Wanted)
{
return LinearSearch(Wanted);
}
private int LinearSearch(string Wanted)
{
for (int i = 0; i < Count(); i++)
{
if (recipients[i].getName().CompareTo(Wanted) == 0)
{
return i;
}
}
return -1;
}
}
class Program
{
static void Main(string[] args)
{
RecipientList recipientList = new RecipientList();
recipientList.Add(new Recipient("Peter"));
recipientList.Add(new Recipient("Mary",45));
recipientList.Add(new Recipient("Julia", 85));
Console.WriteLine("Size: {0}", recipientList.Count());
Console.WriteLine("All recipients:");
recipientList.Display();
int index = recipientList.Find("Mary");
if (index!=-1){
Console.WriteLine("Recipient exists");
}
Console.ReadLine();
}
}
}
Comments
Leave a comment