Lab - File - Exp Java 7-12
Lab - File - Exp Java 7-12
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class XYZBank {
private double balance;
private final Lock lock = new ReentrantLock();
depositThread1.start();
depositThread2.start();
withdrawThread1.start();
withdrawThread2.start();
}
}
Experiment 8
Create the Student Model Class
import java.util.Arrays;
import java.util.List;
class Employee {
private String name;
private String gender;
employees.forEach(employee ->
System.out.println("Name: " + employee.getName() + ", Gender: " +
employee.getGender())
);
}
}
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;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", rollNumber=" + rollNumber +
'}';
}
}
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.time.LocalDateTime;
@Component
public class Student {
private String name;
private String course;
private int rollNumber;
public Student() {
}
@PostConstruct
public void init() {
System.out.println("Init method called at: " + LocalDateTime.now());
}
@PreDestroy
public void destroy() {
System.out.println("Destroy method called at: " + LocalDateTime.now());
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", rollNumber=" + rollNumber +
'}';
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
public Student student() {
return new Student("Alice", "Computer Science", 101);
}
}
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@RestController
@RequestMapping("/student")
public class StudentController {
@Value("${student.name:John Doe}")
private String studentName;
@GetMapping("/name")
public String getStudentName() {
return studentName;
}
@GetMapping("/address")
public String getStudentAddress() {
return studentAddress;
}
@GetMapping("/beans")
public String[] getBeans() {
return applicationContext.getBeanDefinitionNames();
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Experiment 12
Create the Base Class MyPaymentServices
ShoppingPayment.java
@Override
public void payBill(double amount) {
if (amount != balance) {
System.out.println("Error: The amount paid does not match the balance due.");
return;
}
counter++;
paymentId = "S" + counter;
CreditCardPayment.java
@Override
public void payBill(double amount) {
counter++;
paymentId = "C" + counter;
Tester.java
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.