将变量转换为bigDecimal

时间:2016-01-06 19:48:25

标签: java xml jaxb

我正在尝试对工作中的一个非常复杂的程序进行一些更改。该程序使用JAXB,并具有许多不同的类和功能。在它的核心,目的是允许用户创建表示基于卡的事务的XML文件。现在,用户可以输入金额,然后将金额输入最终的XML中。我需要扩展这一点,同时让用户能够将事务分成多个部分。这意味着我需要能够更改金额字段。对我来说不幸的是,作者设计金额字段的方式有点混乱,所以我希望有人可以解释一下。有大量的代码,我无法粘贴所有代码,但我会尽力展示问题。

我正在使用的代码的主要部分是:

while(allocationCounter>0){
            StructuredRemittanceInformation9 Strd = objectFactory
                    .createStructuredRemittanceInformation9(); // create Strd
            ReferredDocumentInformation3 RfrdDocInf = objectFactory
                    .createReferredDocumentInformation3(); // create RfrdDocInf
            ReferredDocumentType2 tpdoc = objectFactory
                    .createReferredDocumentType2(); // create tp
            ReferredDocumentType1Choice cdOrPrtry = objectFactory
                    .createReferredDocumentType1Choice(); // create CdOrPrtry
            cdOrPrtry.setCd(DocumentType5Code.SOAC);
            tpdoc.setCdOrPrtry(cdOrPrtry); // set CdorPrtry
            RfrdDocInf.setTp(tpdoc); // set Tp
            RfrdDocInf.setNb("0001");
            Strd.getRfrdDocInf().add(RfrdDocInf); // set RfrdDocInf

            RemittanceAmount2 RfrdDocAmt = objectFactory.createRemittanceAmount2(); // create
                                                                                    // RfrdDocAmt
            DocumentAdjustment1 adjstmntAmt = objectFactory
                    .createDocumentAdjustment1(); // create AdjstmntAmtAndRsn
            ActiveOrHistoricCurrencyAndAmount adjAmt = objectFactory
                    .createActiveOrHistoricCurrencyAndAmount(); // create Amt


            adjAmt.setCcy("USD");
            adjAmt.setValue((BigDecimal) transInfo.get("amount"));      
            adjstmntAmt.setAmt(adjAmt); // set Amt
            adjstmntAmt.setCdtDbtInd(CreditDebitCode.DBIT); // set CdtDbtInd
            adjstmntAmt.setAddtlInf(transInfo.get("dbtrName").toString()); // set
                                                                            // AddtlInf
            RfrdDocAmt.getAdjstmntAmtAndRsn().add(adjstmntAmt); // set
                                                                // AdjstmntAmtAndRsn
            Strd.setRfrdDocAmt(RfrdDocAmt); // set RfrdDocAmt

            CreditorReferenceInformation2 cdtrRefInf = objectFactory
                    .createCreditorReferenceInformation2(); // create CdtrRefInf
            CreditorReferenceType2 cdtrTp = objectFactory
                    .createCreditorReferenceType2(); // create Tp
            CreditorReferenceType1Choice CdorPrtryTp = objectFactory
                    .createCreditorReferenceType1Choice(); // create cdPrtry
            CdorPrtryTp.setPrtry("BNR");
            cdtrTp.setCdOrPrtry(CdorPrtryTp);// set CdPrtry
            cdtrRefInf.setTp(cdtrTp); // set Tp
            cdtrRefInf.setRef(transInfo.get("referenceNumber").toString());
            Strd.setCdtrRefInf(cdtrRefInf);// set CdtrRefInf

            rmtInf.getStrd().add(Strd); // set strd

            System.out.println("TEST PASSED");
            allocationCounter--;
            }

特别是这行代码

adjstmntAmt.setAmt(adjAmt); // set Amt

这就是设定金额的原因,而这正是我需要改变的。这个数量链接回几个类,但从这里开始:

transactionInfo.put("amount", amount);

在该类中,用户将金额放入文本字​​段,然后保存 我在上面发布的adjstmntAmt(adjAmt)在另一个类中引用: 进入transactionInfo哈希并稍后调用。

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.07.01 at 10:11:45 AM EDT 
//


