in c# \"Create a program named ConferencesDemo for a hotel that hosts business c
ID: 3884603 • Letter: I
Question
in c# "Create a program named ConferencesDemo for a hotel that hosts business conferences. Allows a user to enter data about five Conference objects and then displays them in order of attendance from smallest to largest. The Conference class contains fields for the Conference group name, starting date (as a string), and number of attendees. Include properties for each field. Also, include an IComparable.CompareTo() method so that Conference objects can be sorted."
Farrell, Joyce. Microsoft Visual C# 2015: An Introduction to Object-Oriented Programming (Page 419).
Thank you in advance for your help.
Explanation / Answer
/*
Conference class definition with implementation
* of IComparable interface
*/
//Conference.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConferenceApplication
{
class Conference : IComparable
{
private string groupName;
private string startDate;
private int attendees;
public Conference(string groupName, string startDate, int attendees)
{
this.groupName = groupName;
this.startDate = startDate;
this.attendees = attendees;
}
//Property
public string GroupName
{
set { groupName = value; }
get { return groupName; }
}
//Property
public string StartDate
{
set { startDate = value; }
get { return startDate; }
}
//Property
public int Attendees
{
set { attendees = value; }
get { return attendees; }
}
//Override CompareTo method
public int CompareTo(Object obj)
{
Conference Temp = (Conference )obj;
if (this.attendees < Temp.attendees)
return -1;
if (this.attendees > Temp.attendees)
return 1;
else
return 0;
}
public void display()
{
Console.WriteLine(String.Format("Name : {0:s}, Date : {1:s}, #Attendance: {2:d}", groupName, startDate, attendees));
}
}
}
-----------------------------------------------------------------------------------------------
//ConferencesDemo.cs
//Test program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConferenceApplication
{
class ConferencesDemo
{
static void Main(string[] args)
{
//Create an instance of 5 Conference objects
Conference[] list = new Conference[5];
//read 5 conference objects from user
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine("Enter group name {0} :", i + 1);
string name = Console.ReadLine();
Console.WriteLine("Enter date ");
string date = Console.ReadLine();
Console.WriteLine("Enter # attandees", i + 1);
int attandance = Convert.ToInt32(Console.ReadLine());
//Create conference object
list[i] = new Conference(name, date, attandance);
}
//calling Sort method on list
Array.Sort(list);
//print sorted conference by number of attendees to console
for (int i = 0; i < list.Length; i++)
{
list[i].display();
}
Console.ReadKey();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.