Below is a working program, can anyone help me to sort according to the Bus number.
Btw, do I have to apply Array.Sort ? Pls advise, thanks.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Bus
{
int busNo;
string startDepot, endDepot;
float departTime;
public void Update()
{
Console.Write("Entert Bus No.:");
busNo = int.Parse(Console.ReadLine());
Console.Write("Enter Start depot: ");
startDepot = Console.ReadLine();
Console.Write("Enter End depot: ");
endDepot = Console.ReadLine();
Console.Write("Enter depart time: ");
departTime = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Bus No. is " + busNo);
Console.WriteLine("Start depot is " + startDepot);
Console.WriteLine("End depot is " + endDepot);
Console.WriteLine("Depart time is " + departTime);
}
}
class Program
{
static void Main(string[] args)
{
char choice;
Bus[] group1 = null;
int num = 0;
do
{
Console.WriteLine("a) Update b) Display e) Exit");
Console.Write("Enter choice: ");
choice = char.Parse(Console.ReadLine());
switch (choice)
{
case 'a':
Console.WriteLine("Enter Number of Bus");
num = int.Parse(Console.ReadLine());
group1 = new Bus[num]; //dynamic sizing
for (int row = 0; row < num; row++)
{
group1[row] = new Bus();
group1[row].Update();
}
break;
case 'b':
Console.WriteLine("Display all Bus(es)");
for (int row = 0; row < num; row++)
{
//group1[row] = new Bus();
group1[row].Display(); //stuck
}
break;
default: Console.WriteLine("No such choice"); break;
}
} while (choice != 'e');
}
}
}.
AlanPosted Jan 7, 2008, 7:54 PM
Sorry, Keith, but you've lost me there.
I just tried the DisplayPrevious() method which I put in and it worked fine, displaying what the Bus fields were before the current update.
KeithPosted Jan 6, 2008, 3:18 PM
Thanks Alan, there's no bug.
But why can't I display the previous updates ?
The purpose of storing previous updates is actually to display current and previous updates. I tried modifying your codes but itdoesn't seem to work, can you help, thanks.
AlanPosted Jan 6, 2008, 2:41 PM
You'll need to introduce some more fields into the Bus class to store the previous values. Here's some code (written very quickly so watch out for possible bugs!):
using System;
using System.Collections; // namespace for IComparer interface
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 busNos = new List(); // to hold existing bus numbers
{
class Bus
{
int busNo;
static List
string startDepot, endDepot;
float departTime;
// fields to hold values for previous updates
int prevBusNo = -1;
string prevStartDepot, prevEndDepot;
float prevDepartTime;
// flag to record whether info updated more than once
bool updatedTwice = false;
public void Update()
{
// save current values to 'prev' fields
prevBusNo = busNo;
prevStartDepot = startDepot;
prevEndDepot = endDepot;
prevDepartTime = departTime;
if (!updatedTwice) updatedTwice = true;
// now get new values
bool uniqueBusNumber = false; // flag to check whether bus number unique
while(!uniqueBusNumber)
{
Console.Write("Enter Bus number: ");
busNo = int.Parse(Console.ReadLine());
// it's not a problem if busNo is same as prevBusNo
// IndexOf returns -1 if busNo not in list
if (busNos.IndexOf(busNo) > -1 && busNo != prevBusNo)
{
Console.WriteLine("Bus number already used, enter another one");
}
else
{
uniqueBusNumber = true;
// only add bus number to list if different to prevBusNo
if (busNo != prevBusNo)
{
busNos.Add(busNo); // add new bus number to list
busNos.Remove(prevBusNo); // remove previous one
}
}
}
Console.Write("Enter Start depot: ");
startDepot = Console.ReadLine();
Console.Write("Enter End depot: ");
endDepot = Console.ReadLine();
Console.Write("Enter depart time: ");
departTime = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Bus No. is " + busNo);
Console.WriteLine("Start depot is " + startDepot);
Console.WriteLine("End depot is " + endDepot);
Console.WriteLine("Depart time is " + departTime);
}
// insert a new method to display prev values
public void DisplayPrevious()
{
if (updatedTwice)
{
Console.WriteLine("Previous Bus No. is " + prevBusNo);
Console.WriteLine("Previous Start depot is " + prevStartDepot);
Console.WriteLine("Previous End depot is " + prevEndDepot);
Console.WriteLine("Previous Depart time is " + prevDepartTime);
}
else
{
Console.WriteLine("Bus info has only been updated once");
}
}
// insert a nested class which implements IComparer
internal class BusComparer : IComparer
{
// this is only method that needs implementing
// and is used to sort buses by their numbers (ascending)
public int Compare(object obj1, object obj2)
{
Bus bus1 = (Bus)obj1;
Bus bus2 = (Bus)obj2;
return bus1.busNo.CompareTo(bus2.busNo);
}
}
}
class Program
{
static void Main(string[] args)
{
char choice;
Bus[] group1 = null;
int num = 0;
do
{
Console.WriteLine("a) Update b) Display e) Exit");
Console.Write("Enter choice: ");
choice = char.Parse(Console.ReadLine());
switch (choice)
{
case 'a':
Console.WriteLine("Enter Number of Buses"); // changed text here
num = int.Parse(Console.ReadLine());
group1 = new Bus[num]; //dynamic sizing
for (int row = 0; row < num; row++)
{
group1[row] = new Bus();
group1[row].Update();
}
break;
case 'b':
Console.WriteLine("Display all Bus(es)");
// sort group1 based on our nested class
Array.Sort(group1, new Bus.BusComparer());
for (int row = 0; row < num; row++)
{
//group1[row] = new Bus();
group1[row].Display();
}
break;
// insert new case here to stop it saying no such case
case 'e':
break;
default: Console.WriteLine("No such choice"); break;
}
} while (choice != 'e');
// update values in the group1[0] bus object
Console.WriteLine("\nUpdate values for first bus :");
group1[0].Update();
// now display them
group1[0].Display();
// now display previous values
group1[0].DisplayPrevious();
}
}
}
KeithPosted Jan 6, 2008, 12:13 PM
Thanks Alan, your code is great.
Based on the above code, I realised that whenever I do a 2nd update, the information in 1st update will be erased. Is there any way to keep the 1st update ? I want the program to keep the previous update whenever there's a new update. Can you help ? Thanks in advance.
AlanPosted Jan 5, 2008, 8:25 AM
What I'd do is to give the Bus class a static field of type List to record the bus numbers which have been used for Bus objects created so far. You can then make sure that a new bus number is unique by checking it against this List. The input of the bus number needs to be placed in a loop so that you can ask the user to re-enter if in fact a number has been used before. Something like the following:
using System;
using System.Collections; // namespace for IComparer interface
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 busNos = new List(); // to hold existing bus numbers
{
class Bus
{
int busNo;
static List
string startDepot, endDepot;
float departTime;
public void Update()
{
bool uniqueBusNumber = false; // flag to check whether bus number unique
while(!uniqueBusNumber)
{
Console.Write("Enter Bus number: ");
busNo = int.Parse(Console.ReadLine());
if (busNos.IndexOf(busNo) > -1 ) // IndexOf returns -1 if busNo not in list
{
Console.WriteLine("Bus number already used, enter another one");
}
else
{
uniqueBusNumber = true; // stops loop from interating
busNos.Add(busNo); // add new bus number to list
}
}
Console.Write("Enter Start depot: ");
startDepot = Console.ReadLine();
Console.Write("Enter End depot: ");
endDepot = Console.ReadLine();
Console.Write("Enter depart time: ");
departTime = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Bus No. is " + busNo);
Console.WriteLine("Start depot is " + startDepot);
Console.WriteLine("End depot is " + endDepot);
Console.WriteLine("Depart time is " + departTime);
}
// insert a nested class which implements IComparer
internal class BusComparer : IComparer
{
// this is only method that needs implementing
// and is used to sort buses by their numbers (ascending)
public int Compare(object obj1, object obj2)
{
Bus bus1 = (Bus)obj1;
Bus bus2 = (Bus)obj2;
return bus1.busNo.CompareTo(bus2.busNo);
}
}
}
class Program
{
static void Main(string[] args)
{
char choice;
Bus[] group1 = null;
int num = 0;
do
{
Console.WriteLine("a) Update b) Display e) Exit");
Console.Write("Enter choice: ");
choice = char.Parse(Console.ReadLine());
switch (choice)
{
case 'a':
Console.WriteLine("Enter Number of Buses"); // changed text here
num = int.Parse(Console.ReadLine());
group1 = new Bus[num]; //dynamic sizing
for (int row = 0; row < num; row++)
{
group1[row] = new Bus();
group1[row].Update();
}
break;
case 'b':
Console.WriteLine("Display all Bus(es)");
// sort group1 based on our nested class
Array.Sort(group1, new Bus.BusComparer());
for (int row = 0; row < num; row++)
{
//group1[row] = new Bus();
group1[row].Display();
}
break;
// insert new case here to stop it saying no such case
case 'e':
break;
default: Console.WriteLine("No such choice"); break;
}
} while (choice != 'e');
}
}
}
KeithPosted Jan 3, 2008, 1:15 PM
KeithPosted Jan 1, 2008, 2:02 PM
AlanPosted Dec 31, 2007, 7:17 AM
To sort the Bus array by bus number, you need to add some functionality to the Bus class, namely a nested class BusComparer which implements the IComparer interface.
Here's the revised program. I've taken the opportunity to correct or clarify a couple of small points I noticed when I tried to run it myself:
using System;
using System.Collections; // add namespace for IComparer interface
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Bus
{
int busNo;
string startDepot, endDepot;
float departTime;
public void Update()
{
Console.Write("Enter Bus number: "); // changed text for this line
busNo = int.Parse(Console.ReadLine());
Console.Write("Enter Start depot: ");
startDepot = Console.ReadLine();
Console.Write("Enter End depot: ");
endDepot = Console.ReadLine();
Console.Write("Enter depart time: ");
departTime = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Bus No. is " + busNo);
Console.WriteLine("Start depot is " + startDepot);
Console.WriteLine("End depot is " + endDepot);
Console.WriteLine("Depart time is " + departTime);
}
// insert a nested class which implements IComparer
internal class BusComparer : IComparer
{
// this is the only method that needs implementing
// and is used to sort buses by their numbers (ascending)
public int Compare(object obj1, object obj2)
{
Bus bus1 = (Bus)obj1;
Bus bus2 = (Bus)obj2;
return bus1.busNo.CompareTo(bus2.busNo);
}
}
}
class Program
{
static void Main(string[] args)
{
char choice;
Bus[] group1 = null;
int num = 0;
do
{
Console.WriteLine("a) Update b) Display e) Exit");
Console.Write("Enter choice: ");
choice = char.Parse(Console.ReadLine());
switch (choice)
{
case 'a':
Console.WriteLine("Enter Number of Buses"); // changed text here
num = int.Parse(Console.ReadLine());
group1 = new Bus[num]; //dynamic sizing
for (int row = 0; row < num; row++)
{
group1[row] = new Bus();
group1[row].Update();
}
break;
case 'b':
Console.WriteLine("Display all Bus(es)");
// sort group1 based on our nested class
Array.Sort(group1, new Bus.BusComparer());
for (int row = 0; row < num; row++)
{
//group1[row] = new Bus();
group1[row].Display();
}
break;
// insert new case here to stop it saying no such choice when exit pressed
case 'e':
break;
default: Console.WriteLine("No such choice"); break;
}
} while (choice != 'e');
}
}
}