0% found this document useful (0 votes)
24 views19 pages

Assignment 5 Debi

Bbn

Uploaded by

devildada0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

Assignment 5 Debi

Bbn

Uploaded by

devildada0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignment - V

Topic: Base on Java Annotation


Name: Debi Prasad Rath
Registration Number: 2251018009
Section: I

1. Create an interface ‘PaymentProcessor’ with a method String


processPayment(double amount,String currency). Create 3 classes named
creditCardProcessor, payPalProcessor,and BankTransferProcessor each
implementing the PaymentProcessor interface. In each class, override the
processPayment method to return a string that includes the type of
paymentprocessor(eg.credit crd),the amount, and currency. Create a class named
PaymentService that contains a dependency on the PaymentProcessor interface and a
method void makePayment(double amount, String currency) that calls the
processPayment method. The method should print a message “processing creditcard
payment of 100.0USD.
Use setter method to add dependencies. Use annotation based configuration and create
different beans to populate PaymentProcessor with the creditCardProcessor,
PayPalProcessor, and BankTransferProcessor classes.Write a Run class of Spring
application to execute the application.

CODE:
PaymentProcessor.java
package com.A5Q1;

public interface PaymentProcessor {


String processPayment(double amount, String currency);
}

CreditCardProcessor.java
package com.A5Q1;
import org.springframework.stereotype.Component;

@Component
public class CreditCardProcessor implements PaymentProcessor{
@Override
public String processPayment(double amount, String currency) {
return "Processing payment with credit card of "+amount+currency;
}
}
PayPalProcessor.java
package com.A5Q1;
import org.springframework.stereotype.Component;

@Component
public class PayPalProcessor implements PaymentProcessor{
@Override
public String processPayment(double amount, String currency){
return "Processing payment with PayPal of "+amount+currency;
}
}
BankTransferProcessor.java
package com.A5Q1;
import org.springframework.stereotype.Component;

@Component
public class BankTransferProcessor implements PaymentProcessor{
@Override
public String processPayment(double amount, String currency) {
return "Processing payment with Bank Transfer of "+amount+currency;
}
}

PaymentService.java
package com.A5Q1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class PaymentService {
private PaymentProcessor paymentProcessor;

@Autowired
@Qualifier("creditCardProcessor")
public void setPaymentProcessor(PaymentProcessor paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}

public void makePayment(double amount, String currency) {


String message = paymentProcessor.processPayment(amount, currency);
System.out.println(message);
}
}

AppConfig.java
package com.A5Q1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.A5Q1")
public class AppConfig {

@Bean
public PaymentProcessor creditCardProcessor() {
return new CreditCardProcessor();
}

@Bean
public PaymentProcessor payPalProcessor() {
return new PayPalProcessor();
}

@Bean
public PaymentProcessor bankTransferProcessor() {
return new BankTransferProcessor();
}
}

App.java
package com.A5Q1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);

PaymentService paymentService = context.getBean(PaymentService.class);

paymentService.setPaymentProcessor(context.getBean("creditCardProcessor",
PaymentProcessor.class));
paymentService.makePayment(100.0, "INR");

paymentService.setPaymentProcessor(context.getBean("payPalProcessor",
PaymentProcessor.class));
paymentService.makePayment(200.0, "INR");

paymentService.setPaymentProcessor(context.getBean("bankTransferProcessor",
PaymentProcessor.class));
paymentService.makePayment(300.0, "INR");
}
}

OUTPUT:

2. Write a java program to create a class Book with private data members such as
bookName, authorName,and edition. Use setter and getter, and display method. Create
a annotation based configuration and display book detail in the main class of Spring
Application.

CODE:
Book.java
package com.A5Q2;
public class Book {
String bookName, authorName;
int edition;

public String getBookName() {


return bookName;
}

public void setBookName(String bookName) {


this.bookName = bookName;
}

public String getAuthorName() {


return authorName;
}

public void setAuthorName(String authorName) {


this.authorName = authorName;
}

public int getEdition() {


return edition;
}

public void setEdition(int edition) {


this.edition = edition;
}

void display(){
System.out.println("Book Name: " + bookName);
System.out.println("Author Name: " + authorName);
System.out.println("Edition: " + edition);
}
}
AppConfig.java
package com.A5Q2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.A5Q2")
public class AppConfig {
@Bean
public Book book(){
Book book=new Book();
book.setBookName("Hands-on Application Development using Spring Boot");
book.setAuthorName("Shagun Bakliwal");
book.setEdition(2);

return book;
}
}

