ラベル Review of C# の投稿を表示しています。 すべての投稿を表示
ラベル Review of C# の投稿を表示しています。 すべての投稿を表示

2010/04/15

Exam Preparation C# Use ArrayList in ArrayList

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;


 

namespace ConsoleApplication2

{


//Drow picture use ArrayList in ArrayList


class
Program

{


static
void Main(string[] args)

{


//Initialize parent arraylist


ArrayList al = new
ArrayList();


 


//Initialize child arraylist


ArrayList childal1 = new
ArrayList();

childal1.Add(" * ");


//Initialize child arraylist


ArrayList childal2 = new
ArrayList();

childal2.Add(" *** ");


//Initialize child arraylist


ArrayList childal3 = new
ArrayList();

childal3.Add(" ***** ");


//Initialize child arraylist


ArrayList childal4 = new
ArrayList();

childal4.Add("*******");


//Initialize child arraylist


ArrayList childal5 = new
ArrayList();

childal5.Add(" ***** ");


//Initialize child arraylist


ArrayList childal6 = new
ArrayList();

childal6.Add(" *** ");


//Initialize child arraylist


ArrayList childal7 = new
ArrayList();

childal7.Add(" * ");


 


//Put in child array into parent array

al.Add(childal1);

al.Add(childal2);

al.Add(childal3);

al.Add(childal4);

al.Add(childal5);

al.Add(childal6);

al.Add(childal7);


 


//Display ArrayList data


foreach (ArrayList array in al)

{


//Initialize IEnumerator


IEnumerator enm = array.GetEnumerator();


//Get data using IEnumerator


while (enm.MoveNext())

{


Console.WriteLine(enm.Current.ToString());

}

}

}

}

}

Exam preparation C# Sorting

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.IO;


 

namespace ConsoleApplication5

{


//Sorting programming


//I create 3 classes Product, Sorting and Program.


//Product class has ID, name, price, taxrate, tax and total price.


//Soting class has compare method.


//Program class has user to input product, create their objects and sort by total price.


//Also, this result stores txt file.


class
Program

{


static
void Main(string[] args)

{


//Create ArrayList


ArrayList al = new
ArrayList();


 


//Input TaxRate


Console.WriteLine("Please input TaxRate.");


//Read TaxRate


string input = Console.ReadLine();


//Convert double


double taxrate = double.Parse(input);


 


//Set TaxRate value

Product.SetTaxRate(taxrate);


 


//Declare variable for do while loop


int i = 0;


 


//Start do while loop


do

{


//Input ID


Console.WriteLine("Please input product ID.");


//Read ID


string input1 = Console.ReadLine();


//Convert integer


int id = int.Parse(input1);


 


//Input Name


Console.WriteLine("Please input product Name.");


//Read Name


string name = Console.ReadLine();


 


//Input price


Console.WriteLine("Please input product Price.");


//Read price


string input2 = Console.ReadLine();


//Convert double


double price = double.Parse(input2);


 


//Create object

Product pr= new Product(id, name, price);

al.Add(pr);


 


//Ask to input another product


Console.WriteLine("Do you want to input another prodyct? yes-1 no-0");


//Read answer


string input3 = Console.ReadLine();


//Convert integer

i = int.Parse(input3);


 


//End of do while loop

}


while (i == 1);


 


//Display current Array order before sorting


foreach (Product pr in al)

{


//Set tax value

pr.SetTax();


//Set total price value

pr.SetTotalPrice();


//Display current value


Console.WriteLine("ID: "+pr.GetID() + "\nName: " + pr.GetName() + "\nPrice: " + pr.GetPrice() + "\nTax: " + pr.GetTax() + "\nTotalPrice: " + pr.GetTotalPrice()+"\n");

}


 


//Excute sorting

al.Sort(new Sorting());


 


try

{


//Initialize filestreem


FileStream fs = new
FileStream

(


//Set file access


@"E:\VirtualExpander_J\C#\Fileaccess\exampractice.txt",


FileMode.Create,


FileAccess.ReadWrite,


FileShare.Read

);


 


//Initialize stream writer


StreamWriter sw = new
StreamWriter(fs);


 


//Display current Array order after sorting


foreach (Product pr1 in al)

{


 


//Display current value


Console.WriteLine("ID: " + pr1.GetID() + "\nName: " + pr1.GetName() + "\nPrice: " + pr1.GetPrice() + "\nTax: " + pr1.GetTax() + "\nTotalPrice: " + pr1.GetTotalPrice() + "\n");


 


//Write current value

sw.WriteLine("ID: " + pr1.GetID() + "Name: " + pr1.GetName() + "Price: " + pr1.GetPrice() + "Tax: " + pr1.GetTax() + "TotalPrice: " + pr1.GetTotalPrice() + "\n");

}


 


//Close stream writer

sw.Close();


//Close file streem

fs.Close();

}


catch

{


Console.WriteLine("Try block error");

}

}

}


//Product template


class
Product

{


//Declare variables


int ID;


string Name;


double Price;


static
double TaxRate;


double Tax;


double TotalPrice;


 


//Create method return ID


public
int GetID()

{


return ID;

}


 


//Create method return Name


public
string GetName()

{


return Name;

}


 


//Create method return Price


public
double GetPrice()

{


return Price;

}


 


//Create static method set TaxRate


public
static
void SetTaxRate(double d)

{

TaxRate = d;

}


 


//Create ststic method return TaxRate


public
static
double GetTaxRate()

{


return TaxRate;

}


 


//Create method calculate Tax


public
void SetTax()

{

Tax = Price * TaxRate;

}


 


//Create method return Tax


public
double GetTax()

{


return Tax;

}


 


//Create method calculate TotalPrice


public
void SetTotalPrice()

{

TotalPrice = Price + Tax;

}


 


//Create method return TotalPrice


public
double GetTotalPrice()

{


return TotalPrice;

}


 


//Constructor of Product class


public Product(int id, string name, double price)

{

ID = id;

Name = name;

Price = price;

}


//End of product class

}


//Sorting


class
Sorting : IComparer

{


//Compare method


public
int Compare(object a, object b)

{


//Declare return value


int value = 0;


 


//Cast object a and b

Product prod = (Product)a;

Product prod1 = (Product)b;


 


//Get TotalPrice value


double comprice = prod.GetTotalPrice();


double comprice1 = prod1.GetTotalPrice();


 


//Compare each case


if (comprice > comprice1)

{

value = 10;

}


 


if (comprice == comprice1)

{

value = 0;

}


 


if (comprice < comprice1)

{

value = -10;

}


 


//return result


return value;

}

}

}

