@Leo_Magalhaes escreveu:
Não faço a mínima ideia do que esteja acontecendo, por que meu objeto
Money
é adulterado com ofowarding
??? Olha que bizarro o resultado:valor de val: 1000.0
val no construtor AssetDebt: 1000.0
val no construtor Liabilitie: 28.571683207522636
val no objeto liabilitie: 28.571683207522636
valor da divida: 28.571683207522636Era para o valor de val ser o mesmo... Abaixo o código:
import java.text.NumberFormat; import java.text.DecimalFormat; import java.util.Locale; import java.util.ArrayList; class Teste { public static void main(String[] args) { ArrayList<LiabilitieInterface> personalDebt = new ArrayList<LiabilitieInterface>(); AssetDebt teste; Money val; for (int i=1; /*i<11* /i<2; i++) { StringBuilder sb = new StringBuilder(); sb.append("Divida #").append(i); String s = sb.toString(); val = new Money(i*1000); System.out.println("valor de val: " + val.getValue() + " "); teste = new AssetDebt(val, i*50, s); val = teste.getValue(); System.out.println("valor da divida: " + val.getValue()); //System.out.println(teste.getName() + ": " + teste.getValue().stringValue() + " em " + teste.getPeriods() + " vezes de " + teste.getMonthlyPay().stringValue()); personalDebt.add(teste); } } } interface LiabilitieInterface { String getName(); int getPeriods(); //OK int getPeriodsLeft(); //OK double getInterest(); //OK Money getValue(); //OK Money getMonthlyPay(); //ok boolean writeOff(Money writeoff, int p); //OK void writeOff(Money writeoff); //OK void periodPassWithPay(); void periodPassWithoutPay(); } class Liabilitie { private Money value; private Money monthlyPay; private double interest; void periodPassWithoutPay() { this.value.times(1 + this.interest); } void pay(Money pay) { this.value.sub(pay); } void pay() { this.value.sub(this.monthlyPay); } void periodPassWithPay() { periodPassWithoutPay(); pay(); } Money getValue() { return this.value; } Money getMonthlyPay() { return this.monthlyPay; } double getInterest() { return this.interest; } Liabilitie(Money val, Money mval, double d) { System.out.println("val no construtor Liabilitie: " + val.getValue()); this.value = val; System.out.println("val no objeto liabilitie: " + this.value.getValue()); this.monthlyPay = mval; this.interest = d; } } class AssetDebt implements LiabilitieInterface { private Liabilitie liabilitie; private String name; private int periods; private int periodsLeft; public double PERIODS_MAX = 120; public double DEFAULT_INTEREST = 0.015; public int minCreditPoints; public String getName() { return this.name; } public int getPeriods() { return this.periods; } public int getPeriodsLeft() { return this.periodsLeft; } public double getInterest() { return this.liabilitie.getInterest(); } public Money getValue() { return this.liabilitie.getValue(); } public Money getMonthlyPay() { return this.liabilitie.getMonthlyPay(); } public void writeOff(Money writeoff) { int p = this.periodsLeft; double d = this.evaluateInterest(/*double mktInterest*/); this.liabilitie.pay(writeoff); Money val = this.liabilitie.getValue(); Money mval = this.refinance(liabilitie.getValue(), p, d); this.liabilitie = new Liabilitie(val, mval, d); } public boolean writeOff(Money writeoff, int p) { boolean b = validPeriods(p); if (!b) return false; double d = this.evaluateInterest(/*double mktInterest*/); this.liabilitie.pay(writeoff); this.periods = p; Money val = this.liabilitie.getValue(); Money mval = this.refinance(liabilitie.getValue(), p, d); this.liabilitie = new Liabilitie(val, mval, d); return true; } public void periodPassWithPay() { this.liabilitie.periodPassWithPay(); } public void periodPassWithoutPay() { this.liabilitie.periodPassWithoutPay(); } private boolean validPeriods(int p) { if (p>0 && p <= this.PERIODS_MAX) return true; else return false; } private Money refinance(Money newval, int p, double d) { double CF = d / ( 1 - 1/( Math.pow((1+d),p))); newval.times(CF); return newval; } private double evaluateInterest () { return DEFAULT_INTEREST; } public AssetDebt (Money val, int p, String nam) { System.out.println("val no construtor AssetDebt: " + val.getValue()); double d = this.evaluateInterest(); Money mval = new Money(); mval = this.refinance(val, p, d); this.periods = this.periodsLeft = p; this.name = nam; this.liabilitie = new Liabilitie (val, mval , d); } } class Money { private double value; private String valueString; public Money(double x) { this.setValue(x); } private void setValue(double x) { this.value = x; this.stringValue(); } public double getValue() { return this.value; } public String stringValue() { Locale Brasil = new Locale("pt","BR"); NumberFormat currencyf = NumberFormat.getCurrencyInstance(Brasil); this.valueString = currencyf.format(this.value); return valueString; } public void add(Money x) { double y = this.value; double z = x.value; this.setValue(y+z); } public void sub(Money x) { double y = this.value; double z = x.value; this.setValue(y-z); } public void times(double x) { double y = this.value; this.setValue(x*y); } }
Mensagens: 3
Participantes: 1