Skip to content

Commit

Permalink
Merge pull request #5072 from IllianiCBT/loan_zeroDivision
Browse files Browse the repository at this point in the history
Fixed Divide by Zero Error in Loans Dialog
  • Loading branch information
HammerGS authored Oct 21, 2024
2 parents 1abf171 + 918f04b commit b2d982f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
22 changes: 12 additions & 10 deletions MekHQ/src/mekhq/campaign/finances/Loan.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
*/
package mekhq.campaign.finances;

import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.Objects;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import megamek.common.Compute;
import megamek.logging.MMLogger;
import mekhq.campaign.finances.enums.FinancialTerm;
import mekhq.campaign.finances.financialInstitutions.FinancialInstitutions;
import mekhq.utilities.MHQXMLUtility;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.Objects;

/**
* TODO : Update loan baseline based on latest Campaign Operations Rules
Expand Down Expand Up @@ -197,9 +196,13 @@ public void calculateAmortization() {
final double periodicRate = (getRate() / 100.0) / paymentsPerYear;

setRemainingPayments(numberOfPayments);
setPaymentAmount(getPrincipal()
if (periodicRate > 0) {
setPaymentAmount(getPrincipal()
.multipliedBy(periodicRate * Math.pow(1 + periodicRate, numberOfPayments))
.dividedBy(Math.pow(1 + periodicRate, numberOfPayments) - 1));
} else {
setPaymentAmount(getPrincipal().dividedBy(numberOfPayments));
}
}

public void paidLoan() {
Expand Down Expand Up @@ -388,8 +391,7 @@ public boolean equals(final Object other) {
return false;
} else if (this == other) {
return true;
} else if (other instanceof Loan) {
final Loan loan = (Loan) other;
} else if (other instanceof Loan loan) {
return getInstitution().equals(loan.getInstitution())
&& getReferenceNumber().equals(loan.getReferenceNumber())
&& getPrincipal().equals(loan.getPrincipal())
Expand Down
36 changes: 16 additions & 20 deletions MekHQ/src/mekhq/gui/dialog/NewLoanDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@
*/
package mekhq.gui.dialog;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ResourceBundle;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import megamek.client.ui.baseComponents.MMComboBox;
import megamek.client.ui.preferences.JWindowPreference;
import megamek.client.ui.preferences.PreferencesNode;
Expand All @@ -48,6 +30,20 @@
import mekhq.campaign.finances.Money;
import mekhq.campaign.finances.enums.FinancialTerm;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Objects;
import java.util.ResourceBundle;

/**
* @author Taharqa
*/
Expand Down Expand Up @@ -627,11 +623,11 @@ public void actionPerformed(ActionEvent e) {
@Override
public void stateChanged(ChangeEvent e) {
if (campaign.getCampaignOptions().isUseLoanLimits()) {
if (e.getSource() == sldInterest) {
if (Objects.equals(e.getSource(), sldInterest)) {
sldCollateral.removeChangeListener(this);
sldCollateral.setValue(Loan.recalculateCollateralFromInterest(rating, sldInterest.getValue()));
sldCollateral.addChangeListener(this);
} else if (e.getSource() == sldCollateral) {
} else if (Objects.equals(e.getSource(), sldCollateral)) {
sldInterest.removeChangeListener(this);
sldInterest.setValue(Loan.recalculateInterestFromCollateral(rating, sldCollateral.getValue()));
sldInterest.addChangeListener(this);
Expand Down

0 comments on commit b2d982f

Please sign in to comment.