2010/04/14

Exam preparation C# Random number

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


 

namespace ConsoleApplication4

{


//Random number


//Someone select number and bet money.


//If this number much the random number, he will play again and select number again.


//If not, he will lose money.


//And programming ask him whether he wants to play again or not.


class
Program

{


static
void Main(string[] args)

{


//Initialize Random


Random rd = new
Random();


 


//Declare variable for while roop


int h=0;


 


//He plays at least one time. After playing, he chose to play again or not.


do

{


 


//Ask him to select number


Console.WriteLine("Pay $5.00 and select numbaer from 1-6.");


 


//Generate random number


int rd1 = 1 + rd.Next(6);


 


//Display random number for Debug


Console.WriteLine("Random number " + rd1);


 


//Read input data


string input1=Console.ReadLine();


 


//Convert from string to interger


int i=int.Parse(input1);


 


//Check to mutch input number and random number


if (i == rd1)

{


//Display congraturations and ask 2nd number


Console.WriteLine("Congraturations!! First much!");


Console.WriteLine("Please select 2nd number from 1-6.");


 


//Generate 2nd random numbaer


int rd2 = 1 + rd.Next(6);


 


//Display 2nd random number for debug


Console.WriteLine("2nd rndom number " + rd2);


 


//Read input 2nd data


string input2 = Console.ReadLine();


 


//Convert from string to interger


int k = int.Parse(input2);


 


//Check to much 2nd input number and 2nd random number


if (k == rd2)

{


//Display congraturations


Console.WriteLine("Congraturations!! Second much!!");


Console.WriteLine("You win this game and get $1,000.00!!");


break;

}


else

{


//Display his loose


Console.WriteLine("Sorry you lose this game.");

}

}


else

{


//Display his loose


Console.WriteLine("Sorry you lose this game.");

}


//Ask him to play again or not


Console.WriteLine("Do you want to play again? Yes-1 No-0");


 


//Read input data


string input3 = Console.ReadLine();


 


//Convert from string to integer

h = int.Parse(input3);


 


//End of do while loop

}while(h==1);


 


//Display good bye


Console.WriteLine("Good bye! See you!!");


 

}

}

}

2010/04/13

Exam preparation C# Roll die

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


 

namespace ConsoleApplication3

