0% found this document useful (0 votes)
17 views

Lab - File - Exp Java 7-12

java experiment
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab - File - Exp Java 7-12

java experiment
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Experiment 7

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class XYZBank {
private double balance;
private final Lock lock = new ReentrantLock();

public XYZBank(double initialBalance) {


this.balance = initialBalance;
}

public void deposit(double amount) {


lock.lock();
try {
balance += amount;
System.out.println(Thread.currentThread().getName() + " deposited: " +
amount + ", New balance: " + balance);
} finally {
lock.unlock();
}
}

public void withdraw(double amount) {


lock.lock();
try {
if (balance >= amount) {
balance -= amount;
System.out.println(Thread.currentThread().getName() + " withdrew: " +
amount + ", New balance: " + balance);
} else {
System.out.println(Thread.currentThread().getName() + " attempted to
withdraw: " + amount + " but insufficient balance. Current balance: " + balance);
}
} finally {
lock.unlock();
}
}
}

class DepositThread extends Thread {


private final XYZBank bank;
private final double amount;

public DepositThread(XYZBank bank, double amount) {


this.bank = bank;
this.amount = amount;
}
public void run() {
bank.deposit(amount);
}
}

class WithdrawThread extends Thread {


private final XYZBank bank;
private final double amount;

public WithdrawThread(XYZBank bank, double amount) {


this.bank = bank;
this.amount = amount;
}

public void run() {


bank.withdraw(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);

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;

public Employee(String name, String gender) {


this.name = name;
this.gender = gender;
}

public String getName() {


return name;
}

public String getGender() {


return gender;
}
}

public class Main {


public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Alice", "Female"),
new Employee("Bob", "Male"),
new Employee("Charlie", "Male"),
new Employee("Diana", "Female")
);

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;

public Student(String name, String course, int rollNumber) {


this.name = name;
this.course = course;
this.rollNumber = 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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(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 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() {
}

public Student(String name, String course, int rollNumber) {


this.name = name;
this.course = course;
this.rollNumber = rollNumber;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getCourse() {


return course;
}

public void setCourse(String course) {


this.course = course;
}

public int getRollNumber() {


return rollNumber;
}

public void setRollNumber(int rollNumber) {


this.rollNumber = rollNumber;
}

@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 +
'}';
}
}

Create the Configuration Class

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);
}
}

Create the Main Application Class

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {


public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
Student student = context.getBean(Student.class);
System.out.println(student);
context.close();
}
}
Output
Init method called at: 2024-07-15T12:34:56.789
Student{name='Alice', course='Computer Science', rollNumber=101}
Destroy method called at: 2024-07-15T12:34:56.789
Experiment 11
Create the Student Controller

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;

@Value("${student.address:1234 Default St, Default City}")


private String studentAddress;

private final ApplicationContext applicationContext;

public StudentController(ApplicationContext applicationContext) {


this.applicationContext = applicationContext;
}

@GetMapping("/name")
public String getStudentName() {
return studentName;
}

@GetMapping("/address")
public String getStudentAddress() {
return studentAddress;
}

@GetMapping("/beans")
public String[] getBeans() {
return applicationContext.getBeanDefinitionNames();
}
}

Create the Main Application Class


package com.example.demo;

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

public abstract class MyPaymentServices {


protected double balance;
protected int customerId;

public MyPaymentServices(double balance, int customerId) {


this.balance = balance;
this.customerId = customerId;
}

public void setBalance(double balance) {


this.balance = balance;
}

public double getBalance() {


return balance;
}

public int getCustomerId() {


return customerId;
}

public abstract void payBill(double amount);


}

ShoppingPayment.java

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) {
System.out.println("Error: The amount paid does not match the balance due.");
return;
}
counter++;
paymentId = "S" + counter;

System.out.println("Congratulations: You have successfully made a payment of


Rs. " + amount + ". Payment details are:-");
System.out.println("Customer Id: " + customerId);
System.out.println("Payment Id: " + paymentId);
System.out.println("Previous Due: " + balance);
System.out.println("Remaining Due: 0");
}
}

CreditCardPayment.java

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);
this.balanceDue = 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;
}
System.out.println("Congratulations: You have successfully made a payment of
Rs. " + amount + ". Payment details are:-");
System.out.println("Customer Id: " + customerId);
System.out.println("Payment Id: " + paymentId);
System.out.println("Previous Due: " + balance);
System.out.println("Remaining Due: " + balanceDue);
System.out.println("Cash Back Wallet Balance: " + cashBack);
}
}

Tester.java

public class Tester {


public static void main(String[] args) {
// Test case for Credit Card Payment
System.out.println("Input 1 (For Credit Card Payment)");
MyPaymentServices creditCardPayment1 = new CreditCardPayment(10000.56,
5001);
creditCardPayment1.payBill(15000.00);

System.out.println("\nInput 2 (For Credit Card Payment)");


MyPaymentServices creditCardPayment2 = new CreditCardPayment(10000.56,
5001);
creditCardPayment2.payBill(0);

System.out.println("\nInput 3 (For Credit Card Payment)");


MyPaymentServices creditCardPayment3 = new CreditCardPayment(10000.56,
5001);
creditCardPayment3.payBill(5000);

// Test case for Shopping Payment


System.out.println("\nInput 4 (For Shopping Payment)");
MyPaymentServices shoppingPayment1 = new ShoppingPayment(5000.56,
561328);
shoppingPayment1.payBill(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.

You might also like