Request of help - blocked student

Hello to all!

Here we are, I am student and I have to create a small project in Swift, only, I block..


We had as this lesson:


· A class « Personne »: this class will have to have three attributes (nom (String), prenom (String) and age (Int)). You will define a builder accepting three parameters who will allow to initialize our three attributes. Finally, you will define a method affiche() which will post the nom, the prone and the age of the person.

For that, I obtain this code:


class Personne { /
    var nom: String
    var prenom: String
    var age: Int

    init(nom: String, prenom: String, age: Int) { /
        self.nom = nom
        self.prenom = prenom
        self.age = age
    }

    func affiche() { /
        print("Etudiant : "+self.nom, self.prenom)
        print("Age : \(self.age)")
    }
}


Then:

· A Etudiant class which inherits from the class Personne: this class will have for additional attributes type Bool's for bourse (Bool) and annee (Int). You will define a builder who will allow to initialize all the attributes of this class. You will also redefine the method affiche() which will post all the attributes of Etudiant. For that purpose, I require the use the keyword super (for the builder and the method affiche()).

I have this code:


class Etudiant : Personne {
    var bourse: Bool
    var annee: Int

    init(nom: String, prenom: String, age: Int, bourse: Bool, annee: Int) {
        self.bourse = bourse
        self.annee = annee
        super.init(nom: nom, prenom: prenom, age: age)
    }

    override func affiche() {
        super.affiche()
        print("Etudiant : "+self.nom, self.prenom)
        print("Age : \(self.age)")
        print("Bourse : \(self.bourse)")
        print("Année d'étude : \(self.annee)")
    }

}


And to finish, where I block(surround)..

· A class Promotion: she will have for only attribute students' list. You will implement a builder without parameters who will initialize your vacuous picture. You will define of more an method ajouterEtudiant(etudiant: Etudiant) which will add a student in your picture and a method rechercherEtudiant (nom: String, prone: String) - > Bool who will allow to know if yes or not the student asked in parameter is present in our picture. Finally, you will define a last method affichePromotion() which will allow to post the students of the whole promotion.

I tried this, but nothing go:


class Promotion {
    var liste: [Etudiant] = []
  
    func ajouterEtudiant(etudiant: Etudiant) { /
        liste.append(etudiant)
    }
  
    func rechercherEtudiant(nom: String, prenom: String) -> Bool {
      
        var nom = nom /
        var prenom = prenom
      
        /
        /
        var motTrouve: Bool = false
      
        for mot in self.liste {
            if mot ==  nom{
                motTrouve = true
            }
        }
    }
}


To finish, my main.swift file necessarily has to be this one:


let promotion = Promotion()
let etudiant1 = Etudiant(nom: "Durand", prenom: "Jacques", age: 18, bourse: true, annee: 2015)
let etudiant2 = Etudiant(nom: "Dupont", prenom: "Jean", age: 19, bourse: false, annee: 2015)
let etudiant3 = Etudiant(nom: "Duval", prenom: "Robert", age: 18, bourse: true, annee: 2015)
promotion.ajouterEtudiant(etudiant1)
promotion.ajouterEtudiant(etudiant2)
promotion.ajouterEtudiant(etudiant3)
if promotion.rechercherEtudiant("Dupont", prenom: "Jean") {
    print("Dupont Jean est bien présent dans cette promotion !")
}
promotion.affichePromotion()


Thanks to all for your precious help!

· A class Promotion: she will have for only attribute students' list. You will implement a builder without parameters who will initialize your vacuous picture. You will define of more an method ajouterEtudiant(etudiant: Etudiant) which will add a student in your picture and a method rechercherEtudiant (nom: String, prone: String) - > Bool who will allow to know if yes or not the student asked in parameter is present in our picture. Finally, you will define a last method affichePromotion() which will allow to post the students of the whole promotion.

It might be better to post the original French rather than running it through Google Translate; I'm having trouble parsing the above.


Anyway, looking at your code, it looks like your "liste" variable is an array of Etudiants, so your "mot" variable in your for loop is going to be an Etudiant. You're then using Etudiant on the left hand side of an == operator, which won't work since Etudiant isn't Equatable; however, the larger problem is that the thing you're testing it against, "nom", isn't the same type of object—it's a String, not an Etudiant, so testing its equality against an Etudiant doesn't really make sense.


From this, it should be pretty clear what you need to do.


(Alternatively, if your class has covered closures yet, and if you're using Swift 2.0, you could look into using indexOf instead of the for loop. If you haven't covered functional programming concepts yet, disregard this note.)


(There are some other optimizations I'd make as well; for example, if you find the object in the for loop, you can just return then and there, or at least break out of the for loop. There's no need to keep iterating through the array and checking all the students if you've already found the one you were looking for.)

Request of help - blocked student
 
 
Q