Update OOP/Java/Lab/Week6/Account/AccountManagement.java

This commit is contained in:
Aadit Agrawal 2024-09-14 09:33:00 +05:30
parent fd70bcae47
commit ab8e519672

View File

@ -1,76 +1,103 @@
interface Sports { // Account class
int getNumberOfGoals(); class Account {
String dispTeam(); private String customerName;
} private int accountNumber;
private double balance;
private String accountType;
class Hockey implements Sports { public Account(String customerName, int accountNumber, double balance, String accountType) {
this.customerName = customerName;
private int goals; this.accountNumber = accountNumber;
private String team; this.balance = balance;
this.accountType = accountType;
public Hockey(int goals, String team) {
this.goals = goals;
this.team = team;
} }
public int getNumberOfGoals() { public void deposit(double amount) {
return goals; balance += amount;
} }
public String dispTeam() { public void withdraw(double amount) {
return team; balance -= amount;
}
public double getBalance() {
return balance;
}
public String getAccountType() {
return accountType;
} }
} }
class Football implements Sports { // SavingsAccount class
class SavingsAccount extends Account {
private double interestRate;
private int goals; public SavingsAccount(String customerName, int accountNumber, double balance, double interestRate) {
private String team; super(customerName, accountNumber, balance, "Savings");
this.interestRate = interestRate;
public Football(int goals, String team) {
this.goals = goals;
this.team = team;
} }
public int getNumberOfGoals() { public void computeInterest() {
return goals; double interest = balance * interestRate / 100;
deposit(interest);
} }
public String dispTeam() { public void withdraw(double amount) {
return team; if (amount > getBalance()) {
} System.out.println("Insufficient balance");
}
public class SportsMatch {
public static void main(String[] args) {
Hockey team1 = new Hockey(3, "India");
Hockey team2 = new Hockey(2, "England");
Football team3 = new Football(2, "AFC Richmond");
Football team4 = new Football(1, "West Ham United");
System.out.println("Hockey Match:");
displayWinner(team1, team2);
System.out.println("\nFootball Match:");
displayWinner(team3, team4);
}
public static void displayWinner(Sports team1, Sports team2) {
System.out.println(
team1.dispTeam() + " goals: " + team1.getNumberOfGoals()
);
System.out.println(
team2.dispTeam() + " goals: " + team2.getNumberOfGoals()
);
if (team1.getNumberOfGoals() > team2.getNumberOfGoals()) {
System.out.println("Winner: " + team1.dispTeam());
} else if (team2.getNumberOfGoals() > team1.getNumberOfGoals()) {
System.out.println("Winner: " + team2.dispTeam());
} else { } else {
System.out.println("It's a tie!"); super.withdraw(amount);
} }
} }
} }
// CurrentAccount class
class CurrentAccount extends Account {
private double minimumBalance;
private double serviceTax;
public CurrentAccount(String customerName, int accountNumber, double balance, double minimumBalance, double serviceTax) {
super(customerName, accountNumber, balance, "Current");
this.minimumBalance = minimumBalance;
this.serviceTax = serviceTax;
}
public void checkMinimumBalance() {
if (getBalance() < minimumBalance) {
withdraw(serviceTax);
System.out.println("Service tax imposed");
}
}
public void withdraw(double amount) {
if (amount > getBalance()) {
System.out.println("Insufficient balance");
} else {
super.withdraw(amount);
checkMinimumBalance();
}
}
}
// Main function
public class AccountManagement {
public static void main(String[] args) {
SavingsAccount saurabhAccount = new SavingsAccount("Saurabh", 1234, 10000, 5);
CurrentAccount akhilAccount = new CurrentAccount("Akhil", 5678, 5000, 1000, 500);
saurabhAccount.deposit(5000);
System.out.println("Saurabh's balance: " + saurabhAccount.getBalance());
saurabhAccount.computeInterest();
System.out.println("Saurabh's balance after interest: " + saurabhAccount.getBalance());
saurabhAccount.withdraw(2000);
System.out.println("Saurabh's balance after withdrawal: " + saurabhAccount.getBalance());
akhilAccount.deposit(2000);
System.out.println("Akhil's balance: " + akhilAccount.getBalance());
akhilAccount.withdraw(3000);
System.out.println("Akhil's balance after withdrawal: " + akhilAccount.getBalance());
akhilAccount.checkMinimumBalance();
System.out.println("Akhil's balance after checking minimum balance: " + akhilAccount.getBalance());
}
}