{


//Roll die programming


//This example is that someone throws a roll die 500 times.


//And we count how many times each face is shown.


class
Program

{


static
void Main(string[] args)

{


//Initialize Random generator


Random rd = new
Random();


 


//Initialize variables for each face


int count1=0;


int count2=0;


int count3=0;


int count4=0;


int count5=0;


int count6=0;


 


 


//A roll die is thrown 500 times and count each face


for (int i = 0; i <500; i++)

{


//Create random number


int x = 1+rd.Next(6);


 


//Use swich to count each face


switch (x)

{


//Add the count of face 1


case 1:

count1++;


break;


//Add the count of face 2


case 2:

count2++;


break;


//Add the count of face 3


case 3:

count3++;


break;


//Add the count of face 4


case 4:

count4++;


break;


//Add the count of face 5


case 5:

count5++;


break;


//Add the count of face 6


case 6:

count6++;


break;

}

}


//Show total counts in each face


Console.WriteLine("face 1 is " + count1);


Console.WriteLine("face 2 is " + count2);


Console.WriteLine("face 3 is " + count3);


Console.WriteLine("face 4 is " + count4);


Console.WriteLine("face 5 is " + count5);


Console.WriteLine("face 6 is " + count6);

}

}

}

Exam preparation C# Find bug

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


 

namespace ConsoleApplication1

{


//Exam Practice1 Bug fix


class
Program

{


static
void Main(string[] args)

{


//*Bug* Change to create parent array


//tomykid[] a = new tomykid[3];


 


animal[] a = new
animal[3];


dog d = new
dog("tomy");


tomy t = new
tomy("tomy", "white");


tomykid tk = new
tomykid("tomykid","black");


 


//*Bug* Change to be assigned children objects to parent array


//a[0] = (tomykid)d;


//a[1] = (tomykid)t;


//a[2] = tk;


 

a[0] = (animal)d;

a[1] = (animal)t;

a[2] = (animal)tk;


 


//*Bug* Change from for to foreach


//for(tomykid k:a)


//{


//Console.WriteLine(k.size()+k.bark());


//}


 


foreach (animal ani in a)

{

ani.size();

ani.bark();

}


 

}

}


public
interface
animal

{


//*Bug* Remove interface cannot contain fields


//int i = 10;


 


void bark();


 


//*Bug* Add method size()


void size();

}


public
class
dog : animal

{


private
string name;


 


//*Bug* Add virtual


virtual
public
void bark()

{


Console.WriteLine("dog barks");

}


 


//*Bug* Add virtual


virtual
public
void size()

{


Console.WriteLine("size unknown");

}


 


public dog(string i)

{

name = i;

}


 

}


public
class
tomy : dog

{


string color;


 


//*Bug* Add override


override
public
void bark()

{


Console.WriteLine("Tomy barks");

}


 


//*Bug* Add override


override
public
void size()

{


Console.WriteLine("small size");

}


 


public tomy(string name, string color)

: base(name)

{


this.color = color;

}


 

}


public
class
tomykid : tomy

{


string haircolor;


 


//*Bug* Add override


override
public
void bark()

{


Console.WriteLine("tomykid barks");

}


 


//*Bug* Add override


override
public
void size()

{


Console.WriteLine("medium size");

}


 


//*Bug* Add :base(color)


public tomykid(string name,string color)

: base(name,color)

{

haircolor = color;

}

}

}

2010/04/09

C# week11_2

Bug fix

namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
Fruit[] na=new Fruit[2];
na[0]=new Fruit("xyz","white",0.00);
na[1]=new Apple("xyz","black",5.75);
Console.WriteLine(na[0].price);
Console.WriteLine(na[1].color);

}
}
class Fruit
{
string fruitname;
public string color;
public double price;
//Add Constructer
public Fruit(string name, string color,double price)
{
name = fruitname;
this.color = color;
this.price = price;
}
}
class Apple : Fruit
{
//string color;
public Apple(string name, string color,double price):base(name,color,price)
{
//Base(name);
this.color = color;
}
}
class NZApple : Apple
{
//double price;
public NZApple(string name, string color, double price):base(name,color,price)
{
this.price = price;
}
}
}

2010/04/08

C# week11_1

Find bugs
Revised class