App.java
package com.A5Q2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {


public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
Book book = context.getBean(Book.class);
book.display();
}
}

OUTPUT:

3. Create class named as Student which contain a method printMessage(String name)


and it returns Hello yourname. Create a bean object of this class and call the method
printMessage in the main class of Springboot Application by using following ways
a. By implementing CommandLineRunner interface method.
b. Without implementing commandLineRunner.
(use separate configuration class)
CODE:
Student.java
package com.A5Q3A;

public class Student {


String name;
public String printMessage(String name){
return "Hello "+name;
}
}

StudentRunner.java
package com.A5Q3A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StudentRunner implements CommandLineRunner {

private final Student student;

@Autowired
public StudentRunner(Student student) {
this.student = student;
}

@Override
public void run(String... args) {
System.out.println(student.printMessage("World"));
}
}

AppConfig.java
package com.A5Q3A;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public Student student() {
return new Student();
}
}

App.java
package com.A5Q3A;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

OUTPUT:

Code:
Student.java
package com.A5Q3B;

public class Student {


public String printMessage(String name) {
return "Hello " + name;
}
}

AppConfig.java
package com.A5Q3B;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public Student student() {
return new Student();
}
}

App.java
package com.A5Q3B;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(App.class, args);

Student student = context.getBean(Student.class);


System.out.println(student.printMessage("World"));
}
}

OUTPUT:

4. Create a constructor called MyClass which consists of following attributes


Name, regdNo, subjectName and markSecured. Write a display() method which
displays above details.
Create 2 bean object of the constructor for two students and call the display method in
the main class of Springboot Application by using both ways of 3.a and 3.b.

CODE:
MyClass.java
package com.A5Q4;

public class MyClass {


String name, subjectName;
int regdNo, markSecured;

MyClass(String name, int regdNo, String subjectName, int markSecured){


this.name=name;
this.regdNo=regdNo;
this.subjectName=subjectName;
this.markSecured=markSecured;
}

void display(){
System.out.println("Name: "+name);
System.out.println("Registration No: "+regdNo);
System.out.println("Subject Name: "+subjectName);
System.out.println("Mark secured: " + markSecured+"\n");
}
}

StudentRunner.java
package com.A5Q4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StudentRunner implements CommandLineRunner {

private final MyClass student1;


private final MyClass student2;

@Autowired
public StudentRunner(MyClass student1, MyClass student2) {
this.student1 = student1;
this.student2 = student2;
}

@Override
public void run(String... args) {
System.out.println("THIS DATA IS SHOWN BY CommandLineRunner \n");
System.out.println("Student 1 Details:");
student1.display();

System.out.println("Student 2 Details:");
student2.display();
System.out.println("------------------------------------------");
}
}

AppConfig.java
package com.A5Q4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
@Bean
MyClass student1(){
return new MyClass("Alice", 2141013059, "ADP", 75);
}

@Bean
MyClass student2(){
return new MyClass("Bob", 2141018950, "IR", 65);
}
}

App.java
package com.A5Q4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(App.class, args);

MyClass student1 = context.getBean("student1", MyClass.class);


MyClass student2 = context.getBean("student2", MyClass.class);

System.out.println("THIS DATA IS SHOWN WITHOUT CommandLineRunner \n");


System.out.println("Student 1 Details:");
student1.display();

System.out.println("Student 2 Details:");
student2.display();
}
}

OUTPUT:

5. Create a package of 4 classes named as address, Teacher, app, javaConfig.


Address has fields as houseNo, city, postOffice, pin, state. Use setter method for the
given fields and toString method to print string representation of the object.
Teacher class has name, id, mobileNo, and address fields. Use the setter method and
toString method for this class. javaConfig class is java annotation-based configuration
file which creates two address beans and on teacher bean and initialize the fields of
both classes. In teacher class use any one address by using @Qualifier annotation.
Call the teacher object in app class which consists of main method of Spring
application.
CODE:
Address.java

package com.A5Q5;

public class Address {


int houseNo, pin;
String city, postOffice, state;

int getHouseNo(){
return houseNo;
}

void setHouseNo(int houseNo){


this.houseNo=houseNo;
}

public int getPin() {


return pin;
}

public void setPin(int pin) {


this.pin = pin;
}

public String getCity(){


return city;
}

void setCity(String city){


this.city=city;
}

public String getPostOffice(){


return postOffice;
}

void setPostOffice(String postOffice){


this.postOffice=postOffice;
}

public String getState(){


return state;
}

void setState(String state){


this.state=state;
}

@Override
public String toString() {
return "\nAddress"+'\n'+
"{\n" +
"House No: " + houseNo +'\n'+
"Post Office: "+ postOffice+'\n'+
"City: " + city +'\n'+
"State'" + state +'\n'+
"PIN: "+ pin+'\n'+
'}';
}
}

Teacher.java
package com.A5Q5;

public class Teacher {


String name;
int id, mobileNo;
public Address address;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public int getMobileNo() {


return mobileNo;
}

public void setMobileNo(int mobileNo) {


this.mobileNo = mobileNo;
}

public void setAddress(Address address) {


this.address = address;
}

@Override
public String toString() {
return "Teacher \n"+
"{" + '\n'+
"Name: " + name + '\n' +
"ID: " + id + '\n'+
"Mobile No: " + mobileNo + '\n'+
"}" + address;
}
}

JavaConfig.java
package com.A5Q5;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfig {
@Bean
public Address address1(){
Address address=new Address();
address.setHouseNo(01234);
address.setPostOffice("Khandagiri");
address.setCity("Bhubaneswar");
address.setState("Odisha");
address.setPin(751030);
return address;
}

@Bean
public Address address2(){
Address address=new Address();
address.setHouseNo(109);
address.setPostOffice("Rohini");
address.setCity("Delhi");
address.setState("New Delhi");
address.setPin(110086);
return address;
}

@Bean
public Teacher teacher(@Qualifier("address1") Address address) {
Teacher teacher = new Teacher();
teacher.setName("Albert Einstein");
teacher.setId(8672);
teacher.setMobileNo(98765432);
teacher.setAddress(address);
return teacher;
}
}

App.java
package com.A5Q5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(App.class, args);
Teacher teacher = context.getBean(Teacher.class);
System.out.println(teacher);
}
}
OUTPUT:

6. Create a customer class which consists of customerName, accountNo, IFSCCode,


use setter method and override the toString Method.
Create a configuration class(annotation based) named as conFig. Use configuration
and componntscan Annotatuion and create a bean for customer class and initialize the
field by calling setter method of customer class. Display details of customer in the
app/run class which consists of main method of SpringApplication.

Code:
Customer.java
package com.A5Q6;
public class Customer {
private String customerName, accountNo, IFSCCode;

public void setCustomerName(String customerName) {


this.customerName = customerName;
}

public void setAccountNo(String accountNo) {


this.accountNo = accountNo;
}

public void setIFSCCode(String IFSCCode) {


this.IFSCCode = IFSCCode;
}

@Override
public String toString() {
return "Customer{" +
"customerName='" + customerName + '\'' +
", accountNo='" + accountNo + '\'' +
", IFSCCode='" + IFSCCode + '\'' +
'}';
}
}
Config.java
package com.A5Q6;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.A5Q6")
public class Config {

@Bean
public Customer customer() {
Customer customer = new Customer();
customer.setCustomerName("Chandan");
customer.setAccountNo("83847057241");
customer.setIFSCCode("SBIN0001075");
return customer;
}
}

App.java
package com.A5Q6;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(App.class, args);
Customer customer = context.getBean(Customer.class);
System.out.println(customer);
}
}

OUTPUT:
7. Create a class named as Book having fields title, price, pDate(reference of class
pubDate) and override the toString Method. Use @value ,@Autowire @Component
and @override annotation in appropriate position. The class pubDate consists of filed
day, month, and year. Use @value to give value of each field and write toString
method. Display book details in the main method of app.java of Spring Application.

CODE:
PubDate.java
package com.A5Q7;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PubDate {
@Value("10")
private int day;

@Value("11")
private int month;

@Value("2024")
private int year;

@Override
public String toString() {
return "PubDate{" +
"day=" + day +
", month=" + month +
", year=" + year +
'}';
}
}

Book.java
package com.A5Q7;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component
public class Book{
@Value("SpringBoot for Beginners")
String title;

@Value("1099")
int price;

@Autowired
PubDate pubDate;

@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", price=" + price +
", pubDate=" + pubDate +
'}';
}
}

App.java
package com.A5Q7;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App{
public static void main(String[] args) {
ApplicationContext context=SpringApplication.run(App.class, args);
Book book=context.getBean(Book.class);
System.out.println(book);
}
}

OUTPUT:

8. Create a class and use appropriate annotation to display your name in browser
when user type.localhost:8080/home.

Code:
AppController.java
package com.A5Q8;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppContoller {
@GetMapping("/home")
public String greet(){
return "Hello Future";
}
}

App.java

package com.A5Q8;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}

OUTPUT:

You might also like