IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Probleme pour classer tableau via Icomparer


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Novembre 2008
    Messages
    113
    Détails du profil
    Informations forums :
    Inscription : Novembre 2008
    Messages : 113
    Par défaut Probleme pour classer tableau via Icomparer
    bonjour a tous.

    alors voila, on a ici un programme pour gérer un magasin, ou chaque classe représente un filiale du magasin. Nous prendrons pour l'exemple la filiale "Book".

    je crée donc dans mon program.cs une liste de livres, basée sur ArrayList (obligatoire pour l'exercice).

    et je cherche a pouvoir les classer par Sort en utilisant un Icomparer, mais j'ai une erreur que je n'arrive pas a résoudre.

    Voila d'abord la classe Product :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace bookstore2
    {
        public class Product
        {
            private uint _ID;
            private string _Name;
            private decimal _Price;
     
            public uint ID
            {
                get { return _ID; }
                set { _ID = value; }
            }
            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            public decimal Price
            {
                get { return _Price; }
                set { _Price = value; }
            }
     
            public Product (uint id, string name, decimal price)
            {
                ID = id;
                Name = name;
                Price = price;
            }
     
            public Product()
            {
                ID = 0;
                Name = "";
                Price = 0;
            }
     
            public override string ToString()
            {
                return ID + "\t" + Name + "\t" + Price + "\t";
            }
        }
    }
    la classe Book maintenant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace bookstore2
    {
        public class Book : Product
        {
            private string _Author;
            private string _Publisher;
            public BookType type;
    
            public enum BookType {Baking, Scientific}
            
            public string Author
            {
                get { return _Author; }
                set { _Author = value; }
            }
            public string Publisher
            {
                get { return _Publisher; }
                set { _Publisher = value; }
            }
    
            public override string ToString()
            {
                return base.ToString() + Author + "\t" + Publisher + "\t" + type + "\n";
            }
    
            public Book(uint ID, string name, decimal price, string author, string publisher, BookType booktype) : base(ID,name,price)
            {
                Author = author;
                Publisher = publisher;
                type = booktype;
            }
    
            public Book() : base()
            {
                Author = "";
                Publisher = "";
            }
        }
        public class CompareNoms : IComparer<Book>
        {
            public int Compare(Book B1, Book B2)
            {
                int i = B1.Name.CompareTo(B2.Name);
                return i;
            }
        }
    }
    enfin la classe program :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using bookstore2;
    
    namespace ConsoleApplication
    {
        class Program
        {
            public static void Main (string[] args)
            {
                ArrayList bookArray = new ArrayList();
                string choice="";
                while (true)
                {
                    Console.Write("Please select an operation: \n");
                    Console.Write("Add (add)\nRemove (remove)\nPrint (print)\nAuthor (author)\nSort (sort)\n");
                    
                    choice = Console.ReadLine();
                    #region switch cases
                    switch (choice)
                    {
                        #region add
                        case "add":
                        {
                            Console.WriteLine("insert the book id");
                            uint bookid = Convert.ToUInt32(System.Console.ReadLine());
                            Console.WriteLine("insert the book name");
                            string bookname = System.Console.ReadLine(); 
                            Console.WriteLine("insert its price ");
                            decimal bookprice = Convert.ToDecimal(System.Console.ReadLine()); 
                            Console.WriteLine("insert the author name");
                            string authorname = System.Console.ReadLine();
                            Console.WriteLine("insert the publisher");
                            string publisher = System.Console.ReadLine();
                            Console.WriteLine("insert the type : ");
                            Book.BookType type = Book.BookType.Baking;
                            int index = 1;
                            foreach (Book.BookType b in Enum.GetValues(type.GetType()))
                            {
                                Console.WriteLine(index + " : " + b);
                                index += 1;
                            }
                            index = Convert.ToInt32(Console.ReadLine());
                            if (index == 1)
                                type = Book.BookType.Baking;
                            else if (index == 2)
                                type = Book.BookType.Scientific;
                            else
                                Console.WriteLine("Ben ca sera baking alors! nan mais!");
                            index = 1;
                            bookArray.Add(new Book(bookid, bookname, bookprice, authorname, publisher, type));
                            Console.WriteLine("There are so far {0} book(s) created\n", bookArray.Count);
                            break;
                        }
                        #endregion add
    
                        #region remove
                        case "remove":
                        {
                            if (bookArray.Count==0)
                            {
                                Console.Write("no book to remove\n");
                                break;
                            }
                            Console.Write("insert the book ID to remove\n");
                            uint ID = Convert.ToUInt32(System.Console.ReadLine());
                            int index = 0;
                            int indextemp = 0;
                            foreach (Book item in bookArray)
                            {
                                if (item.ID == ID)
                                {
                                    indextemp = index;
                                    break;
                                }
                                index++;
                            }
                            Console.WriteLine("The book {0} has been removed from database", bookArray[indextemp].ToString());
                            bookArray.RemoveAt(indextemp);
                            Console.WriteLine("There are so far {0} book(s) created\n", bookArray.Count);
                            indextemp = 0;
                            index--;
                            break;
                        }
                        #endregion remove
    
                        #region print
                        case "print":
                        {
                            foreach (Book item in bookArray)
                            {
                                System.Console.WriteLine(item.ToString());
                            }
                            break;
                        }
                        #endregion print
    
                        #region search by author
                        case "author":
                        {
                            Console.WriteLine("insert the author name");
                            string authorname = System.Console.ReadLine();
    
                            int sumofbooks = 0;
                            foreach (Book item in bookArray)
                            {
                                if (item.Author == authorname)
                                {
                                    Console.WriteLine(item.ToString());
                                    sumofbooks++;
                                }
                            }
                            if (sumofbooks == 0)
                            {
                                Console.WriteLine("no book of this author found.");
                            }
                            break;
                        }
                        #endregion search by author
    
                        case "sort":
                        {
                            bookArray.Sort(CompareNoms());
                            break;
                        }
                        
                        default:
                        {
                            Console.Write("please choose a valid operation\n");
                            break;
                        }
                    }
                    #endregion switch cases
                }
            }
        }
    }
    En gras, a la fin de Book et vers la fin de Program, la classe héritant Icomparer et ma tentative de classement via sort.

    Merci d'avance

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 8
    Par défaut
    c'est normal,
    Arraylist.sort() utilise System.Collections.ICompare non pas System.Collections.Generic.ICompare

    -----------------------------------------

    La meilleur solution: à la place de l'ArrayList il faut créer une List<BOOK>

    -------------- dans ton code-----------

    public static void Main(string[] args)
    {
    List<Book> bookArray = new List<Book>();
    string choice = "";
    while (true)
    ..
    ..

    case "sort":
    {

    IComparer<Book> myComparer = new CompareNoms();
    bookArray.Sort(myComparer);

    break;
    }

  3. #3
    Membre confirmé
    Inscrit en
    Novembre 2008
    Messages
    113
    Détails du profil
    Informations forums :
    Inscription : Novembre 2008
    Messages : 113
    Par défaut
    super, merci beaucoup a toi, je vais tenter de progresser maintenant !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 27/05/2006, 19h08
  2. Réponses: 21
    Dernier message: 28/02/2006, 15h23
  3. [MySQL] Probleme d'exploitation d'une vue (VIEW) pour un tableau
    Par nico4731 dans le forum PHP & Base de données
    Réponses: 17
    Dernier message: 03/02/2006, 14h26
  4. Réponses: 2
    Dernier message: 30/05/2002, 08h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo