Reputation: 13
I am new to programming and got stuck on an assignment.
The assignment is to input name, age and number of sales for 4 different workers. After that I need to post the workers in order based on their amount of sales.
Where I am mostly stuck is the loop-part. I do not understand how I can insert values to the 4 workers and then return their values in order.
int pay = 1000;
Console.WriteLine("What is the first- and lastname of the worker: ");
string name = Convert.ToString(Console.ReadLine());
Console.WriteLine("What is the age of the worker: ");
int age = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("What district does the worker work at: ");
string district = Convert.ToString(Console.ReadLine());
Console.WriteLine("How many sales has the worker done: ");
int sales = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("Name\t\tAge\tDistrict\tSales");
Console.WriteLine(name + "\t" + age + "\t" + district + "\t\t" + sales);
Console.WriteLine("\n");
if (sales > 199)
{
pay = pay + sales * 4;
Console.WriteLine(name + " is level 4 \nand will get: " + pay + "kr this month");
}
else if (sales <= 199 && sales >= 100)
{
pay = pay + sales * 3;
Console.WriteLine(name + " is level 3 \nand will get: " + pay + "kr this month");
}
else if (sales <= 99 && sales >= 50)
{
pay = pay + sales * 2;
Console.WriteLine(name + " is level 2 \nand will get: " + pay + "kr this month");
}
else
{
pay = pay + sales;
Console.WriteLine(name + " is level 1\nand will get: " + pay + "kr this month");
}
~ Please excuse the mess, I have yet to learn the proper structure of coding.
Upvotes: 1
Views: 579
Reputation: 155
This Worker class will hold information about one worker:
public class Worker()
{
string Name;
int NumberOfSales;
int Age;
public Worker(string _n, int _nos, int _a)
{
Name = _n;
NumberOfSales = _nos;
Age = _a;
}
}
This piece of code will run for 4 times, get information about workers, create Worker object for everyone of them and then add the objects to Workers
List object:
List<Worker> Workers = new List<Workers>();
int Worker = 1;
for (int i = Worker; i < 5; i++)
{
Console.WriteLine("Enter name for Worker {0}", Worker);
name = Console.ReadLine();
Console.WriteLine("Enter age for Worker {0}", Worker);
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of sales for Worker {0}", Worker);
nOfSales = Convert.ToInt32(Console.ReadLine());
Worker worker = new Worker(name, age, nOfSales);
Workers.Add(worker);
}
In this following link, you can learn how to sort your Workers
list object, by looking at every Worker
object's NumberOfSales
property: How to Sort a List<T> by a property in the object
Upvotes: 3