Advertisement
Guest User

district 2

a guest
Oct 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package o1.election
  2.  
  3. class District(val name: String,val seats: Int,val candidates: Vector[Candidate]) {
  4.  
  5. def candidatesFrom (party2: String): Vector[Candidate] = {
  6. var wek = Vector[Candidate]()
  7. //for( index <- 0 to candidates.length-1 )
  8. //{
  9. // if( candidates(index).party == party2 ) wek = wek :+ candidates(index)
  10. //}
  11. wek = candidates.filter( _.party==party2)
  12. wek
  13. }
  14.  
  15. override def toString: String = {
  16. var nap = "juhu"
  17. nap= name + ": " + candidates.length + " " + "candidates " + seats + " seats"
  18. nap
  19. }
  20.  
  21. def topCandidate: Candidate = {
  22. var najlepszy = candidates(0)
  23. var najglo = candidates(0).votes
  24. for( index <- 0 to candidates.length -1)
  25. {
  26. if(najglo< candidates(index).votes)
  27. {
  28. najlepszy = candidates(index)
  29. najglo = candidates(index).votes
  30. }
  31. }
  32. najlepszy
  33. }
  34. def printCandidates(): Unit = {
  35. // for (index <- 0 to candidates.length-1)
  36. // println(candidates(index).toString)
  37. candidates.foreach( strin => println( strin.toString ) )
  38. }
  39.  
  40. def totalVotes(party: String) = {
  41. var ile=0
  42. // for( i <- 0 to candidates.length-1)
  43. // {
  44. // if(candidates(i).party == party) ile+=candidates(i).votes
  45. // }
  46. //ile
  47. var pom2 = candidates.filter(_.party==party)
  48. if(pom2.size>0)pom2.foldLeft( 0 )( (wyn , next) => wyn + next.votes)
  49. else 0
  50. }
  51.  
  52. def totalVotes = {
  53. var ile=0
  54. for( i <- 0 to candidates.length-1)
  55. {
  56. ile+=candidates(i).votes
  57. }
  58. ile
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement