You are on page 1of 5

Mallika

21BBS0171
Q-1 Create a User Defined Exception, Implement a remainder ClockAPP to the person saying
Good Morning, Good Afternoon and Good Night by showing the current time. Write a code that
assigns different values to the String variable timeofDay based on the value of time in hours.
Raise remainder if time is 5 and 12, including 5 but not including 12, set timeofDay to "Morning,
Have Fresh Vegetable Juice and then Sugar Tablet with mild walking". Raise Exception, if time
is between 12 and 17, including 12 but not including 17, set timeofDay as "its Day Time, after
lunch have tablet to avoid sleep. If time is between 17 and 18.30. Including 17 but not including
18.30, set timeofDays as "Hello, Good Evening have a dinner" and finally, if time variable does
not satisfy all the above condition, set time as "Night, Go for sleep".

CODE

class TimeOfDayException extends Exception {


TimeOfDayException(String message) {
super(message);
}
}

public class ClockApp {


public static void main(String[] args) {
try {

int currentTime = 16;


String timeOfDay = getTimeOfDay(currentTime);
System.out.println("Good " + timeOfDay);

} catch (TimeOfDayException e) {
System.out.println("Exception: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}

private static String getTimeOfDay(int currentTime) throws TimeOfDayException {


if (5 <= currentTime && currentTime < 12) {
return "Morning, Have Fresh Vegetable Juice and then Sugar Tablet with mild walking";
} else if (12 <= currentTime && currentTime < 17) {
throw new TimeOfDayException("It's Day Time, after lunch have tablet to avoid sleep.");
} else if (17 <= currentTime && currentTime < 18.30) {
return "Hello, Good Evening, have a dinner";
} else {
return "Night, Go for sleep";
}
}
}

OUTPUT

Q-2 A banking website wants their customer to enter the CIF number as username and user
created password to enable internet banking. Assume the necessary input to perform the
requirement. The following criteria are to be followed,

(1) The CIF number should be combination of their name first two. letters with initials), year of
birth and last four digit of account number (5 marks)

Ex. name of the A/c holder-X.AXUW

Date of birth (dd/mm/yy) 10/10/2000

A/c number -101010104567

Then the username should be created as XA20004567. If the criteria are not obtained display
an exception as "invalid CIF number"

(ii) The password should be restricted as eight characters with Minimum One Capital. Minimum
One numeric and with atleast one special character (except"-Underscorej. If the criteria are not
obtained display an exception as "invalid pwd"

CODE

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class InvalidCIFException extends Exception {


InvalidCIFException(String message) {
super(message);
}
}
class InvalidPasswordException extends Exception {
InvalidPasswordException(String message) {
super(message);
}
}

public class InternetBankingRegistration {

public static void main(String[] args) {


try {
String nameInitials = "XA";
int yearOfBirth = 2000;
String accountNumber = "101010104567";

String cifNumber = generateCIFNumber(nameInitials, yearOfBirth, accountNumber);


System.out.println("Generated CIF Number: " + cifNumber);

String password = generatePassword();


System.out.println("Generated Password: " + password);

} catch (InvalidCIFException | InvalidPasswordException e) {


System.out.println("Exception: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}

private static String generateCIFNumber(String nameInitials, int yearOfBirth, String


accountNumber) throws InvalidCIFException {
if (nameInitials.length() >= 2 && yearOfBirth >= 1900 && yearOfBirth <= 9999 &&
accountNumber.length() >= 4) {
String yearStr = String.valueOf(yearOfBirth).substring(2);
String lastFourDigits = accountNumber.substring(accountNumber.length() - 4);

return nameInitials.substring(0, 2) + yearStr + lastFourDigits;


} else {
throw new InvalidCIFException("Invalid CIF number");
}
}

private static String generatePassword() throws InvalidPasswordException {


String passwordRegex = "^(?=.[A-Z])(?=.[0-9])(?=.*[^A-Za-z0-9_]).{8}$";
Pattern pattern = Pattern.compile(passwordRegex);
String password = "Abcdefg1@";

Matcher matcher = pattern.matcher(password);


if (matcher.matches()) {
return password;
} else {
throw new InvalidPasswordException("Invalid password");
}
}
}

OUTPUT

You might also like