Guest User

Untitled

a guest
Dec 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package com.learning;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import org.junit.Test;
  6.  
  7. public class RomanNumeralsTest {
  8.  
  9. interface RomanNumber {
  10.  
  11. int toNumeral();
  12.  
  13. int value();
  14.  
  15. }
  16.  
  17. class NextEmptyNumber implements RomanNumber {
  18.  
  19. @Override
  20. public int toNumeral() {
  21. return 0;
  22. }
  23.  
  24. @Override
  25. public int value() {
  26. return 0;
  27. }
  28.  
  29. }
  30.  
  31. class V implements RomanNumber {
  32.  
  33. private RomanNumber nextNumber;
  34.  
  35. public V(RomanNumber nextNumber) {
  36. this.nextNumber = nextNumber;
  37. }
  38.  
  39. public V() {
  40. this(new NextEmptyNumber());
  41. }
  42.  
  43. @Override
  44. public int toNumeral() {
  45. if (nextNumber.value() == 5) {
  46. throw new IllegalArgumentException("VV is not valid");
  47. }
  48. return nextNumber.value() + value();
  49. }
  50.  
  51. @Override
  52. public int value() {
  53. return 5;
  54. }
  55.  
  56. }
  57.  
  58. class I implements RomanNumber {
  59.  
  60. private RomanNumber nextNumber;
  61.  
  62. public I(RomanNumber nextNumber) {
  63. this.nextNumber = nextNumber;
  64. }
  65.  
  66. public I() {
  67. this(new NextEmptyNumber());
  68. }
  69.  
  70. public int toNumeral() {
  71. if (nextNumber.value() == 5) {
  72. return nextNumber.toNumeral() - value();
  73. }
  74. return nextNumber.toNumeral() + value();
  75. }
  76.  
  77. public int value() {
  78. return 1;
  79. }
  80.  
  81. }
  82.  
  83. @Test
  84. public void I_is_1() throws Exception {
  85. assertEquals(1, new I().toNumeral());
  86. }
  87.  
  88. @Test
  89. public void II_is_2() throws Exception {
  90. assertEquals(2, new I(new I()).toNumeral());
  91.  
  92. }
  93.  
  94. @Test
  95. public void III_is_3() throws Exception {
  96. assertEquals(3, new I(new I(new I())).toNumeral());
  97. }
  98.  
  99. @Test
  100. public void IV_is_4() throws Exception {
  101. assertEquals(4, new I(new V()).toNumeral());
  102. }
  103.  
  104. @Test
  105. public void VI_is_6() throws Exception {
  106. assertEquals(6, new V(new I()).toNumeral());
  107. }
  108.  
  109. @Test(expected = IllegalArgumentException.class)
  110. public void VV_is_invalid() throws Exception {
  111. new V(new V()).toNumeral();
  112. }
  113. }
Add Comment
Please, Sign In to add comment