Advertisement
algoritmy0599

AG play

May 26th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. public class Golfplayer {
  2.  
  3. private String name;
  4. private int[] strokesPerHole;
  5.  
  6. public Golfplayer(String name, int numberOfHoles) {
  7. if (name == null || name.length() == 0)
  8. throw new IllegalArgumentException("Jméno nemůže být null.");
  9.  
  10. if (numberOfHoles < 1)
  11. throw new IllegalArgumentException("Počet jamek musí být kladný.");
  12.  
  13. this.name = name;
  14. this.strokesPerHole = new int[numberOfHoles];
  15. }
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. public int[] getStrokesPerHole() {
  26. return strokesPerHole;
  27. }
  28. public void setStrokesPerHole(int index, int numberOfStrokes) {
  29. if (index < 0 || index > this.strokesPerHole.length - 1)
  30. throw new IllegalArgumentException("Počer jamek přesahuje škálu.");
  31.  
  32. if (numberOfStrokes < 1)
  33. throw new IllegalArgumentException("Počet odpalů musí být kladné číslo.");
  34.  
  35. this.getStrokesPerHole()[index] = numberOfStrokes;
  36. }
  37.  
  38. public int getTotalStrokes() {
  39. int count = 0;
  40.  
  41.  
  42. for (int i = 0; i < this.strokesPerHole.length; i++) {
  43. count = count + strokesPerHole[i];
  44. }
  45.  
  46. return count;
  47. }
  48.  
  49. @Override
  50. public String toString() {
  51.  
  52. StringBuilder sb = new StringBuilder();
  53. sb.append("GolfPlayer [name = ").append(this.getName()).append(", strokesPerHole = ");
  54.  
  55. for (int i = 0; i < this.strokesPerHole.length; i++) {
  56. if (i == 0) {
  57. if (this.strokesPerHole[i] == 0) {
  58. sb.append("Turnaj nezačal");
  59. break;
  60. }
  61. else {
  62. sb.append(this.strokesPerHole[i]);
  63. }
  64. }
  65. else {
  66. sb.append(", ").append(this.strokesPerHole[i]);
  67. }
  68. }
  69.  
  70. sb.append("]");
  71.  
  72. return sb.toString();
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement