samedi 20 août 2016

refine code logic swift

I have a tableView holding an array of 4 items, the frequency for each type of cleaning which will determine the value of var price.

var frequency = [

    Item(name:"Every week",selected: false, amount: 0),
    Item(name:"Every 2 weeks",selected: false, amount: 0),
    Item(name:"Every 4 weeks",selected: false, amount: 0),
    Item(name:"Once",selected: false, amount: 0),
    Item(name:"End of tenancy cleaning", selected: false, amount: 0)
]

Every time a row is selected, the previous one is deselected, meaning that there will never be 2 rows selected at the same time. Based on the selected row, I need to assign a value to

`var numberHours = 0`
 `var price = 0` 

I have tried doing this with if else statement link to my code, but my code will become too verbose. There is a pattern for all cases in which selectedBed >=3.

`var selectedBed:Int!` // receives value from another ViewController
`var selectedBath:Int!`//  receives value from another ViewController

I need to know if you have a better approach that would make my code less verbose.

`1 bed + 1 bath = 2 hours   £24 12/h    £22 11/h`

`1 bed + 2 bath = 3 hours   £36/12    £33/11`
`1 bed + 3 bath = 3 hours   £36/12    £33/11`
`1 bed + 4 bath = 3 hours   £36/12    £33/11`
`1 bed + 5 or n bath = 5 hours   £60/12    £55/11`



`2 bed + 0 or n bath = 3 hours   £36/12    £33/11`


`3 bed + 1 bath = 3 hours   £36/12    £33/11`
`3 bed + 2 bath = 3 hours   £36/12    £33/11`

`3 bed + 3 bath = 4 hours   £48/12    £44/11 `
`3 bed + 4 bath = 4 hours   £48/12    £44/11 `
`3 bed + 5 bath = 4 hours   £48/12    £44/11 `
`3 bed + 6 bath = 4 hours   £48/12    £44/11 `

`3 bed + 7 bath = 5 hours   £60/12    £55/11 `
`3 bed + 8 bath = 5 hours   £60/12    £55/11 ` 

`3 bed + 9 bath = 6 hours   £72/12    £66/11`
`3 bed + 10 bath = 6 hours   £72/12    £66/11`





`4 bed + 1 bath = 4 hours   £48/12    £44/11`
`4 bed + 2 bath = 4 hours   £48/12    £44/11`

`4 bed + 3 bath = 5 hours   £60/12    £55/11 `
`4 bed + 4 bath = 5 hours   £60/12    £55/11  `
`4 bed + 5 bath = 5 hours   £60/12    £55/11 ` 
`4 bed + 6 bath = 5 hours   £60/12    £55/11  `

`4 bed + 7 bath = 6 hours   £72/12    £66/11 `
`4 bed + 8 bath = 6 hours   £72/12    £66/11 `

`4 bed + 9 bath = 7 hours   £84/12    £77/11`
`4 bed + 10 bath = 7 hours   £84/12    £77/11`

`

Aucun commentaire:

Enregistrer un commentaire