blah blah blah is here! blah blah » Close

up0down
link

Hi all.
I'm making a school work here and I'm stuck and need some help from you guys.
This is a console application I'm working on.
I need to enter 8 salesmen with socialnr city and how much they sold this month. After that it needs to be sorted by sold amount, also there are 4 levels if they sold for less than 50 (level 1), or if they sold between 50 - 100 (level 2) and so on... It should look like this when it's done.
NAME SOCIALNR CITY SALES
Bo xxxxxxxx NY 45
1 Salesman sold according to level 1

Richard xxxxxxx LA 150
Lena xxxxxxx LA 175
2 Salesmen sold according to level 2

I have succed with everything except getting for example "1 Salesman sold according to level 1"
Can someone please look at my code and give me a hint of what I need to do?
Here is my code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Seller
{
public class Salesmen : IComparable
{
public string name;
public int socialnr;
public string city;
public int sales;

public Salesmen (string n, int so, string c, int s)
{
this.name = n;
this.socialnr = so;
this.city = c;
this.sales = s;
}
public void Print()
{
if (sales < 50)
{
Console.WriteLine(name + "\t\t" + socialnr + "\t\t" + city + "\t" + sales);
//Console.WriteLine(" The salesman has sold below 50");
}

if (sales > 50 && sales < 100)
{
Console.WriteLine(name + "\t\t" + socialnr + "\t\t" + city + "\t" + sales);
//Console.WriteLine( " The salesman has sold between 50 - 100");
}
if (sales > 100 && sales < 200)
{
Console.WriteLine(name + "\t\t" + socialnr + "\t\t" + city + "\t" + sales);
//Console.WriteLine( " The salesman has sold between 100 - 200");
}
if (sales > 200)
{
Console.WriteLine(name + "\t\t" + socialnr + "\t\t" + city + "\t" + sales);
//Console.WriteLine( " The salesman has sold 200 or more");
}
}
public int CompareTo(object o)
{
Salesmen sList = (Salesmen)o;
return sales.CompareTo(sList.sales);
}
}
class Program
{
static ArrayList Sale = new ArrayList();

public static void Main()
{
int quantity = 0;
while (quantity < 3)
{
NewPerson();
Console.Clear();
quantity++;
}

Sorting();
Console.WriteLine("Name\tSocialnr\tCity\tSales");
TheList();

Console.ReadKey();
}
static void NewPerson()
{
Console.Write("Enter a name: ");
string name = Console.ReadLine();
Console.Write("Enter a socialnr: ");
int socialnr = int.Parse(Console.ReadLine());
Console.Write("Enter a city: ");
string city = Console.ReadLine();
Console.Write("Enter a salesnr: ");
int sales = int.Parse(Console.ReadLine());

Salesmen sList = new Salesmen(name, socialnr, city, sales);
Sale.Add(sList);
}
static void TheList()
{
foreach (Salesmen sList in Sale)
{
sList.Print();
}
}
static void Sorting()
{
Sale.Sort();
}
}
}

last answered 2 years ago

1 answers

up0down
link

Well, the list of salesmen is already sorted and so, as you iterate through them, you need to figure out how many are within each sales level and then print out the summary for that level.

To do that I'd separate the calculation of the sales level from the printing out of individual details by placing them in separate methods. You also need a static method (as it doesn't refer to any particular salesman) to print out the summary for a given level.

I've made some alterations to your code on those lines and have adjusted the formatting slightly to get it to line up better. There are better ways to format output but I've tried to avoid anything that you might not yet have learnt:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Seller
{
public class Salesmen : IComparable
{
public string name;
public int socialnr;
public string city;
public int sales;

public Salesmen (string n, int so, string c, int s)
{
this.name = n;
this.socialnr = so;
this.city = c;
this.sales = s;
}

public void Print()
{
Console.WriteLine(name + "\t" + socialnr + "\t\t" + city + "\t" + sales);
}

public int GetLevel()
{
if (sales < 50)
{
return 1;
}
if (sales >= 50 && sales < 100)
{
return 2;
}
if (sales >= 100 && sales < 200)
{
return 3;
}
return 4;
}

public static void PrintSummary(int number, int level)
{
string sm;

if (number == 1)
{
sm = " Salesman ";
}
else
{
sm = " Salesmen ";
}

Console.WriteLine(number.ToString() + sm + "sold according to level " + level.ToString());
Console.WriteLine();
}

public int CompareTo(object o)
{
Salesmen sList = (Salesmen)o;
return sales.CompareTo(sList.sales);
}
}
class Program
{
static ArrayList Sale = new ArrayList();

public static void Main()
{
int quantity = 0;
while (quantity < 3)
{
Console.Clear();
NewPerson();
quantity++;
}

Sorting();
Console.Clear();
Console.WriteLine("Name\tSocialnr\tCity\tSales");
Console.WriteLine();
TheList();

Console.ReadKey();
}
static void NewPerson()
{
Console.Write("Enter a name: ");
string name = Console.ReadLine();
Console.Write("Enter a socialnr: ");
int socialnr = int.Parse(Console.ReadLine());
Console.Write("Enter a city: ");
string city = Console.ReadLine();
Console.Write("Enter a salesnr: ");
int sales = int.Parse(Console.ReadLine());

Salesmen sList = new Salesmen(name, socialnr, city, sales);
Sale.Add(sList);
}
static void TheList()
{
int total = 0;
int level = 1;
foreach (Salesmen sList in Sale)
{
int sLevel = sList.GetLevel();
if (sLevel == level)
{
sList.Print();
total++;
}
else
{
if (total > 0) Salesmen.PrintSummary(total, level);
sList.Print();
level = sLevel;
total = 1;
}
}
if (total > 0) Salesmen.PrintSummary(total, level);
}
static void Sorting()
{
Sale.Sort();
}
}
}

Thank you very much, have a great day.

Feedback