Merge pull request 'add Exception Examples' (#1) from exceptions into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package org;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package org.exceptions;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class FileService {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Enter you first name: ");
|
||||||
|
String firstName = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter you last name: ");
|
||||||
|
String lastName = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your national code: ");
|
||||||
|
Long nationalCode = scanner.nextLong();
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your email: ");
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your phone number: ");
|
||||||
|
Long phoneNumber = scanner.nextLong();
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your dateOfBirth: ");
|
||||||
|
String dateOfBirth = scanner.nextLine();
|
||||||
|
|
||||||
|
UserEntity user = null;
|
||||||
|
|
||||||
|
user = new UserEntity(firstName, lastName, nationalCode, email, phoneNumber, parseDate(dateOfBirth));
|
||||||
|
|
||||||
|
System.out.println(user.toString());
|
||||||
|
|
||||||
|
System.out.println(writeFile(user));
|
||||||
|
readFile();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String writeFile(UserEntity user) {
|
||||||
|
|
||||||
|
FileWriter fw = new FileWriter("userInformation.txt", true);
|
||||||
|
BufferedWriter bw = new BufferedWriter(fw);
|
||||||
|
|
||||||
|
bw.write(user.toString());
|
||||||
|
bw.close();
|
||||||
|
return "user information written to file";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void readFile() {
|
||||||
|
|
||||||
|
FileReader file = new FileReader("userInformation.txt");
|
||||||
|
|
||||||
|
BufferedReader fileInput = new BufferedReader(file);
|
||||||
|
|
||||||
|
for (int counter = 0; counter < 3; counter++)
|
||||||
|
System.out.println(fileInput.readLine());
|
||||||
|
|
||||||
|
fileInput.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Date parseDate(String date) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
Date dob = null;
|
||||||
|
|
||||||
|
dob = formatter.parse(date);
|
||||||
|
|
||||||
|
|
||||||
|
return dob;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package org.exceptions;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class RuntimeExceptionDemo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
UserEntity user = new UserEntity("Ali", "Ahmadi", 1234567890L,
|
||||||
|
null, 989111111111L, null);
|
||||||
|
|
||||||
|
validateEmail(user);
|
||||||
|
calculatePayment(5000, 0);
|
||||||
|
convertUserAge("twenty");
|
||||||
|
completeRegistration(user);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//nullPointerException
|
||||||
|
//IllegalArgumentException
|
||||||
|
public static void validateEmail(UserEntity user) {
|
||||||
|
|
||||||
|
String email = user.getEmail();
|
||||||
|
|
||||||
|
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
||||||
|
|
||||||
|
if (!Pattern.matches(emailRegex, email)) {
|
||||||
|
throw new IllegalArgumentException("Invalid email format");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("User email is valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArithmeticException
|
||||||
|
public static void calculatePayment(int totalAmount, int installmentCount) {
|
||||||
|
|
||||||
|
int payment = totalAmount / installmentCount;
|
||||||
|
|
||||||
|
System.out.println("Each installment : " + payment);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NumberFormatException
|
||||||
|
public static void convertUserAge(String ageInput) {
|
||||||
|
|
||||||
|
int age = Integer.parseInt(ageInput);
|
||||||
|
|
||||||
|
System.out.println("User age: " + age);
|
||||||
|
}
|
||||||
|
|
||||||
|
// IllegalStateException
|
||||||
|
public static void completeRegistration(UserEntity user) {
|
||||||
|
|
||||||
|
if (user.getDateOfBirth() == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Registration cannot be completed. Date of birth is missing."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Registration completed");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package org.exceptions;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class UserEntity {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Long nationalCode;
|
||||||
|
private String email;
|
||||||
|
private Long phoneNumber;
|
||||||
|
private Date dateOfBirth;
|
||||||
|
|
||||||
|
|
||||||
|
public UserEntity(String firstName, String lastName, Long nationalCode, String email, Long phoneNumber, Date dateOfBirth) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.nationalCode = nationalCode;
|
||||||
|
this.email = email;
|
||||||
|
this.phoneNumber = phoneNumber;
|
||||||
|
this.dateOfBirth = dateOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 Long getNationalCode() {
|
||||||
|
return nationalCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNationalCode(Long nationalCode) {
|
||||||
|
this.nationalCode = nationalCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getPhoneNumber() {
|
||||||
|
return phoneNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhoneNumber(Long phoneNumber) {
|
||||||
|
this.phoneNumber = phoneNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDateOfBirth() {
|
||||||
|
return dateOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfBirth(Date dateOfBirth) {
|
||||||
|
this.dateOfBirth = dateOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserEntity{" +
|
||||||
|
"firstName='" + firstName + '\'' +
|
||||||
|
", lastName='" + lastName + '\'' +
|
||||||
|
", nationalCode=" + nationalCode +
|
||||||
|
", email='" + email + '\'' +
|
||||||
|
", phoneNumber=" + phoneNumber +
|
||||||
|
", dateOfBirth=" + dateOfBirth +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
HELLO!
|
||||||
Reference in New Issue
Block a user