30/03/2010
Sorting Exam Practice
namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input increment");
string j = Console.ReadLine();
double inc = double.Parse(j);
Employee.SetIncrement(inc);
ArrayList al = new ArrayList();
int n=0;
do
{
Console.WriteLine("Please input EmployeeID");
string k = Console.ReadLine();
int id = int.Parse(k);
Console.WriteLine("Please input Employee Name");
string name = Console.ReadLine();
Console.WriteLine("Please input Salary");
string l = Console.ReadLine();
double salary = double.Parse(l);
al.Add(new Employee(id, name, salary));
Console.WriteLine("Do you want to enter another Employee? Yes-1 No-0");
string m = Console.ReadLine();
n = int.Parse(m);
}
while (n == 1);
al.Sort(new Sorting());
try
{
FileStream fs = new FileStream
(
@"E:\VirtualExpander_J\C#\Fileaccess\Employeesort.txt",
FileMode.Create,
FileAccess.ReadWrite,
FileShare.Read
);
StreamWriter sw = new StreamWriter(fs);
foreach (Employee m in al)
{
Console.WriteLine(m.GetEID() + " " + m.GetEName() + " " + m.GetNewSalary());
sw.WriteLine(m.GetEID() + " " + m.GetEName() + " " + m.GetNewSalary());
sw.Flush();
}
sw.Close();
fs.Close();
}
catch
{
Console.WriteLine("Try block error");
}
}
}
class Employee
{
int EID;
string EName;
double Salary;
static double Increment=0;
static public void SetIncrement(double d)
{
Increment = d;
}
public int GetEID()
{
return EID;
}
public string GetEName()
{
return EName;
}
public double GetNewSalary()
{
Salary = Salary * (1 + Increment);
return Salary;
//double NewSalary = Salary + (Salary * Increment);
//return NewSalary;
}
public Employee(int id, string name, double salary)
{
EID = id;
EName = name;
Salary = salary;
}
}
class Sorting : IComparer
{
public int Compare(Object a, Object b)
{
Employee em = (Employee)a;
Employee em1 = (Employee)b;
int value = 0;
if (em.GetNewSalary() > em1.GetNewSalary())
{
value = 10;
}
if (em.GetNewSalary() == em1.GetNewSalary())
{
value = 0;
}
if (em.GetNewSalary() < em1.GetNewSalary())
{
value = -10;
}
return value;
}
}
}
0 件のコメント:
コメントを投稿