You are on page 1of 38

// JBS-S4-L1-1- Customer and Address Class

import java.util.*;

//class Address

class Address{

String city,state,country;

int zip;

//default constructor

public Address(){

//parameterized Contstructor

public Address(String city,String state,int zip,String country){

this.city=city;

this.state=state;

this.zip=zip;

this.country=country;

//setters and getters

public void setCity(String city){

this.city=city;

public String getCity(){

return city;

public void setState(String state){

this.state=state;

public String getState(){

return state;

}
public void setZip(int zip){

this.zip=zip;

public int getZip(){

return zip;

public void setCountry(String country){

this.country=country;

public String getCountry(){

return country;

//override toString()

public String toString(){

return "Address [city="+city+

", state="+state+

", zip="+zip+

", country="+country+

"]";

//class Customer

class Customer{

int userId;

String emailId,password,firstName,lastName,city,gender;

long phoneNumber;

Address address;

//default constructor

public Customer(){
}

//parametrized constructor

public Customer(int userId,String emailId,String password,String firstName,String lastName,String city,String


gender,long phoneNumber,Address address){

this.userId=userId;

this.emailId=emailId;

this.password=password;

this.firstName=firstName;

this.lastName=lastName;

this.city=city;

this.gender=gender;

this.phoneNumber=phoneNumber;

this.address=address;

//setters and getters

public void setUserId(int userId){

this.userId=userId;

public int getUserId(){

return userId;

public void setEmailId(String emailId){

this.emailId=emailId;

public String getEmailId(){

return emailId;

public void setPassword(String password){

this.password=password;

}
public String getPassword(){

return password;

public void setFirstName(String firstName){

this.firstName=firstName;

public String getFirstName(){

return firstName;

public void setLasttName(String lastName){

this.lastName=lastName;

public String getLastName(){

return lastName;

public void setCity(String city){

this.city=city;

public String getCity(){

return city;

public void setGender(String gender){

this.gender=gender;

public String getGender(){

return gender;

public void setPhoneNumber(long phoneNumber){

this.phoneNumber=phoneNumber;

}
public long getPhoneNumber(){

return phoneNumber;

public void setAddress(Address address){

this.address=address;

public Address getAddress(){

return address;

//override toString()

public String toString(){

String add=address.toString();

return "Customer [userId="+userId+

", emailId="+emailId+

", password="+password+

", firstName="+firstName+

", lastName="+lastName+

", city="+city+

", gender="+gender+

", phoneNumber="+phoneNumber+

", address="+add+

"]";

//class Source

class Source{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

int userId=sc.nextInt();

String emailId=sc.next();
String password=sc.next();

String firstName=sc.next();

String lastName=sc.next();

String city=sc.next();

String gender=sc.next();

long phoneNumber=sc.nextLong();

String state=sc.next();

int zip=sc.nextInt();

String country=sc.next();

//create object

Address address=new Address(city,state,zip,country);

Customer customer=new
Customer(userId,emailId,password,firstName,lastName,city,gender,phoneNumber,address);

//print customer data

System.out.println(customer);

}
//JBS-S4-L1-2 Admin and ServiceDetails Classes
import java.util.*;

import java.time.LocalDateTime;

//class Admin

class Admin{

int adminId;

String email;

String firstName;

String password;

//default constructor

public Admin(){

//parametrized constructor

public Admin(int adminId,String email,String firstName,String password){

this.adminId=adminId;

this.email=email;

this.firstName=firstName;

this.password=password;

//getters and setters

public void setAdminId(int adminId){

this.adminId=adminId;

public int getAdminId(){

return adminId;

public void setEmailId(String email){

this.email=email;
}

public String getEmailId(){

return email;

public void setFirstNamel(String firstName){

this.firstName=firstName;

public String getFirstName(){

return firstName;

public void setPassword(String password){

this.password=password;

public String getPassword(){

return password;

//override toString()

public String toString(){

return "Admin [adminId="+adminId+

", emailId="+email+

", firstName="+firstName+

", password="+password+

"]";

//class ServiceDetails

class ServiceDetails{

int serviceId;

int busId;

LocalDateTime deptDateTime;
LocalDateTime arrDateTime;

String arrivalCity;

String deptCity;

double cost;

int seatsLeft;

int totalSeats ;

//default constructor

public ServiceDetails(){

//parametrized constructor

public ServiceDetails(int serviceId,int busId,LocalDateTime deptDateTime,

LocalDateTime arrDateTime,String arrivalCity,String deptCity,double cost,

int seatsLeft,int totalSeats){

this.serviceId=serviceId;

this.busId=busId;

this.deptDateTime=deptDateTime;

this.arrDateTime=arrDateTime;

this.arrivalCity=arrivalCity;

this.deptCity=deptCity;

this.cost=cost;

this.seatsLeft=seatsLeft;

this.totalSeats=totalSeats;

//getters and setters

public void setServiceId(int serviceId){

this.serviceId=serviceId;

public int getServiceId(){


return serviceId;

public void setBusId(int busId){

this.busId=busId;

public int getBusId(){

return busId;

public void setServiceid(LocalDateTime deptDateTime){

this.deptDateTime=deptDateTime;

public LocalDateTime getDeptDateTime(){

return deptDateTime;

public void setArrDatetime(LocalDateTime arrDateTime){

this.arrDateTime=arrDateTime;

public LocalDateTime getArrDateTime(){

return arrDateTime;

public void setArrivalCity(String arrivalCity){

this.arrivalCity=arrivalCity;

public String getArrivalCity(){

return arrivalCity;

public void setDeptCity(String deptCity){

this.deptCity=deptCity;

public String getDeptCity(){


return deptCity;

public void setCost(double cost){

this.cost=cost;

public double getCost(){

return cost;

public void setSeatsLeft(int seatsLeft){

this.seatsLeft=seatsLeft;

public int getSeatsLeft(){

return seatsLeft;

public void setTotalSeats(int totalSeats){

this.totalSeats=totalSeats;

public int getTotalSeats(){

return totalSeats;

//override toString()

public String toString(){

return "ServiceDetails [serviceId="+serviceId+

", busId="+busId+

", depDateTime="+deptDateTime+

", arrDateTime="+arrDateTime+

", arrCity="+arrivalCity+

", deptCity="+deptCity+

", cost="+cost+

", seatsLeft="+seatsLeft+
", totalSeats="+totalSeats+

"]";

//class Sourse

class Source{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

//admin variables

int adminId=sc.nextInt();

String email=sc.next();

String firstName=sc.next();

String password=sc.next();

//sericeDetails variables

int serviceId=sc.nextInt();

int busId=sc.nextInt();

int dyear=sc.nextInt();

int dmonth=sc.nextInt();

int dday=sc.nextInt();

int dh=sc.nextInt();

int dm=sc.nextInt();

LocalDateTime deptDateTime=LocalDateTime.of(dyear,dmonth,dday,dh,dm);

int ayear=sc.nextInt();

int amonth=sc.nextInt();

int aday=sc.nextInt();

int ah=sc.nextInt();

int am=sc.nextInt();

LocalDateTime arrDateTime=LocalDateTime.of(ayear,amonth,aday,ah,am);

String arrivalCity=sc.next();

String deptCity=sc.next();
double cost=sc.nextDouble();

int seatsLeft=sc.nextInt();

int totalSeats=sc.nextInt();

//object

Admin A=new Admin(adminId,email,firstName,password);

ServiceDetails SD=new ServiceDetails(serviceId,busId,deptDateTime,arrDateTime,

arrivalCity,deptCity,cost,seatsLeft,totalSeats);

//print servicedetails

System.out.println(SD);

}
JBS-S4-L2-1-Customer Name Validator
import java.util.*;

import javax.naming.InvalidNameException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

//class Customer

class Customer{

String firstName;

String lastName;

Customer(){

Customer(String firstName,String lastName)throws InvalidNameException{

if(firstName.length()<6 || lastName.length()<6){

throw new InvalidNameException();

else{

this.firstName=firstName;

this.lastName=lastName;

Pattern pattern=Pattern.compile("[^a-zA-Z]");

Matcher matcher1=pattern.matcher(firstName);

Matcher matcher2=pattern.matcher(lastName);

boolean isStringContainsSpecialCharacter1=matcher1.find();

boolean isStringContainsSpecialCharacter2=matcher2.find();

if(isStringContainsSpecialCharacter2 || isStringContainsSpecialCharacter2)

throw new InvalidNameException();


}

else{

this.firstName=firstName;

this.lastName=lastName;

public void setFirstName(String firstName)throws InvalidNameException{

if(firstName.length()<6){

throw new InvalidNameException();

else{

this.firstName=firstName;

Pattern pattern=Pattern.compile("[^a-zA-Z]");

Matcher matcher=pattern.matcher(firstName);

boolean isStringContainsSpecialCharacter=matcher.find();

if(isStringContainsSpecialCharacter)

throw new InvalidNameException();

else{

this.firstName=firstName;

public String getFirstName(){

return firstName;

public void setLastName(String lastName)throws InvalidNameException{

if(lastName.length()<6){

throw new InvalidNameException();


}

else{

this.lastName=lastName;

Pattern pattern=Pattern.compile("[^a-zA-Z]");

Matcher matcher=pattern.matcher(lastName);

boolean isStringContainsSpecialCharacter=matcher.find();

if(isStringContainsSpecialCharacter)

throw new InvalidNameException();

else{

this.lastName=lastName;

public String getLastName(){

return lastName;

//class Source

class Source{

public static void main(String[] args)throws InvalidNameException{

Scanner sc=new Scanner(System.in);

String firstName=sc.next();

String lastName=sc.next();

Customer C=new Customer(firstName,lastName);

System.out.println(C.getFirstName());

System.out.println(C.getLastName());

}
JBS-S4-L2-2-Customer Mobile Number
-Exception
import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

class InvalidPhoneNumberExcception extends Exception{

InvalidPhoneNumberExcception(){

super();

class Customer{

long phoneNumber;

Customer(){

Customer(long phoneNumber)throws InvalidPhoneNumberExcception{

Pattern pattern=Pattern.compile("^[6-9][0-9]{9}$");

Matcher matcher=pattern.matcher(Long.toString(phoneNumber));

if(matcher.matches()){

this.phoneNumber=phoneNumber;

else{

try{

throw new InvalidPhoneNumberExcception();

catch(InvalidPhoneNumberExcception e){

if(e instanceof InvalidPhoneNumberExcception){

throw new InvalidPhoneNumberExcception();

}
}

public void setPhoneNumber(long phoneNumber)throws InvalidPhoneNumberExcception{

Pattern pattern=Pattern.compile("^[6-9][0-9]{9}$");

Matcher matcher=pattern.matcher(Long.toString(phoneNumber));

if(matcher.matches()){

this.phoneNumber=phoneNumber;

else{

try{

throw new InvalidPhoneNumberExcception();

catch(InvalidPhoneNumberExcception e){

if(e instanceof InvalidPhoneNumberExcception){

throw new InvalidPhoneNumberExcception();

public long getPhoneNumber(){

return phoneNumber;

class Source{

public static void main(String[] args)throws InvalidPhoneNumberExcception{

Scanner sc=new Scanner(System.in);

long phoneNumber=sc.nextLong();

Customer C=new Customer(phoneNumber);

System.out.println(C.getPhoneNumber());
}

3-1 CUSTOMER SERVICE INTERFACE

class CustomerNotFoundException extends RuntimeException{

public CustomerNotFoundException() {

class Address {

String city;

String State;

int zip;

String Country;

public Address(String city, String state, int zip, String country) {

super();

this.city = city;

State = state;

this.zip = zip;

Country = country;

public String getCity() {

return city;

public void setCity(String city) {

this.city = city;

public String getState() {


return State;

public void setState(String state) {

this.State = state;

public int getZip() {

return zip;

public void setZip(int zip) {

this.zip = zip;

public String getCountry() {

return Country;

public void setCountry(String country) {

this.Country = country;

@Override

public String toString() {

return "Address [city=" + city + ", state=" + State + ", zip=" + zip + ", country=" + Country + "]";

class Customer {

int userId;

String emailId;

String password;

String firstName;

String lastName;

String city;
String gender;

long phoneNumber;

Address address;

public Customer(){

public Customer(int userId, String emailId, String password, String firstName, String lastName, String city,

String gender, long phoneNumber, Address address) {

this.userId = userId;

this.emailId = emailId;

this.password = password;

this.firstName = firstName;

this.lastName = lastName;

this.city = city;

this.gender = gender;

this.phoneNumber = phoneNumber;

this.address = address;

@Override

public String toString() {

return "Customer [userId=" + userId + ", emailId=" + emailId + ", password=" + password + ", firstName="

+ firstName + ", lastName=" + lastName + ", city=" + city + ", gender=" + gender + ", phoneNumber="

+ phoneNumber + ", address=" + address + "]";

public int getUserId() {

return userId;

public void setUserId(int userId) {

this.userId = userId;

}
public String getEmailId() {

return emailId;

public void setEmailId(String emailId) {

this.emailId = emailId;

public String getPassword() {

return password;

public void setPassword(String password) {

this.password = password;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {

this.firstName = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getCity() {

return city;

public void setCity(String city) {

this.city = city;

}
public String getGender() {

return gender;

public void setGender(String gender) {

this.gender = gender;

public long getPhoneNumber() {

return phoneNumber;

public void setPhoneNumber(long phoneNumber) {

this.phoneNumber = phoneNumber;

public Address getAddress() {

return address;

public void setAddress(Address address) {

this.address = address;

interface CustomerService{

void createCustomer(Customer customer);

Customer updateCustomer(Customer customer);

void deleteCustomer(int id);

Customer searchCustomer(int id);

Customer[] getCustomers();

class CustomerServiceImpl implements CustomerService{

public static Customer customerArray[]= new Customer[5];

public static int count=0;


@Override

public void createCustomer(Customer customer) {

customerArray[count]=customer;

count++;

@Override

public Customer updateCustomer(Customer customer) throws CustomerNotFoundException{

for(int i=0;i<5;i++)

if(customer.getUserId()==customerArray[i].userId)

customerArray[i]=customer;

return customerArray[i];

throw new CustomerNotFoundException();

@Override

public void deleteCustomer(int id) throws CustomerNotFoundException {

for(int i=0;i<5;i++)

if(customerArray[i].getUserId()==id) {

customerArray[i]=null;

break;

}throw new CustomerNotFoundException();


}

@Override

public Customer searchCustomer(int id) throws CustomerNotFoundException {

for(int i=0;i<5;i++)

if(customerArray[i].getUserId()==id) {

return customerArray[i];

throw new CustomerNotFoundException();

@Override

public Customer[] getCustomers() {

return customerArray;

}
3-2 ADMIN SERVICE INTERFACE
import java.time.LocalDateTime;

import java.util.ArrayList;

class ServiceDetails{

private int serviceId;

private int busId;

private LocalDateTime deptDateTime;

private LocalDateTime arrDateTime;

private String arrivalCity;

private String deptCity;

private double cost;

private int seatsLeft;

private int totalSeats;

public ServiceDetails(){

public ServiceDetails(int serviceId, int busId, LocalDateTime deptDateTime, LocalDateTime arrDateTime, String
arrivalCity, String deptCity, double cost, int seatsLeft, int totalSeats) {

this.serviceId = serviceId;

this.busId = busId;

this.deptDateTime = deptDateTime;

this.arrDateTime = arrDateTime;

this.arrivalCity = arrivalCity;

this.deptCity = deptCity;

this.cost = cost;

this.seatsLeft = seatsLeft;

this.totalSeats = totalSeats;
}

public int getServiceId() {

return serviceId;

public void setServiceId(int serviceId) {

this.serviceId = serviceId;

public int getBusId() {

return busId;

public void setBusId(int busId) {

this.busId = busId;

public LocalDateTime getDeptDateTime() {

return deptDateTime;

public void setDeptDateTime(LocalDateTime deptDateTime) {

this.deptDateTime = deptDateTime;

public LocalDateTime getArrDateTime() {

return arrDateTime;

}
public void setArrDateTime(LocalDateTime arrDateTime) {

this.arrDateTime = arrDateTime;

public String getArrivalCity() {

return arrivalCity;

public void setArrivalCity(String arrivalCity) {

this.arrivalCity = arrivalCity;

public String getDeptCity() {

return deptCity;

public void setDeptCity(String deptCity) {

this.deptCity = deptCity;

public double getCost() {

return cost;

public void setCost(double cost) {

this.cost = cost;

public int getSeatsLeft() {

return seatsLeft;
}

public void setSeatsLeft(int seatsLeft) {

this.seatsLeft = seatsLeft;

public int getTotalSeats() {

return totalSeats;

public void setTotalSeats(int totalSeats) {

this.totalSeats = totalSeats;

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

ServiceDetails that = (ServiceDetails) o;

if (serviceId != that.serviceId) return false;

if (busId != that.busId) return false;

if (Double.compare(that.cost, cost) != 0) return false;

if (seatsLeft != that.seatsLeft) return false;

if (totalSeats != that.totalSeats) return false;

if (!deptDateTime.equals(that.deptDateTime)) return false;

if (!arrDateTime.equals(that.arrDateTime)) return false;

if (!arrivalCity.equals(that.arrivalCity)) return false;

return deptCity.equals(that.deptCity);
}

interface AdminService{

void createServiceDetails(ServiceDetails serviceDetails);

ServiceDetails updateServiceDetails(ServiceDetails serviceDetails);

void deleteServiceDetails(int id);

ServiceDetails searchServiceDetails(int id);

Object[] getServiceDetails();

class AdminServiceImpl implements AdminService{

public static ServiceDetails[] serviceDetailsArray;

private static ArrayList<ServiceDetails> serviceDetailsArrayList;

static {

serviceDetailsArray = new ServiceDetails[5];

serviceDetailsArrayList = new ArrayList<>();

@Override

public void createServiceDetails(ServiceDetails serviceDetails) {

serviceDetailsArrayList.add(serviceDetails);

serviceDetailsArray = serviceDetailsArrayList.toArray(new ServiceDetails[0]);

@Override

public ServiceDetails updateServiceDetails(ServiceDetails serviceDetails) {

ServiceDetails serviceDetails1 = null;


for(ServiceDetails serviceDetail : serviceDetailsArrayList){

if(serviceDetail.equals(serviceDetails)){

serviceDetailsArrayList.set(serviceDetailsArrayList.indexOf(serviceDetail),serviceDetails);

serviceDetails1 = serviceDetail;

if(serviceDetails1==null) throw new ServiceDetailsNotFoundException();

else serviceDetailsArray = serviceDetailsArrayList.toArray(new ServiceDetails[0]);

return serviceDetails1;

@Override

public void deleteServiceDetails(int id) {

ServiceDetails serviceDetail = null;

for(ServiceDetails serviceDetails : serviceDetailsArrayList){

if(serviceDetails.getServiceId() == id){

serviceDetail = serviceDetails;

serviceDetailsArrayList.remove(serviceDetails);

break;

if(serviceDetail == null) throw new ServiceDetailsNotFoundException();

else serviceDetailsArray = serviceDetailsArrayList.toArray(new ServiceDetails[0]);

@Override

public ServiceDetails searchServiceDetails(int id) {

ServiceDetails serviceDetail = null;

for(ServiceDetails serviceDetails : serviceDetailsArrayList){

if(serviceDetails.getServiceId() == id){
serviceDetail = serviceDetails;

if(serviceDetail==null) throw new ServiceDetailsNotFoundException();

else serviceDetailsArray = serviceDetailsArrayList.toArray(new ServiceDetails[0]);

return serviceDetail;

@Override

public ServiceDetails[] getServiceDetails() {

return serviceDetailsArrayList.toArray(new ServiceDetails[0]);

class ServiceDetailsNotFoundException extends RuntimeException{

ServiceDetailsNotFoundException(){

}
3-3 BOOKING DETAILS SERVICE
class BookingDetails{

private int bookingId;

private int customerId;

private int userId;

private String status;

BookingDetails(){

BookingDetails(int bookingId,int customerId,int userId,String status){

this.bookingId = bookingId;

this.customerId = customerId;

this.userId = userId;

this.status = status;

public int getBookingId(){

return bookingId;

public void setBookingId(int bookingId){

this.bookingId = bookingId;

public int getCustomerId(){

return customerId;

}
public void setCustomerId(int customerId){

this.customerId = customerId;

public int getUserId(){

return userId;

public void setUserId(int userId){

this.userId = userId;

public String getStatus(){

return status;

public void setStatus(String status){

this.status = status;

interface BookingDetailsService{

void addBookingDetails(BookingDetails bookingDetails);

BookingDetails updateBookingDetails(BookingDetails bookingDetails);

void deleteBookingDetails(int bookingId);

BookingDetails[] getBookingDetails();

BookingDetails[] searchByUserId(int customerId);

BookingDetails[] searchByBookingId(int bookingId);

}
class BookingDetailsServiceImpl implements BookingDetailsService{

public static BookingDetails[] bookingDetailsArray = new BookingDetails[5];

public void addBookingDetails(BookingDetails bookingDetails){

for(int i=0,j = bookingDetailsArray.length ; i<j ;i++){

if( bookingDetailsArray[i] == null ){

bookingDetailsArray[i] =bookingDetails;

break;

public BookingDetails updateBookingDetails(BookingDetails bookingDetails){

int i = 0 ;

int j = bookingDetailsArray.length;

for(; i < j; i++){

if( bookingDetailsArray[i].getBookingId() ==
bookingDetails.getBookingId() ){

bookingDetailsArray[i] = bookingDetails;

return bookingDetails;

return null;

public void deleteBookingDetails(int bookingId){

int i = 0 ;
int j = bookingDetailsArray.length;

for(; i < j; i++){

if( bookingDetailsArray[i].getBookingId() == bookingId ){

bookingDetailsArray[i] = null;

break;

public BookingDetails[] getBookingDetails(){

return bookingDetailsArray;

public BookingDetails[] searchByCustomerId(int customerId){

int i = 0 ;

int j = bookingDetailsArray.length;

BookingDetails[] customerBookingDetails = new BookingDetails[2];

int k = 0;

for(; i < j; i++){

if( bookingDetailsArray[i].getCustomerId() == customerId ){

customerBookingDetails[k++] = bookingDetailsArray[i];

if( k == 2 ){

return customerBookingDetails;

}
return customerBookingDetails;

public BookingDetails[] searchByUserId(int userId){

int i = 0 ;

int j = bookingDetailsArray.length;

BookingDetails[] customerBookingDetails = new BookingDetails[2];

int k = 0;

for(; i < j; i++){

if( bookingDetailsArray[i].getUserId() == userId ){

customerBookingDetails[k++] = bookingDetailsArray[i];

if( k == 2 ){

return customerBookingDetails;

return customerBookingDetails;

public BookingDetails[] searchByBookingId(int bookingId){

int i = 0 ;

int j = bookingDetailsArray.length;

BookingDetails[] customerBookingDetails = new BookingDetails[2];

int k = 0;

for(; i < j; i++){

if( bookingDetailsArray[i].getBookingId() == bookingId ){


customerBookingDetails[k++] = bookingDetailsArray[i];

if( k == 2 ){

return customerBookingDetails;

return customerBookingDetails;

interface UserService{

void addBookingDetails(BookingDetails bookingDetails);

BookingDetails[] getBookingDetails(int userId);

class UserServiceImpl implements UserService{

public void addBookingDetails(BookingDetails bookingDetails){

new BookingDetailsServiceImpl().addBookingDetails(bookingDetails);

public BookingDetails[] getBookingDetails(int userId){

return new BookingDetailsServiceImpl().searchByUserId( userId );

You might also like