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!