diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..766cbcf
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index df7c718..8b62440 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,3 @@
-
diff --git a/employee.dat b/employee.dat
new file mode 100644
index 0000000..17a11fb
Binary files /dev/null and b/employee.dat differ
diff --git a/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class
new file mode 100644
index 0000000..a374b9f
Binary files /dev/null and b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class differ
diff --git a/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/PrimeChecker.class b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/PrimeChecker.class
new file mode 100644
index 0000000..5f863d0
Binary files /dev/null and b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/PrimeChecker.class differ
diff --git a/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SecondLargestNumber.class b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SecondLargestNumber.class
new file mode 100644
index 0000000..26165b8
Binary files /dev/null and b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SecondLargestNumber.class differ
diff --git a/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.class b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.class
new file mode 100644
index 0000000..b1aeea0
Binary files /dev/null and b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.class differ
diff --git a/src/nestech/javacourse/example/baitap/Calculator.java b/src/nestech/javacourse/example/baitap/Calculator.java
index af06b7f..1366e73 100644
--- a/src/nestech/javacourse/example/baitap/Calculator.java
+++ b/src/nestech/javacourse/example/baitap/Calculator.java
@@ -11,6 +11,44 @@
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
- // implement your code here
+ boolean isRunning = true;
+
+ while(isRunning){
+ System.out.println("1. Summation (+) ");
+ System.out.println("2. Subtraction (-) ");
+ System.out.println("3. Multiplication (*)");
+ System.out.println("4. Division (/) ");
+ System.out.println("0. Exit");
+ int choice = scanner.nextInt();
+
+ if(choice != 0){
+ System.out.println("Enter first number: ");
+ double firstNumber = scanner.nextDouble();
+ System.out.println("Enter second number: ");
+ double secondNumber = scanner.nextDouble();
+
+ switch (choice){
+ case 1:
+ System.out.println(firstNumber + secondNumber);
+ break;
+ case 2:
+ System.out.println(firstNumber - secondNumber);
+ break;
+ case 3:
+ System.out.println(firstNumber * secondNumber);
+ break;
+ case 4:
+ System.out.println(firstNumber / secondNumber);
+ break;
+ default:
+ System.out.println("The choice is invalid please enter again");
+ }
+
+ }
+ else {
+ System.out.println("Closing application...");
+ isRunning = false;
+ }
+ }
}
}
diff --git a/src/nestech/javacourse/example/baitap/PrimeChecker.java b/src/nestech/javacourse/example/baitap/PrimeChecker.java
index 9c7db00..e65d443 100644
--- a/src/nestech/javacourse/example/baitap/PrimeChecker.java
+++ b/src/nestech/javacourse/example/baitap/PrimeChecker.java
@@ -27,8 +27,17 @@ public static void main(String[] args) {
public static boolean isPrime(int n) {
boolean isValid = false;
- // Implement your code here
-
+ if (n > 2){
+ for(int i = 2; i < Math.sqrt(n); i++){
+ if(n % i != 0){
+ isValid = true;
+ break;
+ }
+ else {
+ isValid = false;
+ }
+ }
+ }
return isValid;
}
}
diff --git a/src/nestech/javacourse/example/baitap/SecondLargestNumber.java b/src/nestech/javacourse/example/baitap/SecondLargestNumber.java
index f9f5de4..29f013d 100644
--- a/src/nestech/javacourse/example/baitap/SecondLargestNumber.java
+++ b/src/nestech/javacourse/example/baitap/SecondLargestNumber.java
@@ -20,7 +20,20 @@ public static void main(String[] args) {
public static int findSecondLargest(int[] arr) {
int secondLargest = Integer.MIN_VALUE;
- // Implement your code here
+ int largest = Integer.MIN_VALUE; // đảm bảo các giá trị của số nguyên trong mảng sẽ lớn hơn giá trị ban đầu
+
+ if(arr.length < 2){
+ return Integer.MIN_VALUE;
+ }
+
+ for (int num : arr){
+ if(num > largest){
+ secondLargest = largest;
+ largest = num;
+ }else if(num > secondLargest && num != largest){
+ secondLargest = num;
+ }
+ }
return secondLargest;
}
diff --git a/src/nestech/javacourse/example/baitap/StudentManagement.java b/src/nestech/javacourse/example/baitap/StudentManagement.java
new file mode 100644
index 0000000..62dbfcf
--- /dev/null
+++ b/src/nestech/javacourse/example/baitap/StudentManagement.java
@@ -0,0 +1,20 @@
+package nestech.javacourse.example.baitap;
+
+
+import nestech.javacourse.example.condition.Student;
+
+public class StudentManagement {
+ public static void main(String[] args) {
+ Student[] stdArray = new Student[3];
+
+ stdArray[0] = new Student(1,"Nguyen","Le",9);
+ stdArray[1] = new Student(1,"Hieu","Nguyen",8);
+ stdArray[2] = new Student(1,"Ly","Pham",10);
+
+ for(Student score : stdArray){
+ if(score.getScore() > 5){
+ System.out.println(score.getFirstName());
+ }
+ }
+ }
+}
diff --git a/src/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.java b/src/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.java
index 198ee95..1427971 100644
--- a/src/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.java
+++ b/src/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.java
@@ -18,7 +18,14 @@ public static void main(String[] args) {
public static int calculateCommonElementsSum(int[] arr1, int[] arr2) {
int sum = 0;
- // Implement your code here
+ for(int i = 0; i < arr1.length; i++){
+ for( int j = 0; j < arr2.length; j++){
+ if(arr1[i] == arr2[j]){
+ sum += arr1[i] + arr2[j];
+ break;
+ }
+ }
+ }
return sum;
}
diff --git a/src/nestech/javacourse/example/baitap/exerciseswithfiles/Employee.java b/src/nestech/javacourse/example/baitap/exerciseswithfiles/Employee.java
new file mode 100644
index 0000000..5dc1fb5
--- /dev/null
+++ b/src/nestech/javacourse/example/baitap/exerciseswithfiles/Employee.java
@@ -0,0 +1,60 @@
+package nestech.javacourse.example.baitap.exerciseswithfiles;
+
+import java.io.Serializable;
+
+public class Employee implements Serializable {
+
+ private long id;
+ private String name;
+ private int age;
+ private String position;
+
+ public Employee(long id, String name, int age, String position) {
+ this.id = id;
+ this.name = name;
+ this.age = age;
+ this.position = position;
+ }
+
+ @Override
+ public String toString() {
+ return "Employee{" +
+ "id=" + id +
+ ", name='" + name + '\'' +
+ ", age='" + age + '\'' +
+ ", position='" + position + '\'' +
+ '}';
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getPosition() {
+ return position;
+ }
+
+ public void setPosition(String position) {
+ this.position = position;
+ }
+}
diff --git a/src/nestech/javacourse/example/baitap/exerciseswithfiles/Main.java b/src/nestech/javacourse/example/baitap/exerciseswithfiles/Main.java
new file mode 100644
index 0000000..1d3856b
--- /dev/null
+++ b/src/nestech/javacourse/example/baitap/exerciseswithfiles/Main.java
@@ -0,0 +1,238 @@
+package nestech.javacourse.example.baitap.exerciseswithfiles;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Scanner;
+
+public class Main {
+
+ private static List employeeList = new ArrayList<>();
+ private static int currentIndex;
+
+ private static Scanner scanner = new Scanner(System.in);
+
+ public static void main(String[] args) {
+
+ String filePath = "employee.dat";
+ boolean run = true;
+
+ while (run){
+
+ System.out.println("EMPLOYEE MANAGEMENT");
+ System.out.println("1. Open file data");
+ System.out.println("2. Display list of employee");
+ System.out.println("3. Display First employee");
+ System.out.println("4. Display Previous employee");
+ System.out.println("5. Display Next employee");
+ System.out.println("6. Display Last employee");
+ System.out.println("7. Input new employee");
+ System.out.println("8. Delete a employee");
+ System.out.println("9. Find employee");
+ System.out.println("10. Save and exit");
+
+ int choice = scanner.nextInt();
+
+ switch (choice){
+ case 1:
+ readFile(filePath);
+ break;
+ case 2:
+ printListEmployee();
+ break;
+ case 3:
+ printEmployeeAtIndex(0);
+ break;
+ case 4:
+ printPreviousEmployee();
+ break;
+ case 5:
+ printNextEmployee();
+ break;
+ case 6:
+ printLastEmployee();
+ break;
+ case 7:
+ addNewEmployee();
+ break;
+ case 8:
+ removeEmployee();
+ break;
+ case 9:
+ findEmployee();
+ break;
+ case 10:
+ writeFile(filePath);
+ System.out.println("Saved!!! ");
+ System.out.println("Closing program...");
+ run = false;
+ break;
+ default:
+ System.out.println("The choose isn't valid, please enter again");
+ break;
+ }
+ }
+ }
+
+ private static void findEmployee() {
+
+ System.out.println("Enter employee name to search: ");
+ String findName = scanner.next();
+
+ boolean found = false;
+ for(Employee tempEmployee : employeeList){
+ if(tempEmployee.getName().equals(findName)){
+ System.out.println("Employee found: " + tempEmployee);
+ found = true;
+ break;
+ }
+ }
+ if(!found){
+ System.out.println("Can't found employee " + findName);
+ }
+ }
+
+ private static void removeEmployee() {
+
+ System.out.println("Enter employee name to remove: ");
+ String removeName = scanner.next();
+
+ boolean removed = false;
+ Iterator iterator = employeeList.iterator();
+ while (iterator.hasNext()){
+ Employee employee = iterator.next();
+ if(employee.getName().equals(removeName)){
+ iterator.remove();
+ removed = true;
+ break;
+ }
+ }
+ if(removed){
+ System.out.println("Employee removed successfully");
+ }
+ else{
+ System.out.println("Can't found employee " + removeName);
+ }
+
+ }
+
+ private static void addNewEmployee() {
+
+ System.out.println("Enter id: ");
+ int id = scanner.nextInt();
+
+ System.out.println("Enter name: ");
+ String name = scanner.next();
+
+ System.out.println("Enter age: ");
+ int age = scanner.nextInt();
+
+ scanner.nextLine(); // Consume the newline character
+
+ System.out.println("Enter position: ");
+ String position = scanner.nextLine();
+
+ Employee newEmployee = new Employee(id, name, age, position);
+ employeeList.add(newEmployee);
+ System.out.println("Employee added successfully.");
+ }
+
+ private static void printLastEmployee() {
+
+ if(employeeList != null && !employeeList.isEmpty()){
+ currentIndex = employeeList.size() - 1;
+ System.out.println("Previous Employee: " + currentIndex);
+ System.out.println(employeeList.get(currentIndex) + "\n");
+ }
+ else{
+ System.out.println("No employee available.");
+ }
+
+ }
+
+ private static void printNextEmployee() {
+
+ if(currentIndex < employeeList.size() - 1){
+ currentIndex ++;
+ printEmployeeAtIndex(currentIndex);
+ }else{
+ System.out.println("No employee available.");
+ }
+ }
+
+ private static void printPreviousEmployee() {
+
+ if(currentIndex > 0){
+ currentIndex--;
+ printEmployeeAtIndex(currentIndex);
+ }
+ else{
+ System.out.println("No employee available.");
+ }
+ }
+
+ private static void printEmployeeAtIndex(int index) {
+
+ if(employeeList.isEmpty()){
+ System.out.println("No employee available.");
+ return;
+ }
+
+ currentIndex = index;
+ System.out.println("Current Employee: " + currentIndex);
+ System.out.println(employeeList.get(currentIndex) + "\n");
+ }
+
+ private static void writeFile(String filePath) {
+ try{
+ FileOutputStream fos = new FileOutputStream(filePath);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+
+// Employee[] employees = {
+// new Employee(1,"John",30,"Boss"),
+// new Employee(2,"Christine",26,"Staff"),
+// new Employee(3,"Jimmy",25,"Staff"),
+// new Employee(4,"Henry",30,"Staff"),
+// };
+
+ oos.writeObject(employeeList);
+
+ oos.close();
+ fos.close();
+
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static void printListEmployee() {
+ if (employeeList != null) {
+ for (Employee tempEmployee : employeeList) {
+ System.out.println(tempEmployee);
+ }
+ System.out.println("Total employee: " + employeeList.size() + "\n");
+ } else {
+ System.out.println("No employee data available.");
+ }
+ }
+
+ private static void readFile(String filePath){
+ try{
+ FileInputStream fis = new FileInputStream(filePath);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+
+ //Employee[] employees = (Employee[]) ois.readObject();
+
+ //employeeList = new ArrayList<>(Arrays.asList(employees));
+
+ employeeList = (ArrayList) ois.readObject();
+
+ System.out.println("Data loaded successfully!!" + "\n");
+ ois.close();
+ fis.close();
+ } catch (IOException | ClassNotFoundException e) {
+ System.out.println("Error loading data from file");
+ }
+ }
+}
diff --git a/src/nestech/javacourse/example/condition/Student.java b/src/nestech/javacourse/example/condition/Student.java
new file mode 100644
index 0000000..48b3f59
--- /dev/null
+++ b/src/nestech/javacourse/example/condition/Student.java
@@ -0,0 +1,47 @@
+package nestech.javacourse.example.condition;
+
+public class Student {
+ private long id;
+ private String firstName;
+ private String lastName;
+ private float score;
+
+ public Student(long id, String firstName, String lastName, float score) {
+ this.id = id;
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.score = score;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ 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 float getScore() {
+ return score;
+ }
+
+ public void setScore(float score) {
+ this.score = score;
+ }
+}
diff --git a/src/nestech/javacourse/example/oop/Director.java b/src/nestech/javacourse/example/oop/Director.java
new file mode 100644
index 0000000..a62860e
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/Director.java
@@ -0,0 +1,13 @@
+package nestech.javacourse.example.oop;
+
+public class Director extends Employee{
+ public Director(String firstName,String lastName,double basicSalary) {
+ setFirstName(firstName);
+ setLastName(lastName);
+ setBasicSalary(basicSalary);
+ }
+ @Override
+ double getTotalSalary() {
+ return super.getBasicSalary() * super.getBasicSalary()*30/100 + 5000000;
+ }
+}
diff --git a/src/nestech/javacourse/example/oop/Doctor.java b/src/nestech/javacourse/example/oop/Doctor.java
new file mode 100644
index 0000000..1528eea
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/Doctor.java
@@ -0,0 +1,14 @@
+package nestech.javacourse.example.oop;
+
+public class Doctor extends Employee{
+ public Doctor(String firstName,String lastName,double basicSalary) {
+ setFirstName(firstName);
+ setLastName(lastName);
+ setBasicSalary(basicSalary);
+ }
+
+ @Override
+ double getTotalSalary() {
+ return super.getBasicSalary() + super.getBasicSalary()*20/100 ;
+ }
+}
diff --git a/src/nestech/javacourse/example/oop/Employee.java b/src/nestech/javacourse/example/oop/Employee.java
new file mode 100644
index 0000000..fd4af5c
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/Employee.java
@@ -0,0 +1,53 @@
+package nestech.javacourse.example.oop;
+
+
+/*
+* Viết chương trình tính lương của nhân viên,
+* Nhân viên có 3 chức vụ: Y Tá, Bác Sĩ, Giám đốc
+* Nhân Viên có họ tên, lương cơ bản
+* Đối với bác sĩ lương = lương cơ bản + lương cơ bản * 20%
+* Đối với giám đốc lương = lương cơ bản + lương cơ bản * 30% + 5 000 000 */
+public abstract class Employee {
+ private String firstName;
+ private String lastName;
+ protected double basicSalary;
+
+ public String getFullName() {
+ return firstName + lastName;
+ }
+
+ abstract double getTotalSalary();
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ boolean validate(double basicSalary) {
+ if (basicSalary < 0) {
+ return false;
+ }
+ return true;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public double getBasicSalary() {
+ return basicSalary;
+ }
+
+ public void setBasicSalary(double basicSalary) {
+ this.basicSalary = basicSalary;
+ }
+}
+
+
diff --git a/src/nestech/javacourse/example/oop/Nurse.java b/src/nestech/javacourse/example/oop/Nurse.java
new file mode 100644
index 0000000..954db3a
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/Nurse.java
@@ -0,0 +1,14 @@
+package nestech.javacourse.example.oop;
+
+public class Nurse extends Employee {
+ public Nurse(String firstName,String lastName,double basicSalary) {
+ setFirstName(firstName);
+ setLastName(lastName);
+ setBasicSalary(basicSalary);
+ }
+
+ @Override
+ double getTotalSalary() {
+ return super.getBasicSalary();
+ }
+}
diff --git a/src/nestech/javacourse/example/oop/SalaryCompute.java b/src/nestech/javacourse/example/oop/SalaryCompute.java
new file mode 100644
index 0000000..58f6245
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/SalaryCompute.java
@@ -0,0 +1,22 @@
+package nestech.javacourse.example.oop;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class SalaryCompute {
+ public static void main(String[] args) {
+ List employees = new ArrayList<>();
+ employees.add(new Nurse("Sang","Le",9000000));
+ employees.add(new Nurse("Nghi","Tran",6000000));
+ employees.add((new Doctor("Hoang","Pham",15000000)));
+ employees.add((new Doctor("Nguyen","Le",30000000)));
+
+ Double sum = 0D;
+ for (Employee tempEmployees : employees){
+ if (tempEmployees.validate(tempEmployees.basicSalary)) {
+ sum += tempEmployees.getTotalSalary();
+ }
+ }
+ System.out.println("Total salary have to pay: " + sum.longValue());
+ }
+}
diff --git a/src/nestech/javacourse/example/oop/StringUtil.java b/src/nestech/javacourse/example/oop/StringUtil.java
new file mode 100644
index 0000000..8ff6b6e
--- /dev/null
+++ b/src/nestech/javacourse/example/oop/StringUtil.java
@@ -0,0 +1,18 @@
+package nestech.javacourse.example.oop;
+
+public class StringUtil {
+ public static void main(String[] args) {
+ String[] arr = "1, 3, 4, 6, 7, 8".split(",");
+ int sum = 0;
+ for(String num : arr){
+ try {
+ String n = num.trim();
+ if(n!=null )
+ sum += Integer.parseInt(num.trim());
+ }catch (Exception e){
+ System.out.println( "invalid number");
+ }
+ }
+ System.out.println(sum);
+ }
+}