package jaxBClasses;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * <p>Java class for ActiveOrHistoricCurrencyAndAmount complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="ActiveOrHistoricCurrencyAndAmount">
 *   &lt;simpleContent>
 *     &lt;extension base="&lt;urn:iso:std:iso:20022:tech:xsd:pain.008.001.04>ActiveOrHistoricCurrencyAndAmount_SimpleType">
 *       &lt;attribute name="Ccy" use="required" type="{urn:iso:std:iso:20022:tech:xsd:pain.008.001.04}ActiveOrHistoricCurrencyCode" />
 *     &lt;/extension>
 *   &lt;/simpleContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ActiveOrHistoricCurrencyAndAmount", propOrder = {
    "value"
})
public class ActiveOrHistoricCurrencyAndAmount {

    @XmlValue
    protected BigDecimal value;
    @XmlAttribute(name = "Ccy", required = true)
    protected String ccy;

    /**
     * Gets the value of the value property.
     * 
     * @return
     *     possible object is
     *     {@link BigDecimal }
     *     
     */
    public BigDecimal getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigDecimal }
     *     
     */
    public void setValue(BigDecimal value) {
        this.value = value;
    }

    /**
     * Gets the value of the ccy property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCcy() {
        return ccy;
    }

    /**
     * Sets the value of the ccy property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCcy(String value) {
        this.ccy = value;
    }

}

它也出现在这个课程中:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.07.01 at 10:11:45 AM EDT 
//


package jaxBClasses;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for DocumentAdjustment1 complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="DocumentAdjustment1">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Amt" type="{urn:iso:std:iso:20022:tech:xsd:pain.008.001.04}ActiveOrHistoricCurrencyAndAmount"/>
 *         &lt;element name="CdtDbtInd" type="{urn:iso:std:iso:20022:tech:xsd:pain.008.001.04}CreditDebitCode" minOccurs="0"/>
 *         &lt;element name="Rsn" type="{urn:iso:std:iso:20022:tech:xsd:pain.008.001.04}Max4Text" minOccurs="0"/>
 *         &lt;element name="AddtlInf" type="{urn:iso:std:iso:20022:tech:xsd:pain.008.001.04}Max140Text" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DocumentAdjustment1", propOrder = {
    "amt",
    "cdtDbtInd",
    "rsn",
    "addtlInf"
})
public class DocumentAdjustment1 {

    @XmlElement(name = "Amt", required = true)
    protected ActiveOrHistoricCurrencyAndAmount amt;
    @XmlElement(name = "CdtDbtInd")
    protected CreditDebitCode cdtDbtInd;
    @XmlElement(name = "Rsn")
    protected String rsn;
    @XmlElement(name = "AddtlInf")
    protected String addtlInf;

    /**
     * Gets the value of the amt property.
     * 
     * @return
     *     possible object is
     *     {@link ActiveOrHistoricCurrencyAndAmount }
     *     
     */
    public ActiveOrHistoricCurrencyAndAmount getAmt() {
        return amt;
    }

    /**
     * Sets the value of the amt property.
     * 
     * @param value
     *     allowed object is
     *     {@link ActiveOrHistoricCurrencyAndAmount }
     *     
     */
    public void setAmt(ActiveOrHistoricCurrencyAndAmount value) {

        this.amt = value;
    }

    /**
     * Gets the value of the cdtDbtInd property.
     * 
     * @return
     *     possible object is
     *     {@link CreditDebitCode }
     *     
     */
    public CreditDebitCode getCdtDbtInd() {
        return cdtDbtInd;
    }

    /**
     * Sets the value of the cdtDbtInd property.
     * 
     * @param value
     *     allowed object is
     *     {@link CreditDebitCode }
     *     
     */
    public void setCdtDbtInd(CreditDebitCode value) {
        this.cdtDbtInd = value;
    }

    /**
     * Gets the value of the rsn property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getRsn() {
        return rsn;
    }

    /**
     * Sets the value of the rsn property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setRsn(String value) {
        this.rsn = value;
    }

    /**
     * Gets the value of the addtlInf property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAddtlInf() {
        return addtlInf;
    }

    /**
     * Sets the value of the addtlInf property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAddtlInf(String value) {
        this.addtlInf = value;
    }

}

所以我的问题很简单:我需要做些什么来使我能够将任何变量放在那行代码中:

adjstmntAmt.setAmt(adjAmt); // set Amt

我尝试用自己的变量替换adjAmt但是我得到一个错误,说明类型必须是ActiveOrHistoricCurrencyAndAmount ..

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我终于找到了这个问题的答案。实际解决方案以此字段的形式出现

adjAmt.setValue((BigDecimal) transInfo.get("amount"));  

我可以用我想要的数量替换括号中的数量。这个金额实际上与另一个我没有在原始问题中发布的课程有关,其中&#34;金额&#34;实际上是在JTextField中设置然后保存到HashMap。