'C # task (sorting by how many sellers)
I am relatively new to this with programming and am reading an introductory course where one task is to read information from sex workers in a sales force. The sales data must be sorted depending on the sales level that each seller has reached, preferably with the highest level first. There are four levels where level four is highest.
At the end of the program, the sales data must print the sorted sales data to both the screen and to a text file.
The following information must be loaded for each salesperson:
- Name.
- Social security number.
- District.
- Number of items sold.
I have not really got the sorting right and I want to put the different workers in the same category (do not know how to handle my Getlevel function here) as the following examples:
Name: Social security number: District: Articles:
Tina 49685958 East 10
Johan 5869494 Öst 40
Eric 59704696 Syd 40 3 Sellers have reached level 1: <50 items
Tyler 06486979 South 200
Anna¨ 68396859 West 200
Filip 58496849 Väst 200 3 Sellers have pretty level 4:> 200 items
Right now I get the result after each worker::
Name: Social security number: District: Articles:
Eva. 49685958. East. 10 1 seller has reached level 1: <50 items
Johan. 5869494 East. 40 1 seller has reached level 1: <50 items
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
// Deklarerar arrays
string[] name = new string[6];
string[] ssn = new string[6];
string[] district = new string[6];
int[] qty = new int[6];
// For loop för att kunna mata in 6 säljare
for (int i = 0; i <= 5; i++)
{
Console.Write("Name: ");
name[i] = Console.ReadLine();
Console.Write("Social security number: ");
ssn[i] = Console.ReadLine();
Console.Write("District: ");
district[i] = Console.ReadLine();
Console.Write("Number of items sold: ");
qty[i] = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
}
Console.WriteLine("\nName:\t\tSocial security number:\tDistrict:\tNumber of items sold:");
// Prints salespeople
for (int i = 0; i < name.Length; i++)
{
Array.Sort(qty);
Array.Reverse(qty);
Console.WriteLine(name[i] + "\t" + ssn[i] + "\t" + district[i] + "\t" + qty[i]);
Console.WriteLine("\n");
GetLevel(qty[i]);
}
// To prevent the console from closing
Console.ReadLine();
}
// Function that calculates the level the seller ends up in
static void GetLevel(int qty)
{
if (qty < 50)
Console.WriteLine(" Seller has reached level 1: <50 items.");
if (qty >= 50 && qty < 100)
Console.WriteLine(" Sellers have reached level 2: 50-99 items");
if (qty >= 100 && qty < 200)
Console.WriteLine(" Sellers have reached level 3: 100-199 items.");
if (qty > 200)
Console.WriteLine(" sellers have reached level 4:> 200 items.");
}
}
}
Solution 1:[1]
If you run this code, the function GetLevel is never called, so the program don't print it. Remove the comment
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
// Deklarerar arrays
string[] name = new string[6];
string[] ssn = new string[6];
string[] district = new string[6];
int[] qty = new int[6];
// For loop för att kunna mata in 6 säljare
for (int i = 0; i <= 5; i++)
{
Console.Write("Name: ");
name[i] = Console.ReadLine();
Console.Write("Social security number: ");
ssn[i] = Console.ReadLine();
Console.Write("District: ");
district[i] = Console.ReadLine();
Console.Write("Number of items sold: ");
qty[i] = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
}
Console.WriteLine("\nName:\t\tSocial security number:\tDistrict:\tNumber of items sold:");
// Prints salespeople
for (int i = 0; i < name.Length; i++)
{
Array.Sort(qty);
Array.Reverse(qty);
Console.WriteLine(name[i] + "\t" + ssn[i] + "\t" + district[i] + "\t" + qty[i]);
Console.WriteLine("\n");
GetLevel(qty[i]);
}
// To prevent the console from closing
Console.ReadLine();
}
// Function that calculates the level the seller ends up in
static void GetLevel(int qty)
{
if (qty < 50)
Console.WriteLine(" Seller has reached level 1: <50 items.");
if (qty >= 50 && qty < 100)
Console.WriteLine(" Sellers have reached level 2: 50-99 items");
if (qty >= 100 && qty < 200)
Console.WriteLine(" Sellers have reached level 3: 100-199 items.");
if (qty > 200)
Console.WriteLine(" sellers have reached level 4:> 200 items.");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marco |