namespace ConsoleApplication27
{
class Program
{
static void Main(string[] args)
{
dog[] a = new dog[3];
dog d = new dog("tomy");
tomy t = new tomy("tomy", "white");
//change
tomykid tk = new tomykid("tomy","black");
a[0] = d;
a[1] =t;
a[2] = tk;
//change
foreach (dog k in a)
{
k.size();
k.bark();
}
}
}
public interface animal
{
void bark();
}
public class dog : animal
{
private string name;
public dog(string i)
{
name = i;
}
public void bark()
{
Console.WriteLine("dog barks");
}
public void size()
{
Console.WriteLine("size unknown");
}

}
public class tomy : dog
{
string color;
public tomy(string name, string color):base(name)
{
this.color=color;
}
public void bark()
{
Console.WriteLine("tomy barks");
}
public void size()
{
Console.WriteLine("small size");
}
}
public class tomykid : tomy
{
string haircolor;
//Change Constructor error
public tomykid(string name, string color):base(name, color)
{
this.haircolor = color;
}
public void bark()
{
Console.WriteLine("tomykid barks");
}
public void size()
{
Console.WriteLine("medium size");
}
}
}

2010/04/02

C# week10_3


31/03/2010

Arraylist in Arraylist


 

namespace ConsoleApplication9

{

class Program

{

static void Main(string[] args)

{

ArrayList row1 = new ArrayList();

row1.Add("* * ****** * * *** ");

ArrayList row2 = new ArrayList();

row2.Add("* * * * * * * ");

ArrayList row3 = new ArrayList();

row3.Add("****** ****** * * * *");

ArrayList row4 = new ArrayList();

row4.Add("* * * * * * * ");

ArrayList row5 = new ArrayList();

row5.Add("* * ****** ****** ****** *** ");


 


 

ArrayList al = new ArrayList();

al.Add(row1);

al.Add(row2);

al.Add(row3);

al.Add(row4);

al.Add(row5);


 


 

foreach (ArrayList a in al)

{

IEnumerator ie = a.GetEnumerator();

while (ie.MoveNext())

{

Console.WriteLine(ie.Current.ToString());

}


 

}

}

}

}


 

Output

HELLO

 

One of my class mates wrote "HELL":)

C# week10_2

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;

}

}

}

 
 

2010/04/01

Would you like to help C# programming?

C#が得意という方。
ぜひ教えてほしいことがあります。
授業で書いたプログラムなのですが、このままコンパイルするとコメントの☆部分が原因でソートできずにランタイムエラーになっちゃいます。
なぜエラーになるのか、もし分かる方がいたら教えてください。
いろいろ調べたものの見つからず。。。。。
もし分かる方がいたら教えてくださいー。
よろしくお願いします!


プログラムの概要
  1. 3つのクラスで構成
  2. Employeeクラス
  • EmployeeID、Name、Salary の変数
  • Salaryが年5%増加と仮定してGetNewSalary()メソッドで新しいサラリーを計算
3. Sortingクラス
  •  IComparerのinheritanceを利用
  • ランタイムエラーのコメントは以下のとおりです。
  • ハンドルされていない例外: System.ArgumentException: Array.Sort が x. CompareTo(x) を呼び出したときに、IComparer または依存する IComparable メソッドは 0 を返しませんでした。x: 'ConsoleApplication24.Employee'  x の型: 'Employee' IComparer: 'ConsoleApplication24.Sorting'
4. Programクラス
  • AnnualIncrementを入力してもらい、Valueを決定
  • Employeeオブジェクトを4つ作成しArrayListにインプット
  • GetNewSalaryで各自の新しいサラリーを表示(ここまでは表示できます。)
  • 新しいサラリーを昇順でソートしてアウトプット(ここでエラー)


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();
            al.Add(new Employee(1, "ABC", 200));
            al.Add(new Employee(2, "DEF", 100));
            al.Add(new Employee(3, "GHK", 300));
            al.Add(new Employee(4, "OPQ", 250));

            foreach (Employee n in al)
            {
                Console.WriteLine(n.GetNewSalary());
            } 
try
            {

            al.Sort(new Sorting());
foreach (Employee m in al)
                {
                    Console.WriteLine(m.GetEID() + " " + m.GetEName() + " " + m.GetNewSalary());

                }
            }
            catch
            {
                Console.WriteLine("Try block error");
            }
        }

    }
    class Employee
    {
        int EID;
        string EName;
        double Salary;
        public 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()
        {
            this.Salary = Salary + (Salary * Increment);
            return this.Salary;
            //☆この部分がエラー。下の2行に置き換えると問題なく動く。
            //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;
            double sal = em.GetNewSalary();
            double sal1 = em1.GetNewSalary();
            int value = 0;


            if (sal > sal1)
            {
                value = 10;
            }
            else if (sal == sal1)
            {
                value = 0;
            }
            else if (sal < sal1)
            {
                value = -10;
            }

            return value;
        }
    }
}

