From edf320e1505e5a4dd831404305f8211e91419173 Mon Sep 17 00:00:00 2001 From: XIVIXMMI Date: Mon, 29 Jan 2024 19:03:46 +0700 Subject: [PATCH 1/2] first commit --- .idea/misc.xml | 1 - .../example/baitap/Calculator.class | Bin 0 -> 1492 bytes .../example/baitap/PrimeChecker.class | Bin 0 -> 1513 bytes .../example/baitap/SecondLargestNumber.class | Bin 0 -> 1389 bytes .../SumDuplicateElementCalculator.class | Bin 0 -> 1332 bytes .../javacourse/example/baitap/Calculator.java | 40 +++++++++++++++++- .../example/baitap/PrimeChecker.java | 13 +++++- .../example/baitap/SecondLargestNumber.java | 15 ++++++- .../baitap/SumDuplicateElementCalculator.java | 9 +++- 9 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class create mode 100644 out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/PrimeChecker.class create mode 100644 out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SecondLargestNumber.class create mode 100644 out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/SumDuplicateElementCalculator.class 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/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class b/out/production/JavaProgramingCourse/nestech/javacourse/example/baitap/Calculator.class new file mode 100644 index 0000000000000000000000000000000000000000..a374b9f242b0fba4418539c07eca4294d717858f GIT binary patch literal 1492 zcmb7ETW=dh6#m9`yc=JdTN4u6zygKZN$PFV21wHcXq=V=rwzD7T%=0l&EibE-nG2z zC`df;h6K+@^cjgrytP89R8T>@AQ1c@kSKh!>ym)_!mh?=&Sk!H<~wKo%ftH*0G!90 zIUL6s1C@T*IGaNXvwAmY;#`jIc@r<1c*(@eCKj~!f{8^fFPd1=0m~*{G4ZN_*9=?| z$X-;wik1b^rSg_QW+m8m1qN!$cdvF^4Yz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OaKNqGJ?isGY?!|8r!U zhlMdp8SWm#TNr231$ANj+<)+#^17_8YHJhK$KPR2ThvSXG^O*^iP`CrKKl($kN3s9 zcUK>x@IV(2|NZlU#_{R+Pdu&lDm6vVU`;ZtN|7WZWSJyKh4E*o&(prd*ot?7Q3r4d yZ<9z>Zh@SNjU;T)InGMD(91!wEAk93(mI6fKhTBv8+kGL7qa}A%R5}j;L?8 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/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; } From 5be22816f3ddd46973a951e4e3c71936475fd97c Mon Sep 17 00:00:00 2001 From: XIVIXMMI Date: Fri, 15 Mar 2024 18:31:18 +0700 Subject: [PATCH 2/2] Exercises with file I/O --- .idea/encodings.xml | 7 + employee.dat | Bin 0 -> 352 bytes .../example/baitap/StudentManagement.java | 20 ++ .../baitap/exerciseswithfiles/Employee.java | 60 +++++ .../baitap/exerciseswithfiles/Main.java | 238 ++++++++++++++++++ .../javacourse/example/condition/Student.java | 47 ++++ .../javacourse/example/oop/Director.java | 13 + .../javacourse/example/oop/Doctor.java | 14 ++ .../javacourse/example/oop/Employee.java | 53 ++++ src/nestech/javacourse/example/oop/Nurse.java | 14 ++ .../javacourse/example/oop/SalaryCompute.java | 22 ++ .../javacourse/example/oop/StringUtil.java | 18 ++ 12 files changed, 506 insertions(+) create mode 100644 .idea/encodings.xml create mode 100644 employee.dat create mode 100644 src/nestech/javacourse/example/baitap/StudentManagement.java create mode 100644 src/nestech/javacourse/example/baitap/exerciseswithfiles/Employee.java create mode 100644 src/nestech/javacourse/example/baitap/exerciseswithfiles/Main.java create mode 100644 src/nestech/javacourse/example/condition/Student.java create mode 100644 src/nestech/javacourse/example/oop/Director.java create mode 100644 src/nestech/javacourse/example/oop/Doctor.java create mode 100644 src/nestech/javacourse/example/oop/Employee.java create mode 100644 src/nestech/javacourse/example/oop/Nurse.java create mode 100644 src/nestech/javacourse/example/oop/SalaryCompute.java create mode 100644 src/nestech/javacourse/example/oop/StringUtil.java 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/employee.dat b/employee.dat new file mode 100644 index 0000000000000000000000000000000000000000..17a11fb809266e9811d5caf1b8e043df04e80248 GIT binary patch literal 352 zcmZ9Hu};G<5QeXbgXq8lLj@8%f;+*27yu#BD(cdK*@R1SD6xajX%hw{9s?vK9)R~? z=RJ4_>{2@5!PedP|98JXz9C@?d-rOgWb3Gs=hmt|C3fBO*Wt^jdW&H&0m1Z$T>}8g zQiR)^&NQ;atdzl=nbtB&>{Q)UC~p-x)%X%^MvS~9SLRe9%PY@KkGL4UzwVse97iA~ z5UT>G5Yb%!HNPz4u+Sd_5UL6$0u{A@_eJ(U#ntIrc~E`un#x*Z;9~u8nyk0M{9TDU~5*i KJ^B>C3%VbpV_bp& literal 0 HcmV?d00001 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/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); + } +}