fanjialong 3 dienas atpakaļ
vecāks
revīzija
34377839ba

+ 35 - 0
java-base-project10/day16/src/com/sf/_01_字符串练习/Test.java

@@ -0,0 +1,35 @@
+package com.sf._01_字符串练习;
+
+import java.util.Scanner;
+
+public class Test {
+    public static void main(String[] args) {
+//        String fileName = "a.jpg";
+//        String fileName1 = "b.txt";
+//        String fileName2 = "c.png";
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请输入上传文件名字");
+        String fileName = scanner.next();
+        String extName = getFileExt(fileName);
+        //判断文件后缀是否是jpg 和png
+        if(extName.equals("png")|| extName.equals("jpg")){
+            System.out.println("上传成功");
+        }else{
+            System.out.println("文件上传格式不正确!必须是jpg 和png 格式");
+        }
+        // 实际网站在做文件上传的时候
+        /**
+         * 上传头像格式必须 jpg , png
+         */
+    }
+
+    public static String getFileExt(String str){
+        // 判断字符串是否为空 或者是""
+        if(str == null || "".equals(str)){
+            return null;
+        }
+        // 先获取. 最后索引位置
+        int index = str.lastIndexOf(".");
+        return str.substring(index+1);
+    }
+}

+ 22 - 0
java-base-project10/day16/src/com/sf/_02_可变字符串stringBuild/Test.java

@@ -0,0 +1,22 @@
+package com.sf._02_可变字符串stringBuild;
+
+public class Test {
+    public static void main(String[] args) {
+        /**
+         * 创建StringBuild
+         */
+        StringBuilder stringBuilder = new StringBuilder();
+//        StringBuilder stringBuilder1 = new StringBuilder(64);
+
+//        stringBuilder.append("abc");
+//        stringBuilder.append("fanjialong");
+        //方法调用支持链式编程    类.方法().方法().方法()
+        stringBuilder.append("abc").append("bcd");
+        // 查看长度
+        stringBuilder.length();
+        // 把可变字符串变成不可变字符串
+        String str = stringBuilder.toString();
+
+        System.out.println(stringBuilder);
+    }
+}

+ 18 - 0
java-base-project10/day16/src/com/sf/_02_可变字符串stringBuild/Test1.java

@@ -0,0 +1,18 @@
+package com.sf._02_可变字符串stringBuild;
+
+public class Test1 {
+    public static void main(String[] args) {
+        int[] arr = {1,2,3,4,5};
+        //{1,2,3,4,5}
+        StringBuilder sb = new StringBuilder();
+        sb.append("{");
+        for (int i = 0; i < arr.length; i++) {
+            if(i == arr.length-1){
+                sb.append(arr[i]+"}");
+            }else{
+                sb.append(arr[i]+",");
+            }
+        }
+        System.out.println(sb);
+    }
+}

+ 33 - 0
java-base-project10/day16/src/com/sf/_03_math/Test.java

@@ -0,0 +1,33 @@
+package com.sf._03_math;
+
+public class Test {
+    public static void main(String[] args) {
+        /**
+         * abs 求绝对值
+         */
+//        System.out.println(Math.abs(-1));
+//
+//        /**
+//         * max / min
+//         */
+//        System.out.println(Math.max(2,3));
+//        System.out.println(Math.min(2,3));
+//
+//        /**
+//         * random: 创建一个0-1 随机数
+//         */
+//        System.out.println(Math.random());
+//
+//        /**
+//         * round : 四舍五入操作
+//         */
+//        System.out.println(Math.round(4.3));
+
+
+        String[] arr = {"奖品1","奖品2","奖品3","奖品4","奖品5",};
+
+        //通过Math random() 0-1   让他创建随机是在数组索引范围内
+        int index = (int) (Math.random() * arr.length);
+        System.out.println(arr[index]);
+    }
+}

+ 27 - 0
java-base-project10/day16/src/com/sf/_04_random/Test.java

@@ -0,0 +1,27 @@
+package com.sf._04_random;
+
+import java.util.Random;
+import java.util.Scanner;
+
+public class Test {
+    public static void main(String[] args) {
+        //1 创建出来一个1-100 随机数
+        Random random = new Random();
+        //0-99   1-100
+        int randomNumber = random.nextInt(100) + 1;
+        Scanner scanner = new Scanner(System.in);
+
+        while (true){
+            System.out.println("请输入要猜测数字");
+            int number = scanner.nextInt();
+            if(number > randomNumber){
+                System.out.println("猜大了");
+            }else if(number < randomNumber){
+                System.out.println("猜小了");
+            }else{
+                System.out.println("恭喜你猜对了");
+                break;
+            }
+        }
+    }
+}

+ 16 - 0
java-base-project10/day16/src/com/sf/_05_uuid/FileUploadUtils.java

@@ -0,0 +1,16 @@
+package com.sf._05_uuid;
+
+import java.util.UUID;
+
+public class FileUploadUtils {
+
+    public static String generateUniqueFileName(String orignFileName){
+        // 1 获取文件后缀
+        int index = orignFileName.lastIndexOf(".");
+        String ext = orignFileName.substring(index);
+        // 2 生成uuid
+        String uuid = UUID.randomUUID().toString().replace("-", "");
+        // 3 拼接新文件名
+        return uuid + ext;
+    }
+}

+ 13 - 0
java-base-project10/day16/src/com/sf/_05_uuid/Test.java

@@ -0,0 +1,13 @@
+package com.sf._05_uuid;
+
+import java.util.UUID;
+
+public class Test {
+    public static void main(String[] args) {
+        // 通过uuid 创建唯一标识 字符串
+        String s = UUID.randomUUID().toString();
+        String replaceStr = s.replace("-", "");
+        System.out.println(s);
+        System.out.println(replaceStr);
+    }
+}

+ 9 - 0
java-base-project10/day16/src/com/sf/_05_uuid/Test1.java

@@ -0,0 +1,9 @@
+package com.sf._05_uuid;
+
+public class Test1 {
+
+    public static void main(String[] args) {
+        String fileName = "1.png";
+        System.out.println(FileUploadUtils.generateUniqueFileName(fileName));
+    }
+}

+ 33 - 0
java-base-project10/day16/src/com/sf/_06_date/Test.java

@@ -0,0 +1,33 @@
+package com.sf._06_date;
+
+import java.util.Date;
+
+public class Test {
+    public static void main(String[] args) throws InterruptedException {
+        Date date1 = new Date();
+
+        System.out.println(date1.toLocaleString());
+        /**
+         * 让程序先睡眠10s中
+         * Thread.sleep(10000);
+         */
+//        Thread.sleep(5000);
+//        Date date2 = new Date();
+//        /**
+//         * 日期比较如何进行比较
+//         * 方式1 : 用两个日期毫秒值进行比较
+//         * 方式2 : date 提供api  before  after
+//         */
+////        if(date1.getTime() > date2.getTime()){
+////            System.out.println("date1日期更大");
+////        }else{
+////            System.out.println("date2日期更大");
+////        }
+//        boolean flag = date1.before(date2);
+//        if(flag){
+//            System.out.println("date1 在date2 之前");
+//        }else{
+//            System.out.println("date1 在date2 之后");
+//        }
+    }
+}

+ 44 - 0
java-base-project10/day16/src/com/sf/_07_simple_date_fomat/Test.java

@@ -0,0 +1,44 @@
+package com.sf._07_simple_date_fomat;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Scanner;
+
+public class Test {
+    public static void main(String[] args) throws ParseException {
+        /**
+         * SimpleDateFormat类主要作用就是做日期格式化
+         * 如果不满足默认日期格式 就可以使用这个类做定制化日期格式
+         *
+         * 两个api
+         * String format(Date date)   把日期转成字符串
+         * Date  parse(String date)   把字符串转成日期
+         *
+         * 构造器
+         * y 年   M 月  d 天   H 小时  m 分钟  s 秒
+         * SimpleDateFormat(String pattern)
+         *
+         */
+//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+//        String str = sdf.format(new Date());
+//        System.out.println(str);
+//        // 把这个字符串转成日期
+//        Date date = sdf.parse(str);
+//        System.out.println(date);
+
+        /**
+         * 在控制台当中录入2026-1-10 12:00:00  信息
+         * 最终在控制台打印2026年1月10日 12点00分00秒
+         */
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入日期");
+        String dateStr = scanner.nextLine();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
+        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        // 把输入时间转成日期
+        Date d = sdf1.parse(dateStr);
+        System.out.println(sdf.format(d));
+
+    }
+}

+ 52 - 0
java-base-project10/day16/src/com/sf/_08_calandar/Test.java

@@ -0,0 +1,52 @@
+package com.sf._08_calandar;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class Test {
+    public static void main(String[] args) {
+        //先获取日历类
+        /**
+         * Caladendar.getInstance()
+         * Calendar 日历类:
+         * 主要作用, 可以对于日期进行相加详见操作
+         * 比如让天数+1   让月+1  让天数-1
+         */
+        Calendar instance = Calendar.getInstance();
+
+        /**
+         * setTime(Date date)
+         * 设置修改日期
+         * 2026 1/9  13:44
+         */
+        instance.setTime(new Date());
+        /**
+         * 获取当前年  月  日   时 分 秒
+         * instance.get();
+         */
+//        System.out.println(instance.get(Calendar.YEAR));
+        // month 是从0开始
+//        System.out.println(instance.get(Calendar.MONTH)+1);
+//        System.out.println(instance.get(Calendar.HOUR));
+//        System.out.println(instance.get(Calendar.MINUTE));
+//        System.out.println(instance.get(Calendar.SECOND));
+        /**
+         * add()
+         */
+        instance.add(Calendar.DAY_OF_MONTH,1);
+        System.out.println(instance.get(Calendar.DAY_OF_MONTH));
+        Date time = instance.getTime();
+        System.out.println(time.toLocaleString());
+
+
+        /**
+         * 在控制台当中输入
+         * 2026/1/9  13:52
+         *
+         * 现在要求将小时+1   将月 +1  将天数+2
+         * 最终输出格式为 2026_2_11  14:52
+         */
+
+
+    }
+}

+ 33 - 0
java-base-project10/day16/src/com/sf/_08_calandar/Test1.java

@@ -0,0 +1,33 @@
+package com.sf._08_calandar;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Scanner;
+
+public class Test1 {
+    public static void main(String[] args) throws ParseException {
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入日期格式 xxxx/xx/xx  xx:xx");
+        String str = scanner.nextLine();
+        // 创建sdf 格式话日期
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+        // 把输入内容转成日期类型才可以做后面+操作
+        Date date = sdf.parse(str);
+        // 创建日历类
+        Calendar instance = Calendar.getInstance();
+        // 把日期存到日历中方便操作
+        instance.setTime(date);
+        //将小时+1  将月+1  将天+1
+        instance.add(Calendar.DAY_OF_MONTH,1);
+        instance.add(Calendar.MONTH,1);
+        instance.add(Calendar.HOUR,1);
+        Date changeDate = instance.getTime();
+        // 创建sdf 格式日期
+        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy_MM_dd HH:mm");
+        String formatDate = sdf1.format(changeDate);
+        System.out.println(formatDate);
+
+    }
+}

+ 65 - 0
java-base-project10/day16/src/com/sf/_09_object/Student.java

@@ -0,0 +1,65 @@
+package com.sf._09_object;
+
+import java.util.Objects;
+
+public class Student implements Cloneable{
+    private String name;
+    private int age;
+
+    public Student() {
+    }
+
+    public Student(String name, int age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+
+    @Override
+    protected Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        Student student = (Student) o;
+        return age == student.age &&
+                Objects.equals(name, student.name);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name, age);
+    }
+
+//
+//    @Override
+//    public int hashCode() {
+//        return Objects.hash(name, age);
+//    }
+
+    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;
+    }
+}

+ 24 - 0
java-base-project10/day16/src/com/sf/_09_object/Test.java

@@ -0,0 +1,24 @@
+package com.sf._09_object;
+
+public class Test {
+    public static void main(String[] args) throws CloneNotSupportedException {
+        Student student = new Student("zhangsan",10);
+        Student student1 = new Student("zhangsan",10);
+
+        Student cloneObj = (Student) student1.clone();
+
+        System.out.println("克隆对象的名字:"+ cloneObj.getName());
+        // getClass -> 后面再讲反射会用到
+        System.out.println(student.getClass());
+
+        // 计算hashCode 值 默认是根据对象地址计算
+        System.out.println(student.hashCode());
+        System.out.println(student1.hashCode());
+
+
+        // 比较对象内容是否相同 前提你必须要重写object 当中equlas方法
+        System.out.println(student.equals(student1));
+        // 比较地址
+        System.out.println(student==student1);
+    }
+}

