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 :
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 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"; } } }
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 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; } } }
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.
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 } } } }
Merci d'avance![]()
Partager