0

This C# assignment is about creating groups for salesmen at diffrent levels depending how much they sold for, and then write this info to a textfile using a streamwriter.

I have done all parts except the streamwriter part. What i do so far is to create the file, but there is no ouput. When i run this the content in the console is correct but the textfile becomes modified but blank.

The streamwriter is in the bottom part.

´

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Threading.Tasks;

namespace Inlämningsuppgift2
{

// Emplooyeklassen where names, personal info, district och sold items sparas och hämtas

class Emplooye
{

    String namn;
    int personnummer;
    string distrikt;
    int såldaartiklar;
    int säljnivå;


    public Emplooye(string namn, int personnummer, string distrikt, int såldaartiklar)
    {
        this.namn = namn;
        this.personnummer = personnummer;
        this.distrikt = distrikt;
        this.såldaartiklar = såldaartiklar;

    }

    public string Name
    {
        get { return namn; }
        set { namn = value; }
    }


    public int PersonNummer
    {
        get { return personnummer; }
        set { personnummer = value; }
    }


    public string Distrikt
    {
        get { return distrikt; }
        set { distrikt = value; }
    }


    public int AmountSold
    {
        get { return såldaartiklar; }
        set { såldaartiklar = value; }
    }

    public int SäljNivå
    {
        get { return säljnivå; }
        set { säljnivå = value; }
    }
}
 }

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Inlämningsuppgift2;
 using System.IO;

  namespace ConsoleApp3

{
    class Säljlista
    {
        static void Main(string[] args)
        {



            List<Emplooye> ObjSeller = new List<Emplooye>();
            List<Emplooye> group1 = new List<Emplooye>();
            List<Emplooye> group2 = new List<Emplooye>();
            List<Emplooye> group3 = new List<Emplooye>();

            ObjSeller.Add(new Emplooye("Oscar Norberg", 961111, "Täby", 140));
            ObjSeller.Add(new Emplooye("jonas okembia", 970912, "Uppsala", 70));
            ObjSeller.Add(new Emplooye("Mille vega", 981212, "Danderyd", 40));
            ObjSeller.Add(new Emplooye("Isak friisjespesen", 991132, "Skåne", 80));
            ObjSeller.Add(new Emplooye("Maja olofsson", 974132, "Täby", 123));
            ObjSeller.Add(new Emplooye("Annmarie norberg", 944432, "Täby", 230));
            ObjSeller.Add(new Emplooye("Ulla persson", 9312332, "Uppland", 124));
            ObjSeller.Add(new Emplooye("Christer Nilsen", 9988332, "Vallentuna", 444));

            Console.WriteLine("Namn    Personnummer     Distrikt   Antal");

            //Here the employees gets sorted in groups depending how much they sold for.


            foreach (Emplooye e in ObjSeller)
            {
                if (e.AmountSold > 0 && e.AmountSold <= 90)

                {
                    group1.Add(e);

                }

                else if (e.AmountSold > 90 && e.AmountSold <= 200)
                {
                    group2.Add(e);
                }
                else
                {
                    group3.Add(e);
                }

            }


            group1.Sort(delegate (Emplooye t1, Emplooye t2)
            { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
            );

            for (int i = 0; i < group1.Count; i++)

            {

                string name = group1.ElementAt(i).Name;
                int pnr = group1.ElementAt(i).PersonNummer;
                String district = group1.ElementAt(i).Distrikt;
                int amountsold = group1.ElementAt(i).AmountSold;

                Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);

            }
            Console.WriteLine("salesmen at level 1" + ": " + group1.Count);
            Console.WriteLine("");


            group2.Sort(delegate (Emplooye t1, Emplooye t2)
            { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
           );

            for (int i = 0; i < group2.Count; i++)

            {

                string name = group2.ElementAt(i).Name;
                int pnr = group2.ElementAt(i).PersonNummer;
                String district = group2.ElementAt(i).Distrikt;
                int amountsold = group2.ElementAt(i).AmountSold;

                Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);

            }
            Console.WriteLine("salesmen at level 2 2" + ": " + group2.Count);
            Console.WriteLine("");


            group3.Sort(delegate (Emplooye t1, Emplooye t2)
            { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
       );

            for (int i = 0; i < group3.Count; i++)

            {

                string name = group3.ElementAt(i).Name;
                int pnr = group3.ElementAt(i).PersonNummer;
                String district = group3.ElementAt(i).Distrikt;
                int amountsold = group3.ElementAt(i).AmountSold;

                Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);

            }
            Console.WriteLine("salesmen at level 3" + ": " + group3.Count);
            Console.WriteLine("");


        StreamWriter SW = new StreamWriter("important.txt");
        foreach(Emplooye i in group1)
        {

            SW.WriteLine(group1.ToString());

           foreach(Emplooye i in group2)
        {

            SW.WriteLine(group2.ToString());
        } 
            foreach(Emplooye i in group3)
        {

            SW.WriteLine(group3.ToString());



    }
}





    }
  • 1
    1) You forgot to *close* the writer. 2) `"important.txt"` is a relative path that points to the application's current directory. During debugging that relative path points to `bin\Debug`. Are you looking at the correct file? – Panagiotis Kanavos Nov 27 '18 at 13:55
  • Simply use the File.WriteAllLines() method. – Kunal Mukherjee Nov 27 '18 at 13:56
  • Do yourself a favor and hit `CTRL` + `K` + `D` (Format Code) in your `Visual Studio` when you have this file open. – Rand Random Nov 27 '18 at 14:29
  • FYI - `group3.Sort(delegate (Emplooye t1, Emplooye t2) { return (t1.AmountSold.CompareTo(t2.AmountSold)); }` - could be rewritten to just `group3.Sort((x, y) => x.AmountSold.CompareTo(y.AmountSold));` - shorter and looks nicer - but since you are sorting again and again with the same Information you should consider implementing `IComparable` in your `Emplooye` class than you can just call `list.Sort()` and it will sort based on your implementation of `IComparable` – Rand Random Nov 27 '18 at 14:39
  • @PanagiotisKanavos says "'Cannot write to a closed TextWriter" if i add it. 2) Yes i'm looking at the correct file i think, because it adds System.Collections.Generic.List`1[Inlämningsuppgift2.Emplooye] now everytime i run the program after implementing "using (StreamWriter SW = new StreamWriter("important.txt)) – Oscar Norberg Nov 27 '18 at 15:42
  • @OscarNorberg you won't get any such exception if you write to the writer *inside* the `using` block. If you don't close the writer, any changes may still be buffered – Panagiotis Kanavos Nov 27 '18 at 15:45
  • @PanagiotisKanavos - in his `foreach` he wrote the `list - group1` intead of the `item - i` – Rand Random Nov 27 '18 at 16:13

1 Answers1

3

You aren't closing the stream writer so it is buffering the output in memory but not writing to the file. Try a 'using' statement:

using(StreamWriter SW = new StreamWriter("important.txt"))
{
    foreach(Emplooye i in group1)
    {
        SW.WriteLine(i.ToString());
    }

    foreach(Emplooye i in group2)
    {
        SW.WriteLine(i.ToString());
    } 

    foreach(Emplooye i in group3)
    {
        SW.WriteLine(i.ToString());
    }
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
MikeS
  • 1,734
  • 1
  • 9
  • 13
  • Thanks for your reply, System.Collections.Generic.List`1[Inlämningsuppgift2.Emplooye] is what i get when trying this – Oscar Norberg Nov 27 '18 at 14:43
  • @OscarNorberg - updated the answer to use the `i` variable instead of the `group1/2/3` - but since you didn't overwrite the `ToString()` method in your `Emplooye` class it will only output the type of `Emplooye` if you want to output the `Name` for example change it to `SW.WriteLine(i.Name);` – Rand Random Nov 27 '18 at 16:12