+ 69 - 0
java-base-project10/day16/src/com/sf/_10_arrayList/Student.java

@@ -0,0 +1,69 @@
+package com.sf._10_arrayList;
+
+public class Student {
+    private String name;
+    private int age;
+    private double chineseScore;
+
+
+    public Student() {
+    }
+
+    public Student(String name, int age, double chineseScore) {
+        this.name = name;
+        this.age = age;
+        this.chineseScore = chineseScore;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public int getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return chineseScore
+     */
+    public double getChineseScore() {
+        return chineseScore;
+    }
+
+    /**
+     * 设置
+     * @param chineseScore
+     */
+    public void setChineseScore(double chineseScore) {
+        this.chineseScore = chineseScore;
+    }
+
+    public String toString() {
+        return "Student{name = " + name + ", age = " + age + ", chineseScore = " + chineseScore + "}";
+    }
+}

+ 59 - 0
java-base-project10/day16/src/com/sf/_10_arrayList/Test.java

@@ -0,0 +1,59 @@
+package com.sf._10_arrayList;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Test {
+    public static void main(String[] args) {
+        /**
+         * 创建list
+         * new ArrayList();
+         */
+        List<String> list = new ArrayList<>();
+        list.add("zhangsan");
+        list.add("lisi");
+        list.add("wangwu");
+
+        /**
+         * 集合.add(元素内容);
+         */
+        System.out.println(list);
+        /**
+         * 集合.remove(索引)
+         */
+//        list.remove(0);
+        list.remove("lisi");
+        System.out.println(list);
+
+
+        List<Integer> list1 = new ArrayList<>();
+        list1.add(1);
+        list1.add(2222);
+        list1.add(3);
+        list1.add(4);
+        /**
+         * 传递的参数是索引
+         * remove(int 类型)
+         */
+//        list1.remove(new Integer(2222));
+        /**
+         * 集合名字.set(索引,修改元素内容)
+         */
+        list1.set(2,555);
+        System.out.println(list1);
+        /**
+         * size() 查询集合大小
+         * get(int index);
+         */
+        System.out.println("集合大小"+ list1.size());
+        System.out.println("索引为0内容为:"+ list1.get(0));
+
+
+        List<Student> students = new ArrayList<>();
+//        students.add(new Student("zhangsan",10));
+//        students.add(new Student("lisi",10));
+//        students.add(new Student("wangwu",10));
+        System.out.println(students);
+
+    }
+}

+ 54 - 0
java-base-project10/day16/src/com/sf/_10_arrayList/Test1.java

@@ -0,0 +1,54 @@
+package com.sf._10_arrayList;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Test1 {
+    public static void main(String[] args) {
+        List<Student> students = new ArrayList<>();
+        students.add(new Student("张三",18,90));
+        students.add(new Student("李四",17,91));
+        students.add(new Student("王五",19,92));
+        students.add(new Student("赵六",18,93));
+        students.add(new Student("赵六",18,93));
+        students.add(new Student("赵六",18,93));
+        students.add(new Student("赵六",18,93));
+
+        List<Student> sts = unqiueList(students);
+        System.out.println(sts);
+//        System.out.println(students);
+//        for (Student student : students) {
+//            if(student.getAge()  == 18){
+//                System.out.println("姓名:"+ student.getName() +"语文成绩:"+ student.getChineseScore());
+//            }
+//        }
+    }
+
+    public static List<Student> unqiueList(List<Student> students){
+        // [{zhangsan,18,80}]
+        List<Student> unqiueList = new ArrayList<>();
+        // [{zhangsan,18,80},{zhangsan,18,80},{lisi,18,80},,{wangwu,18,80}]
+        /**
+         * 第一种思路: 创建新的集合, 把之前集合内容添加新的集合中
+         * 在添加之前判断判断name 是否已经存在
+         */
+        for (Student student : students) {
+            // 判断去除集合内容是否为0 , 如果为0 ,添加任何元素都可以成功
+            if(unqiueList.size() == 0){
+                unqiueList.add(student);
+            }else{
+                // 默认值是false
+                boolean flag = false;
+                for (Student student1 : unqiueList) {
+                    if(student1.getName().equals(student.getName())){
+                        flag = true;
+                    }
+                }
+                if(!flag){
+                    unqiueList.add(student);
+                }
+            }
+        }
+        return unqiueList;
+    }
+}