Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
package MyBank;

public interface PrintDetails {
    void printDetails();
}


home 

package MyBank;

public class Home extends Loan implements PrintDetails {
    public Home(double costPerMonth) {
        super(costPerMonth);
    }

    @Override
    public void calculateLoan() {
        // Perform calculations specific to home loan
    }

    @Override
    public void printDetails() {
        System.out.println("Type: Home");
        System.out.println("Cost per month: $" + getCostPerMonth());
    }
}


car



package MyBank;

public class Car extends Loan implements PrintDetails {
    public Car(double costPerMonth) {
        super(costPerMonth);
    }

    @Override
    public void calculateLoan() {
        // Perform calculations specific to car loan
    }

    @Override
    public void printDetails() {
        System.out.println("Type: Car");
        System.out.println("Cost per month: $" + getCostPerMonth());
    }
}



loan 



package UseLoan;

import MyBank.*;

import java.util.Scanner;

public class LoanTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter loan type (home/car): ");
        String loanType = scanner.nextLine();

        Loan loan;

        if (loanType.equalsIgnoreCase("home")) {
            System.out.print("Enter cost per month for home loan: ");
            double costPerMonth = scanner.nextDouble();
            loan = new Home(costPerMonth);
        } else if (loanType.equalsIgnoreCase("car")) {
            System.out.print("Enter cost per month for car loan: ");
            double costPerMonth = scanner.nextDouble();
            loan = new Car(costPerMonth);
        } else {
            System.out.println("Invalid loan type!");
            return;
        }

        loan.calculateLoan();
        loan.printDetails();
    }
}