● Доробити програму "Банківський облік". Користувач може відкривати депозит та брати гроші в кредит.
Java используя дебагін та тип даних.
инет неправильно
Ответы
import java.util.Scanner;
public class Bank {
private double balance;
private double interestRate;
private double loanInterestRate;
public Bank(double balance, double interestRate, double loanInterestRate) {
this.balance = balance;
this.interestRate = interestRate;
this.loanInterestRate = loanInterestRate;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited " + amount + " dollars. Your balance is now " + balance + " dollars.");
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn " + amount + " dollars. Your balance is now " + balance + " dollars.");
} else {
System.out.println("Insufficient funds. Your balance is " + balance + " dollars.");
}
}
public void calculateInterest(int years) {
double interest = balance * Math.pow(1 + interestRate, years) - balance;
System.out.println("Interest earned over " + years + " years: " + interest + " dollars.");
}
public void applyForLoan(double amount, int years) {
double interest = amount * loanInterestRate * years;
double totalAmount = amount + interest;
if (totalAmount <= balance * 0.5) {
balance += amount;
System.out.println("Loan of " + amount + " dollars approved. Your balance is now " + balance + " dollars.");
} else {
System.out.println("Loan application rejected. Total amount due is " + totalAmount + " dollars, which exceeds 50% of your balance.");
}
}
public static void main(String[] args) {
Bank bank = new Bank(1000, 0.05, 0.1);
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out.println("Enter 1 to deposit, 2 to withdraw, 3 to calculate interest, 4 to apply for loan, or 5 to quit:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter deposit amount:");
double depositAmount = scanner.nextDouble();
bank.deposit(depositAmount);
break;
case 2:
System.out.println("Enter withdrawal amount:");
double withdrawalAmount = scanner.nextDouble();
bank.withdraw(withdrawalAmount);
break;
case 3:
System.out.println("Enter number of years:");
int years = scanner.nextInt();
bank.calculateInterest(years);
break;
case 4:
System.out.println("Enter loan amount:");
double loanAmount = scanner.nextDouble();
System.out.println("Enter number of years:");
years = scanner.nextInt();
bank.applyForLoan(loanAmount, years);
break;
case 5:
quit = true;
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}