2010/03/29

C# week10_1

29/03/2010

Sorting

 
 

namespace ConsoleApplication23

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Please enter Annual increment");

string a = Console.ReadLine();

int j = int.Parse(a);

Employee.AnnualIncrement=j;

Console.WriteLine(j);

Employee[] e = new Employee[4];

e[0] = new Employee(1, "ABC", 100.00);

e[1] = new Employee(2, "AAA", 200.00);

e[2] = new Employee(3, "XYZ", 500.00);

e[3] = new Employee(4, "CMG", 300.00);


 

foreach (Employee m in e)

{

Console.WriteLine(m.EmployeeName);

}

Array.Sort(e, new Sorting());

 
 

 
 

foreach (Employee n in e)

{

Console.WriteLine(n.EmployeeName+ " " + n.Salary + " "+n.GetAnnualIncrement());


 

}

 
 

}

}

class Employee

{

public int EmployeeID;

public string EmployeeName;

public double Salary;

public static int AnnualIncrement;

 
 

public double GetAnnualIncrement()

{

double inc = Salary*AnnualIncrement/100;

return inc;

}

 
 

public Employee(int id, string name, double salary)

{

EmployeeID =id;

EmployeeName=name;

Salary = salary;

}

}

class Sorting : IComparer

{

 
 

public int Compare(object a, object b)

{

Employee em = (Employee)a;

Employee em1 = (Employee)b;

 
 

double increment = em.GetAnnualIncrement();

double increment1 = em1.GetAnnualIncrement();

 
 

if (increment > increment1)

{

return 10;

}

else

if (increment == increment1)

{

return 0;

}

else

{

return -10;

}


 

}

 
 

}

 
 

}

2010/03/26

C# week9_3

24/03/2010

 
 

Class Program

Making programming by myself

 
 

  1. Random number simple lotto

    namespace ConsoleApplication7

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.WriteLine("Please input number from 0-10");

    Random r = new Random();

    int j = r.Next(10);

    Console.WriteLine(j);

    string num = Console.ReadLine();

    int i = int.Parse(num);

     
     

    if (i == j)

    {

    Console.WriteLine("Frist Match!!");

    Console.WriteLine("Please input number from 0-10");

    string num1 = Console.ReadLine();

    int k = int.Parse(num1);

    i = i - 1;

    if (i == k)

    {

    Console.WriteLine("Conglaturations!! You will $1,000.00!!");

    }

    }

    else

    {

    Console.WriteLine("Do you want to play again? yes-1 no-0");

    string num2 = Console.ReadLine();

    int l = int.Parse(num2);

    while (l == 1)

    {

    Console.WriteLine("Please input number from 0-10");

    string num3 = Console.ReadLine();

    int m = int.Parse(num3);

    int n = r.Next(10);

    if (m == n)

    {

    Console.WriteLine("Frist Match!!");

    Console.WriteLine("Please input number from 0-10");

    string num4 = Console.ReadLine();

    int o = int.Parse(num4);

    m = m - 1;

    if (o == m)

    {

    Console.WriteLine("Conglaturations!! You will $1,000.00!!");

    }

    }

    else

    {

    Console.WriteLine("Do you want to play again? yes-1 no-0");

    string num4 = Console.ReadLine();

    int p = int.Parse(num4);

    while (p == 1)

    {

     
     

    }

    if (p == 0)

    {

    Console.WriteLine("Good bye");

    }

    }

     
     

    }

    if (l == 0)

    {

    Console.WriteLine("Good bye");

     
     

    }

    }

    }

    }

     
     

  2. Enter 1 Employee data and calculate tax and net salary

    namespace ConsoleApplication8

    {

    class Program

    {

    static void Main(string[] args)

    {

     
     

    Console.WriteLine("Please input Employee ID");

    string k = Console.ReadLine();

    int id = int.Parse(k);

    Console.WriteLine("Please input Employee Name");

    string name = Console.ReadLine();

    Console.WriteLine("Please input Employee Job");

    string job = Console.ReadLine();

    Console.WriteLine("Please input Employee Salary");

    string j = Console.ReadLine();

    double salary = double.Parse(j);

     
     

     
     

    Employee e=new Employee(id,name,job,salary);

    int taxrate = e.GetTaxRate();

    double tax = e.GetTax();

    double netsalary = e.GetNetTax();

    Console.WriteLine("Employee Details");

    Console.WriteLine("Employee ID:" + id);

    Console.WriteLine("Employee Name:" + name);

    Console.WriteLine("Employee Job:" + job);

    Console.WriteLine("Employee Salary:" + salary);

    Console.WriteLine("Employee Tax Rate:" + taxrate+"%");

    Console.WriteLine("Employee Tax:" + tax);

    Console.WriteLine("Employee Net Salary:" + netsalary);

     
     

    }

    }

    class Employee

    {

    int EmployeeID;

    string EmployeeName;

    string EmployeeJob;

    double Salary;

     
     

    public int GetTaxRate()

    {

    int taxrate = 0;

    if (Salary < 40000)

    {

    taxrate = 20;

    }

    if (40000 <= Salary & Salary < 60000)

    {

    taxrate = 25;

    }

    if (60000 <= Salary & Salary < 80000)

    {

    taxrate = 30;

    }

    if (80000 <= Salary)

    {

    taxrate = 40;

    }

    return taxrate;

    }

    public double GetTax()

    {

    double tax = 0;

    tax = Salary * GetTaxRate() / 100;

    return tax;

    }

    public double GetNetTax()

    {

    double nettax = 0;

    nettax = Salary - GetTax();

    return nettax;

    }

    public int GetID()

    {

    return EmployeeID;

    }

    public string GetName()

    {

    return EmployeeName;

    }

    public string GetJob()

    {

    return EmployeeJob;

    }

    public double GetSalary()

    {

    return Salary;

    }

    public Employee(int id, string name, string job, double salary)

    {

    EmployeeID = id;

    EmployeeName = name;

    EmployeeJob = job;

    Salary = salary;

    }

     
     

    }

    }

     
     

     
     

  3. Enter many Employees data and calculate tax and net salary

    namespace ConsoleApplication8

    {

    class Program

    {

    static void Main(string[] args)

    {

    ArrayList employee = new ArrayList();

     
     

    for (int i = 0; i < 2; i++)

    {

    Console.WriteLine("Please input Employee ID");

    string k = Console.ReadLine();

    int id = int.Parse(k);

    Console.WriteLine("Please input Employee Name");

    string name = Console.ReadLine();

    Console.WriteLine("Please input Employee Job");

    string job = Console.ReadLine();

    Console.WriteLine("Please input Employee Salary");

    string j = Console.ReadLine();

    double salary = double.Parse(j);

     
     

    Employee em = new Employee(id, name, job, salary);

    employee.Add(em);

    }

    foreach (Employee e in employee)

    {

    int id = e.GetID();

    string name = e.GetName();

    string job = e.GetJob();

    double salary = e.GetSalary();

    int taxrate = e.GetTaxRate();

    double tax = e.GetTax();

    double netsalary = e.GetNetTax();

    Console.WriteLine("Employee Details");

    Console.WriteLine("Employee ID:" + id);

    Console.WriteLine("Employee Name:" + name);

    Console.WriteLine("Employee Job:" + job);

    Console.WriteLine("Employee Salary:" + salary);

    Console.WriteLine("Employee Tax Rate:" + taxrate);

    Console.WriteLine("Employee Tax:" + tax);

    Console.WriteLine("Employee Net Salary:" + netsalary);


     

    }

    }

    }

    class Employee

    {

    int EmployeeID;

    string EmployeeName;

    string EmployeeJob;

    double Salary;

     
     

    public int GetTaxRate()

    {

    int taxrate = 0;

    if (Salary < 40000)

    {

    taxrate = 20;

    }

    if (40000 <= Salary & Salary < 60000)

    {

    taxrate = 25;

    }

    if (60000 <= Salary & Salary < 80000)

    {

    taxrate = 30;

    }

    if (80000 <= Salary)

    {

    taxrate = 40;

    }

    return taxrate;

    }

    public double GetTax()

    {

    double tax = 0;

    tax = Salary * GetTaxRate() / 100;

    return tax;

    }

    public double GetNetTax()

    {

    double nettax = 0;

    nettax = Salary - GetTax();

    return nettax;

    }

    public int GetID()

    {

    return EmployeeID;

    }

    public string GetName()

    {

    return EmployeeName;

    }

    public string GetJob()

    {

    return EmployeeJob;

    }

    public double GetSalary()

    {

    return Salary;

    }

    public Employee(int id, string name, string job, double salary)

    {

    EmployeeID = id;

    EmployeeName = name;

    EmployeeJob = job;

    Salary = salary;

    }

     
     

    }

    }

     
     

UA-9417263-1