fork download
  1. import java.io.*;
  2. import java.math.BigDecimal;
  3. import java.util.Scanner;
  4.  
  5. interface IBond
  6. {
  7. public BigDecimal getPrice();
  8. public void setPrice( BigDecimal price );
  9.  
  10. public BigDecimal getPeriod();
  11. public void setPeriod( BigDecimal period );
  12.  
  13. public BigDecimal getCouponRate();
  14. public void setCouponRate( BigDecimal couponRate );
  15.  
  16. public BigDecimal getCpiRate();
  17. public void setCpiRate( BigDecimal cpiRate );
  18. }
  19.  
  20. abstract class Bond implements IBond
  21. {
  22. public Bond( BigDecimal price, BigDecimal period, BigDecimal couponRate, BigDecimal cpiRate )
  23. {
  24. this.price = price;
  25. this.period = period;
  26. this.couponRate = couponRate;
  27. this.cpiRate = cpiRate;
  28. }
  29.  
  30. protected BigDecimal price;
  31. public BigDecimal getPrice() { return price; }
  32. public void setPrice( BigDecimal price ) { this.price = price; }
  33.  
  34. protected BigDecimal period;
  35. public BigDecimal getPeriod() { return period; }
  36. public void setPeriod( BigDecimal period ) { this.period = period; }
  37.  
  38. protected BigDecimal couponRate;
  39. public BigDecimal getCouponRate() { return couponRate; }
  40. public void setCouponRate( BigDecimal couponRate ) { this.couponRate = couponRate; }
  41.  
  42. protected BigDecimal cpiRate;
  43. public BigDecimal getCpiRate() { return cpiRate; }
  44. public void setCpiRate( BigDecimal cpiRate ) { this.cpiRate = cpiRate; }
  45.  
  46. }
  47.  
  48. class ZeroCouponBond extends Bond
  49. {
  50. public ZeroCouponBond( BigDecimal price, BigDecimal period, BigDecimal couponRate, BigDecimal cpiRate )
  51. {
  52. super( price, period, couponRate, cpiRate );
  53. }
  54. }
  55.  
  56. class RegularBond extends Bond
  57. {
  58. public RegularBond( BigDecimal price, BigDecimal period, BigDecimal couponRate, BigDecimal cpiRate )
  59. {
  60. super( price, period, couponRate, cpiRate );
  61. }
  62. }
  63.  
  64. class RealReturnBond extends Bond
  65. {
  66. public RealReturnBond( BigDecimal price, BigDecimal period, BigDecimal couponRate, BigDecimal cpiRate )
  67. {
  68. super( price, period, couponRate, cpiRate );
  69. }
  70. }
  71.  
  72. abstract class Calculator
  73. {
  74. public static BigDecimal Calculate( IBond bond )
  75. {
  76. return new BigDecimal(0);
  77. }
  78.  
  79. protected static BigDecimal CalculateZeroCouponYield( IBond bond )
  80. {
  81. return bond.getPrice().divide( bond.getPeriod().multiply( bond.getPrice() ) );
  82. }
  83.  
  84. protected static BigDecimal CalculateRegularCouponYield( IBond bond )
  85. {
  86. return CalculateZeroCouponYield( bond ).add( bond.getCouponRate() );
  87. }
  88.  
  89. protected static BigDecimal CalculateRealReturnBondYield( IBond bond )
  90. {
  91. return CalculateRegularCouponYield( bond ).add( bond.getCpiRate() );
  92. }
  93. }
  94.  
  95. class ZeroCouponYieldCalculator extends Calculator
  96. {
  97. public static BigDecimal Calculate( IBond bond )
  98. {
  99. return CalculateZeroCouponYield( bond );
  100. }
  101. }
  102.  
  103. class RegularYieldCalculator extends Calculator
  104. {
  105. public static BigDecimal Calculate( IBond bond )
  106. {
  107. return CalculateRegularCouponYield( bond );
  108. }
  109. }
  110.  
  111. class RealReturnYieldCalculator extends Calculator
  112. {
  113. public static BigDecimal Calculate( IBond bond )
  114. {
  115. return CalculateRealReturnBondYield( bond );
  116. }
  117. }
  118.  
  119. class Solution {
  120. public static void main(String args[] ) throws Exception {
  121. Scanner scanner = new Scanner(System.in);
  122.  
  123. BigDecimal price = scanner.nextBigDecimal();
  124. BigDecimal period = scanner.nextBigDecimal();
  125. BigDecimal couponRate = scanner.nextBigDecimal();
  126. BigDecimal cpiRate = scanner.nextBigDecimal();
  127.  
  128. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  129. IBond zeroCouponBond = new ZeroCouponBond( price, period, couponRate, cpiRate );
  130. IBond regularBond = new RegularBond( price, period, couponRate, cpiRate );
  131. IBond realReturnBond = new RealReturnBond( price, period, couponRate, cpiRate );
  132. System.out.println( "price: " + price + " period: " + period + " couponrate: " + couponRate + " cpirate: " + cpiRate );
  133. System.out.println( ZeroCouponYieldCalculator.Calculate( zeroCouponBond ) + " " + RegularYieldCalculator.Calculate( regularBond ) + " " + RealReturnYieldCalculator.Calculate( realReturnBond ) );
  134. System.out.println(BigDecimal.valueOf(10.1234).setScale(2, BigDecimal.ROUND_HALF_UP));
  135. }
  136. }
Success #stdin #stdout 0.08s 2184192KB
stdin
80
10
0.050
0.010
stdout
price: 80 period: 10 couponrate: 0.050 cpirate: 0.010
0.1 0.150 0.160
10.12