Experiment 7
import [Link];
import [Link];
class XYZBank {
private double balance;
private final Lock lock = new ReentrantLock();
public XYZBank(double initialBalance) {
[Link] = initialBalance;
}
public void deposit(double amount) {
[Link]();
try {
balance += amount;
[Link]([Link]().getName() + " deposited: " +
amount + ", New balance: " + balance);
} finally {
[Link]();
}
}
public void withdraw(double amount) {
[Link]();
try {
if (balance >= amount) {
balance -= amount;
[Link]([Link]().getName() + " withdrew: " +
amount + ", New balance: " + balance);
} else {
[Link]([Link]().getName() + " attempted to
withdraw: " + amount + " but insufficient balance. Current balance: " + balance);
}
} finally {
[Link]();
}
}
}
class DepositThread extends Thread {
private final XYZBank bank;
private final double amount;
public DepositThread(XYZBank bank, double amount) {
[Link] = bank;
[Link] = amount;
}
public void run() {
[Link](amount);
}
}
class WithdrawThread extends Thread {
private final XYZBank bank;
private final double amount;
public WithdrawThread(XYZBank bank, double amount) {
[Link] = bank;
[Link] = amount;
}
public void run() {
[Link](amount);
}
}
public class Main {
public static void main(String[] args) {
XYZBank bank = new XYZBank(1000);
Thread depositThread1 = new DepositThread(bank, 200);
Thread depositThread2 = new DepositThread(bank, 300);
Thread withdrawThread1 = new WithdrawThread(bank, 400);
Thread withdrawThread2 = new WithdrawThread(bank, 500);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Experiment 8
Create the Student Model Class
import [Link];
import [Link];
class Employee {
private String name;
private String gender;
public Employee(String name, String gender) {
[Link] = name;
[Link] = gender;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
}
public class Main {
public static void main(String[] args) {
List<Employee> employees = [Link](
new Employee("Alice", "Female"),
new Employee("Bob", "Male"),
new Employee("Charlie", "Male"),
new Employee("Diana", "Female")
);
[Link](employee ->
[Link]("Name: " + [Link]() + ", Gender: " +
[Link]())
);
}
}
Output
Name: Alice, Gender: Female
Name: Bob, Gender: Male
Name: Charlie, Gender: Male
Name: Diana, Gender: Female
Experiment 9
public class Student {
private String name;
private String course;
private int rollNumber;
public Student(String name, String course, int rollNumber) {
[Link] = name;
[Link] = course;
[Link] = rollNumber;
}
public String getName() {
return name;
}
public String getCourse() {
return course;
}
public int getRollNumber() {
return rollNumber;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", rollNumber=" + rollNumber +
'}';
}
}
Create the Spring XML Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<!-- Bean definition for Student -->
<bean id="student" class="Student">
<constructor-arg value="Alice"/>
<constructor-arg value="Computer Science"/>
<constructor-arg value="101"/>
</bean>
</beans>
Create the Main Application Class
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("[Link]");
Student student = (Student) [Link]("student");
[Link](student);
}
}
Input
<bean id="student" class="Student">
<constructor-arg value="Alice"/>
<constructor-arg value="Computer Science"/>
<constructor-arg value="101"/>
</bean>
Output
Student{name='Alice', course='Computer Science', rollNumber=101}
Experiment 10
Create the Student Model Class
import [Link];
import [Link];
import [Link];
import [Link];
@Component
public class Student {
private String name;
private String course;
private int rollNumber;
public Student() {
}
public Student(String name, String course, int rollNumber) {
[Link] = name;
[Link] = course;
[Link] = rollNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
[Link] = course;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
[Link] = rollNumber;
}
@PostConstruct
public void init() {
[Link]("Init method called at: " + [Link]());
}
@PreDestroy
public void destroy() {
[Link]("Destroy method called at: " + [Link]());
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", rollNumber=" + rollNumber +
'}';
}
}
Create the Configuration Class
import [Link];
import [Link];
import [Link];
@Configuration
@ComponentScan(basePackages = "[Link]")
public class AppConfig {
@Bean
public Student student() {
return new Student("Alice", "Computer Science", 101);
}
}
Create the Main Application Class
import [Link];
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext([Link]);
Student student = [Link]([Link]);
[Link](student);
[Link]();
}
}
Output
Init method called at: 2024-07-15T[Link].789
Student{name='Alice', course='Computer Science', rollNumber=101}
Destroy method called at: 2024-07-15T[Link].789
Experiment 11
Create the Student Controller
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@RestController
@RequestMapping("/student")
public class StudentController {
@Value("${[Link]:John Doe}")
private String studentName;
@Value("${[Link] Default St, Default City}")
private String studentAddress;
private final ApplicationContext applicationContext;
public StudentController(ApplicationContext applicationContext) {
[Link] = applicationContext;
}
@GetMapping("/name")
public String getStudentName() {
return studentName;
}
@GetMapping("/address")
public String getStudentAddress() {
return studentAddress;
}
@GetMapping("/beans")
public String[] getBeans() {
return [Link]();
}
}
Create the Main Application Class
package [Link];
import [Link];
import [Link];
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link], args);
}
}
Experiment 12
Create the Base Class MyPaymentServices
public abstract class MyPaymentServices {
protected double balance;
protected int customerId;
public MyPaymentServices(double balance, int customerId) {
[Link] = balance;
[Link] = customerId;
}
public void setBalance(double balance) {
[Link] = balance;
}
public double getBalance() {
return balance;
}
public int getCustomerId() {
return customerId;
}
public abstract void payBill(double amount);
}
[Link]
public class ShoppingPayment extends MyPaymentServices {
private static int counter = 1000;
private String paymentId;
public ShoppingPayment(double balance, int customerId) {
super(balance, customerId);
}
public String getPaymentId() {
return paymentId;
}
@Override
public void payBill(double amount) {
if (amount != balance) {
[Link]("Error: The amount paid does not match the balance due.");
return;
}
counter++;
paymentId = "S" + counter;
[Link]("Congratulations: You have successfully made a payment of
Rs. " + amount + ". Payment details are:-");
[Link]("Customer Id: " + customerId);
[Link]("Payment Id: " + paymentId);
[Link]("Previous Due: " + balance);
[Link]("Remaining Due: 0");
}
}
[Link]
public class CreditCardPayment extends MyPaymentServices {
private static int counter = 1000;
private String paymentId;
private double cashBack = 0;
private double balanceDue = 0;
public CreditCardPayment(double balance, int customerId) {
super(balance, customerId);
[Link] = balance;
}
public String getPaymentId() {
return paymentId;
}
public double getCashBack() {
return cashBack;
}
public double getBalanceDue() {
return balanceDue;
}
@Override
public void payBill(double amount) {
counter++;
paymentId = "C" + counter;
if (amount < balanceDue) {
balanceDue -= amount;
} else {
cashBack = amount - balanceDue;
balanceDue = 0;
}
[Link]("Congratulations: You have successfully made a payment of
Rs. " + amount + ". Payment details are:-");
[Link]("Customer Id: " + customerId);
[Link]("Payment Id: " + paymentId);
[Link]("Previous Due: " + balance);
[Link]("Remaining Due: " + balanceDue);
[Link]("Cash Back Wallet Balance: " + cashBack);
}
}
[Link]
public class Tester {
public static void main(String[] args) {
// Test case for Credit Card Payment
[Link]("Input 1 (For Credit Card Payment)");
MyPaymentServices creditCardPayment1 = new CreditCardPayment(10000.56,
5001);
[Link](15000.00);
[Link]("\nInput 2 (For Credit Card Payment)");
MyPaymentServices creditCardPayment2 = new CreditCardPayment(10000.56,
5001);
[Link](0);
[Link]("\nInput 3 (For Credit Card Payment)");
MyPaymentServices creditCardPayment3 = new CreditCardPayment(10000.56,
5001);
[Link](5000);
// Test case for Shopping Payment
[Link]("\nInput 4 (For Shopping Payment)");
MyPaymentServices shoppingPayment1 = new ShoppingPayment(5000.56,
561328);
[Link](5000.00);
}
}
Outputs
For input 1
Congratulations: You have successfully made a payment of Rs. 15000.0 .Payment
details are:-
Customer Id: 5001
Payment Id: C1001
Previous Due: 10000.56
Remaining Due: 0.0
Cash Back Wallet Balance: 4999.44
For Input 2
Congratulations: You have successfully made a payment of Rs. 0.0 .Payment details
are:-
Customer Id: 5001
Payment Id: C1002
Previous Due: 10000.56
Remaining Due: 10000.56
Cash Back Wallet Balance: 0.0
For Input 3
Congratulations: You have successfully made a payment of Rs. 5000.0 .Payment
details are:-
Customer Id: 5001
Payment Id: C1003
Previous Due: 10000.56
Remaining Due: 5000.56
Cash Back Wallet Balance: 0.0
For Input 4
Error: The amount paid does not match the balance due.