前提
現在二つのパッケージを用いて、コンストラクタBondの引数couponが0の時とそれ以外の時でBondTypeの文字列を分岐させることに取り組んでいます。そこでパッケージtest1のBondTypeのgetter内で分岐をするようにコードを書いたのですが、うまく分岐ができているか確認するため、パッケージtest2のBondTestでgetBondType()の部分を出力させたいのですが、出力の仕方でつまづいている状況です。
getterの出力方法についてご教授いただけないでしょうか。enum型を使うのが今回が初めてで使用方法が間違っていれば加えてご教授いただきたいです。よろしくお願い致します。
ディレクトリは下記の通りです。
Stage
・test1→・Bond.java
・Bondtype.java
・test2→・BondTest.java
実現したいこと
該当のソースコード
Bond.java
package test1; import java.util.Objects; public class Bond { private String code; private String name; private int maturity; private double coupon; private String BondType; public Bond (String code, String name, int maturity, double coupon) { this.code = code; this.name = name; this.maturity = maturity; this.coupon = coupon; // チェック処理 if (code == null || name == null) { throw new IllegalArgumentException("nullです。"); } if (maturity < 20000101 || 29991231 < maturity || coupon < 0) { throw new IllegalArgumentException("数値が範囲外です"); } } //getter public String getCode() { return this.code; } public String getName() { return this.name; } public int getMaturity() { return this.maturity; } public double getCoupon() { return this.coupon; } public String getBondType() { if (coupon == 0) { BondType.equals("ZERO_COUPON_BOND"); } else { BondType.equals("COUPON_BOND"); } return this.BondType; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(code); builder.append(name); builder.append(maturity); builder.append(coupon); builder.append(BondType); return builder.toString(); } //equalsメソッド @Override public boolean equals (Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Bond other =(Bond)obj; if (this.code != other.code) { return false; } if (maturity == 0) { if (other.maturity != 0) { return false; } } else if (maturity != other.maturity) { return false; } if (Double.compare(other.coupon,coupon) != 0) { return false; } return true; } //hashCode @Override public int hashCode() { return Objects.hash(this.code, this.maturity, this.coupon); } }
Bondtype.java
package test1; public enum BondType { COUPON_BOND, ZERO_COUPON_BOND };
BondTest.java
package test2; public class BondTest { public static void main (String [] args) { test1.Bond bond3 = new test1.Bond("code", "name", 20220812, 1.0); test1.BondType bondType = new test1.BondType(); bondType.show(); } }
試したこと
BondTestファイルに
・test1.BondType bondType = new test1.BondType();
bondType.show();
・System.out.println(test1.builder);
などでコンパイルしたが、シンボルが得られず出力できなかった。

0 コメント