我正在努力做继承的事情,但没有一个会起作用

时间:2014-12-09 22:22:15

标签: java eclipse inheritance

我试图从贷款中获得不同的变量,但他们都没有得到loan.java。我的继承来自Loan.Java> BusinessLoan.java> CreateLoan.java。我可以从CreateLoan获取一个变量来显示在业务中但是当我在业务中设置它时我无法抓住它。而且我知道其中一些东西是愚蠢的,但这是我的决定,所以有些东西是必需的。继承我的代码

Loan.java

package Construction;

public class Loan implements LoanConstant{
    public static int loanNumber;
    public static String lastName;
    public static int loanAmount;
    public static int interestRate;
    public static int term;
    public int primeRate;

    public int getLoanNumber() { return loanNumber; }
    public void setLoanNumber(int n) { n = loanNumber; }

    public String getLastName() { return lastName; }
    public void setLastName(String s) { s = lastName; }

    public int getLoanAmount() { return loanAmount; }
    public void setLoanAmount(int n) {
        n = loanAmount; 
        if (loanAmount > MAX_LOAN_AMOUNT)
            loanAmount = MAX_LOAN_AMOUNT;
        }

    public int getTerm() { return term; }
    public void setTerm(int n) { 
        n = term; 
        if (term == 1) {
            term = SHORT_TERM;
        } else if (term == 3) {
            term = MEDIUM_TERM;
        } else if(term == 5) {
            term = LONG_TERM;
        } else
            term = SHORT_TERM;
    }

    public int getInterestRate() { return interestRate; }
    public void setInterestRate(int i) { i = interestRate; }

    public static void displayAll() {
        System.out.println("The Company's Name is " + COMPANY_NAME);
        System.out.println("The loan number is " + loanNumber);
        System.out.println("The last name on the loan is " + lastName);
        System.out.println("The loan amount is " + loanAmount);
        System.out.println("The interest rate on the loan is " + interestRate);
        System.out.println("The term on the account is " + term);
    }
}

PersonalLoan.java

package Construction;

public class PersonalLoan extends Loan{

    public PersonalLoan(int ln, String last, int la, int term) {
        setLoanNumber(ln);
        setLastName(last);
        setLoanAmount(la);
        setTerm(term);
        interestRate = (int)((primeRate * 0.02) + primeRate);
        setInterestRate(interestRate);
    }
}

BusinessLoan.java

package Construction;

public class BusinessLoan extends Loan{

    public BusinessLoan(int ln, String last, int la, int term) {
        setLoanNumber(ln);
        setLastName(last);
        setLoanAmount(la);
        setTerm(term);
        interestRate = (int)((primeRate * 0.01) + primeRate);
        setInterestRate(interestRate);
    }

}

CreateLoan.java

package Construction;

import java.util.Scanner;


public class CreateLoan {

    public static void main(String[] args) {
        int x = 0;
        int primeRate;
        String type;
        Scanner input = new Scanner(System.in);
        Loan[] loans = new Loan[5];
        System.out.println("Please enter the prime interest rate");
        primeRate = input.nextInt();
        primeRate = primeRate/100;
        input.nextLine();

        for(x = 0; x < 6; ++x) {
            System.out.println("Please enter a loan type. Choose either Business or Personal. If you don't type it like that you'll get an error.");
            type = input.nextLine();
            if (type.equalsIgnoreCase("Business")) {
                System.out.println("What is the account number on the loan?");
                int ln = input.nextInt();
                System.out.println("What is the last name on the account?");
                String last = input.nextLine();
                input.nextLine();
                System.out.println("What is the loan amount? If you put more then 100k it'll only accept up to 100k");
                int la = input.nextInt();
                System.out.println("What is the term on the account? If you enter something other then 1, 3, or 5 it will default to a short term.");
                int term = input.nextInt();
                loans[x] = new BusinessLoan(ln, last, la, term);
                System.out.println("The Company's Name is " + Loan.COMPANY_NAME);
                System.out.println("The loan number is " + loans[x].getLoanNumber());
                System.out.println("The last name on the loan is " + loans[x].getLastName());
                System.out.println("The loan amount is " + loans[x].getLoanAmount());
                System.out.println("The interest rate on the loan is " + loans[x].getInterestRate());
                System.out.println("The term on the account is " + loans[x].getTerm());
            }
            else if (type.equalsIgnoreCase("Personal")) {
                System.out.println("What is the account number on the loan?");
                int ln = input.nextInt();
                System.out.println("What is the last name on the account?");
                String last = input.nextLine();
                input.nextLine();
                System.out.println("What is the loan amount? If you put more then 100k it'll only accept up to 100k");
                int la = input.nextInt();
                System.out.println("What is the term on the account? If you enter something other then 1, 3, or 5 it will default to a short term.");
                int term = input.nextInt();
                loans[x] = new PersonalLoan(ln, last, la, term);
                System.out.println("The Company's Name is " + Loan.COMPANY_NAME);
                System.out.println("The loan number is " + loans[x].getLoanNumber());
                System.out.println("The last name on the loan is " + loans[x].getLastName());
                System.out.println("The loan amount is " + loans[x].getLoanAmount());
                System.out.println("The interest rate on the loan is " + loans[x].getInterestRate());
                System.out.println("The term on the account is " + loans[x].getTerm());
            } else {
                System.out.println("You've entered an invalid type. Please restart and try again.");
                System.exit(0);
            }
        }
    }
}

LoanConstants.java

package Construction;

public interface LoanConstant {
    public final static int SHORT_TERM = 1;
    public final static int MEDIUM_TERM = 3;
    public final static int LONG_TERM = 5;
    public final static String COMPANY_NAME = "Sanchez Construction";
    public final static int MAX_LOAN_AMOUNT = 100000;
}

1 个答案:

答案 0 :(得分:2)

除了Loan字段是静态的(删除静态)。您还应该更新您的二传手。

public void setLoanNumber(int n) { n = loanNumber; }
public void setLastName(String s) { s = lastName; }

您要将值分配给传入的变量(而不是字段)。应该是

public void setLoanNumber(int n) { loanNumber = n; }
public void setLastName(String s) { lastName = s; }

public void setTerm(int n) { 
    // n = term; 
    if (n == 1) {
        term = SHORT_TERM;
    } else if (n == 3) {
        term = MEDIUM_TERM;
    } else if (n == 5) {
        term = LONG_TERM;
    } else
        term = SHORT_TERM;
}

public void setInterestRate(int i) { interestRate = i; }