简体   繁体   English

使用对象将方法调用到Main中

[英]Calling methods into Main with objects

I am writing a program that allows the user to create as many loans as they want, store the loans in an array and then later do stuff to those loans, such as amortization and make payment. 我正在编写一个程序,该程序允许用户创建所需数量的贷款,将贷款存储在阵列中,然后再对这些贷款进行处理,例如摊销和付款。 My problem is that I can't find a way to remove redundant code in Main. 我的问题是我找不到在Main中删除冗余代码的方法。 If I create a method to "getLoanInfo" then my HomeLoan objects do not exist in Main. 如果我创建“ getLoanInfo”方法,则我的HomeLoan对象在Main中不存在。 How can I remove the redundancy? 如何删除冗余? I will eventually loop the code so that the user can go back to the beginning and pick a type of loan again and add more if they want. 我最终将循环代码,以便用户可以回到开始并再次选择一种贷款,并根据需要添加更多。

public static void main(String[] args) throws IOException {

System.out.println("Welcome to the Loan system!  \n");
System.out.println("What type of Loan do you want? ");
System.out.println("Please enter 'H' for Home or 'C' for Car: ");

for(int i = 0; i < 5; i++){ 

char c = scan.next().charAt(0);

if(c == 'H'){

    System.out.println("\nHow many Home Loans would you like? ");
    HomeCount = scan.nextInt();

    for(int j = 0; j < HomeCount; j++){

    System.out.println("Please enter your Name: ");
    String nameOf = scan.next();

    System.out.println("Please enter an ID Number: ");
    String id_Number = scan.next();
    System.out.println("Please enter a Loan Number: ");
    String loan_Number = scan.next();
    System.out.println("Please enter the amount of the Loan: ");
    double loan_Amount = scan.nextDouble();

    System.out.println("Please enter your desired Interest Rate: ");
    double interest_rate = scan.nextDouble();
    while(interest_rate > 100.00){ //Check valid number
    System.out.println("Invalid percentage. Please enter 0.00 -    100.00");
    interest_rate = scan.nextDouble();
    }
    System.out.println("How long will you like the loan? Please enter 1-30.");
    int term = scan.nextInt();
    while(term > 30){ // Check for valid number
    System.out.println("Invalid number of years. Please enter 1-30.");
    term = scan.nextInt();
    }
    System.out.println("Current balance is being set to the Loan amount: ");
    double c_balance = loan_Amount;
    System.out.println("Please enter an address for the Home: ");
    String address = scan.next();

    HL[j] = new HomeLoan(nameOf,id_Number,loan_Number,loan_Amount,
            interest_rate,term,c_balance,address);

    }
    new TestingClasses().getInfo(); //was trying a getInfo method

    System.out.println("Would you like to view your loan? ");
    char x = scan.next().charAt(0);

    if( x == 'Y'){
    System.out.println(HL); //When using getInfo method HL is not in scope
    }
    }

    if(c == 'C'){

    System.out.println("\nHow many Car Loans would you like? ");
    CarCount = scan.nextInt();

    CarLoan[] CL = new CarLoan[CarCount];

    for(int j = 1; j < CarCount; j++){ //Loop through loans

    System.out.println("Please enter your Name: ");
    String name = scan.next();
    System.out.println("Please enter an ID Number: ");
    String id_number = scan.next();
    System.out.println("Please enter a Loan Number: ");
    String loan_number = scan.next();
    System.out.println("Please enter the amount of the Loan: ");
    double loan_amount = scan.nextDouble();

    System.out.println("Please enter your desired Interest Rate: ");
    double interest_rate = scan.nextDouble();
    while(interest_rate > 100.00){ //Check valid number
    System.out.println("Invalid percentage. Please enter 0.00 - 100.00");
    interest_rate = scan.nextDouble();
    }
    System.out.println("How long will you like the loan? Please enter 1-30.");
    int term = scan.nextInt();
    while(term > 30){ // Check for valid number
    System.out.println("Invalid number of years. Please enter 1-30.");
    term = scan.nextInt();
    }
    System.out.println("Current balance is being set to the Loan amount. ");
    double c_balance = loan_amount;
    System.out.println("Please enter the Make of the car: ");
    String make = scan.next();
    System.out.println("Please enter the Model of the car: ");
    String model = scan.next();
    System.out.println("Please enter the Year of the car: ");
    String year = scan.next();
    CL[j] = new  
            CarLoan(name,id_number,loan_number,loan_amount,interest_rate,
            term,c_balance,make,model,year);

    }

    }

If I create a method to "getLoanInfo" then my HomeLoan objects do not exist in Main. 如果我创建“ getLoanInfo”方法,则我的HomeLoan对象在Main中不存在。

Why can't you do this 你为什么不能这样做

public static void main(String[] args){
    HomeLoan homeLoan = new HomeLoan();

    getLoanInfo(homeLoan);
}

public static void getHomeInfo(HomeLoan homeloan){
    ... // get loan info
}

Just add a HomeLoan arg to the method 只需将HomeLoan arg添加到方法中

There are many ways. 有很多方法。 Below is one such way. 下面是一种这样的方式。 For sample, I put everything in one class. 作为示例,我将所有内容都放在一个类中。 You can consider this, as one of the option to eliminate redundancy. 您可以将其视为消除冗余的一种方法。

import java.io.IOException;
import java.util.Scanner;
abstract class Loan
{

};
class HomeLoan extends Loan
{
    String loan_Number;
    public HomeLoan(String nameOf, String id_Number, String loan_Number,
            double loan_Amount, double interest_rate, int term,
            double c_balance, String address) {
        // TODO Auto-generated constructor stub
        this.loan_Number=loan_Number;
    }
    public String getHomeLoanNumber()
    {
        return "THIS IS HomeLoan Object:"+loan_Number;
    }
}
class CarLoan extends Loan
{
    String loan_Number;
    public CarLoan(String name, String id_number, String loan_number,
            double loan_amount, double interest_rate, int term,
            double c_balance, String make, String model, String year) {
        this.loan_Number=loan_number;
    }
    public String getCarLoanNumber()
    {
        return "THIS IS CarLoan Object:"+loan_Number;
    }
}
public class Test {



 private static Loan getLoanInfo(Scanner scan,char c)
 {

      System.out.println("Please enter your Name: ");
        String nameOf = scan.next();

        System.out.println("Please enter an ID Number: ");
        String id_Number = scan.next();
        System.out.println("Please enter a Loan Number: ");
        String loan_Number = scan.next();
        System.out.println("Please enter the amount of the Loan: ");
        double loan_Amount = scan.nextDouble();

        System.out.println("Please enter your desired Interest Rate: ");
        double interest_rate = scan.nextDouble();
        while(interest_rate > 100.00){ //Check valid number
        System.out.println("Invalid percentage. Please enter 0.00 -    100.00");
        interest_rate = scan.nextDouble();
        }
        System.out.println("How long will you like the loan? Please enter 1-30.");
        int term = scan.nextInt();
        while(term > 30){ // Check for valid number
        System.out.println("Invalid number of years. Please enter 1-30.");
        term = scan.nextInt();
        }
        System.out.println("Current balance is being set to the Loan amount: ");
        double c_balance = loan_Amount;

        if(c=='H')
        {
             System.out.println("Please enter an address for the Home: ");
                String address = scan.next();

             return new HomeLoan(nameOf,id_Number,loan_Number,loan_Amount,
                        interest_rate,term,c_balance,address);
        }
        else if(c=='C')
        {
              System.out.println("Please enter the Make of the car: ");
                String make = scan.next();
                System.out.println("Please enter the Model of the car: ");
                String model = scan.next();
                System.out.println("Please enter the Year of the car: ");
                String year = scan.next();
                return new CarLoan(nameOf,id_Number,loan_Number,loan_Amount,interest_rate,
                        term,c_balance,make,model,year);
        }
        else
        return null;
 }
public static void main(String[] args) throws IOException {

    int HomeCount,CarCount;
    HomeLoan HL[]=null;
    CarLoan CL[];
    System.out.println("Welcome to the Loan system!  \n");
    System.out.println("What type of Loan do you want? ");
    System.out.println("Please enter 'H' for Home or 'C' for Car: ");
    Scanner scan=new Scanner(System.in);
    for(int i = 0; i < 5; i++){ 

    char c = scan.next().charAt(0);

    if(c == 'H'){

        System.out.println("\nHow many Home Loans would you like? ");
        HomeCount = scan.nextInt();
        HL= new HomeLoan[HomeCount];
        for(int j = 0; j < HomeCount; j++){

            HL[j]=(HomeLoan)getLoanInfo(scan,c);
            System.out.println(HL[j].getHomeLoanNumber());

        }

        System.out.println("Would you like to view your loan? ");
        char x = scan.next().charAt(0);

        if( x == 'Y'){
            System.out.println(HL); //When using getInfo method HL is not in scope
        }
        }

        if(c == 'C'){

        System.out.println("\nHow many Car Loans would you like? ");
        CarCount = scan.nextInt();

        CL = new CarLoan[CarCount];

        for(int j = 0; j < CarCount; j++){ //Loop through loans

            CL[j]=(CarLoan)getLoanInfo(scan,c);
            System.out.println(CL[j].getCarLoanNumber());

        }

        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM