fanjialong преди 3 дни
родител
ревизия
b29fd43ebe

+ 11 - 0
java-base-project10/day15/day15.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 58 - 0
java-base-project10/day15/src/com/sf/_01_包装类/Test.java

@@ -0,0 +1,58 @@
+package com.sf._01_包装类;
+
+public class Test {
+
+    public static void main(String[] args) {
+        /**
+         * 包装类装箱和拆箱
+         * int -> Integer
+         * 拆箱
+         * Integer -> int
+         *
+         * 装箱:
+         * 第一种  Integer i = new Integer(int)
+         * 第二种  Integer i1 = Integer.valueOf(int)
+         */
+        Integer i1 = new Integer(18);
+        Integer i3 = new Integer(18);
+        Integer i2 = Integer.valueOf(10);
+        Integer i4 = Integer.valueOf(10);
+        System.out.println(i1);
+        System.out.println(i2);
+
+        System.out.println(i1 == i3);
+        System.out.println(i2 == i4);
+
+        /**
+         * 这两种装箱方式有什么区别
+         * 如果是用valueOf 方式他会用常量池有缓存效果
+         * -128 - 127
+         *
+         * 自动装箱
+         * Integer i5 = 10;
+         * 他底层使用是valueOf
+         */
+        Integer i5  = 18;
+        Integer i6  = 18;
+        System.out.println(i5 == i6);
+
+        /**
+         * 拆箱
+         * Integer  -> int
+         * integer.intValue();
+         */
+        Integer i7 = 20;
+        int i8 = i7.intValue();
+        System.out.println(i8);
+
+        /**
+         * 其他的包装类和Integer 操作是一模一样
+         * 把boolean 装箱成Boolean
+         */
+        Boolean flag = true;
+
+
+
+
+    }
+}

+ 68 - 0
java-base-project10/day15/src/com/sf/_01_包装类/Test1.java

@@ -0,0 +1,68 @@
+package com.sf._01_包装类;
+
+public class Test1 {
+    public static void main(String[] args) {
+        /**
+         * 把字符串转成int
+         * int parseInt(String str)
+         */
+        int num = Integer.parseInt("10");
+        System.out.println(num);
+        /**
+         * Number 数字 Format 格式化
+         * 数字格式异常
+         * 把一个字符串转成数字 过程中出现异常
+         * NumberFormatException
+         */
+//        int num1 = Integer.parseInt("abc10bc");
+//        System.out.println(num1);
+
+//        Scanner scanner = new Scanner(System.in);
+//        System.out.println("请录入年龄");
+//        String age = scanner.next();
+//        //把字符串age 转成int
+//        int intAGE = Integer.parseInt(age);
+//        System.out.println(intAGE);
+
+        /**
+         * 我们可能从别人那里拿到数据就是一个字符串
+         * 我希望获取到int , 就可以使用parseInt 准成int 类型
+         */
+
+
+        /**
+         * 把int 类型转成String 类型
+         * toString()
+         */
+        Integer num3 = 30;
+        String str = num3.toString();
+        System.out.println(str);
+
+        /**
+         * Integer.MAX_VALUE int 最大值
+         * Integer.MIN_VALUE int 最小值
+         *
+         * 他可以作为int 合理范围判断
+         * 比如: 通过键盘录入一个订单编号
+         * 这个订单编号必须在 int 范围之内
+         * 如果超出了范围告诉他订单不合理
+         */
+//         orderNo = "1231231123123123122445";
+//        if(age < Integer.MAX_VALUE && age > Integer.MIN_VALUE){
+//            System.out.println("订单编号合法");
+//        }else{
+//            System.out.println("订单编号不合法");
+//        }
+
+//        Scanner s  = new Scanner(System.in);
+//        System.out.println("请录入你的数字");
+//        long number = s.nextLong();
+//        if(num > Integer.MAX_VALUE){
+//            System.out.println("超出范围");
+//        }
+
+        int compare = Integer.compare(3, 3);
+        System.out.println(compare);
+
+    }
+}

+ 37 - 0
java-base-project10/day15/src/com/sf/_01_包装类/Test2.java

@@ -0,0 +1,37 @@
+package com.sf._01_包装类;
+
+public class Test2 {
+    public static void main(String[] args) {
+        String totalPriceStr = "80"; // 菜品总价
+        String isMemeberStr = "true"; // 是否会员
+        Long orderNo = 12312123123132L; // 订单编号
+        Integer fullReduce = 50; // 满减金额
+        double deliveryFee= 3.5; // 固定配送配
+        // 把菜品总价转成double
+        double totalPrice = Double.parseDouble(totalPriceStr);
+        // 把会员转成Boolean
+        boolean isMember = Boolean.parseBoolean(isMemeberStr);
+        // 判断是否是会员如果是配送变成0
+        if(isMember){
+            deliveryFee = 0;
+        }
+        // 满减金额和菜品总价比较
+        // 1 前面大   -1 前面小   0 相等
+        // int      String  -> Double.valueOf(s)
+        Double d = Double.valueOf(totalPrice);
+        int flag = Integer.compare(d.intValue(), fullReduce);
+        Integer fullReducePrice = 0;
+        if(flag >=0){
+            // 如果达到了满减门槛可以减少10元,如果没达到就是0元
+            fullReducePrice = 10;
+        }
+        // 把订单编号转成字符串类型
+        String orderNoStr = orderNo.toString();
+        // 计算最终支付金额
+        // 菜品总价 - 满减金额 + 配送狒
+        double finalPrice = totalPrice - fullReducePrice + deliveryFee;
+        System.out.println("最终支付金额:"+ finalPrice);
+        System.out.println("订单编号为:"+orderNoStr +"配送费:"+deliveryFee +
+                "满减金额:"+ fullReducePrice +"商品总价:"+ totalPrice);
+    }
+}

+ 24 - 0
java-base-project10/day15/src/com/sf/_01_包装类/Test3.java

@@ -0,0 +1,24 @@
+package com.sf._01_包装类;
+
+public class Test3 {
+    public static void main(String[] args) {
+        /**
+         * 通过键盘录入方式录入姓名年龄
+         * 如果姓名为空提醒姓名不能为空
+         * 如果年龄为空提醒年龄不能为空
+         */
+//        Scanner scanner = new Scanner(System.in);
+//        System.out.println("请录入姓名");
+//        String name = scanner.nextLine();
+//        System.out.println("请录入年龄");
+        String name = null;
+        Integer age = null;
+        if(name == null){
+            System.out.println("姓名为空");
+        }
+        // 包装类是可以做判断是否为null
+        if (age == null){
+            System.out.println("年龄不能为空");
+        }
+    }
+}

+ 41 - 0
java-base-project10/day15/src/com/sf/_02_bigdecmail/Test.java

@@ -0,0 +1,41 @@
+package com.sf._02_bigdecmail;
+
+import java.math.BigDecimal;
+import java.util.Scanner;
+
+public class Test {
+    public static void main(String[] args) {
+        // 使用double类型计算  0.01 + 0.09
+        /**
+         * 在java 中
+         * float 和double 虽然可以表示小数, 但是表示小数都不是精准小数
+         *
+         * 在大多数情况可以使用这个非精准小数
+         *
+         * 在什么情况必须要使用精准小数,设计到钱相关内容
+         * 精准小数在java 当中使用BigDecimal
+         */
+//        System.out.println(0.09 + 0.01); // ?
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请输入第一个数字");
+        String num1 = scanner.next();
+        System.out.println("请输入第二个数字");
+        String num2 = scanner.next();
+        System.out.println("请输入运算符  + - * /");
+
+        String operate = scanner.next();
+        BigDecimal bigDecimal = new BigDecimal(num1);
+        BigDecimal bigDecimal1 = new BigDecimal(num2);
+        if("+".equals(operate)){
+            System.out.println(bigDecimal.add(bigDecimal1));
+        }else if("-".equals(operate)){
+            System.out.println(bigDecimal.subtract(bigDecimal1));
+        }else if("*".equals(operate)){
+            System.out.println(bigDecimal.multiply(bigDecimal1));
+        }else if("/".equals(operate)){
+            System.out.println(bigDecimal.divide(bigDecimal1));
+        }
+
+
+    }
+}

+ 43 - 0
java-base-project10/day15/src/com/sf/_02_bigdecmail/Test1.java

@@ -0,0 +1,43 @@
+package com.sf._02_bigdecmail;
+
+import java.math.BigDecimal;
+
+public class Test1 {
+    public static void main(String[] args) {
+        //商品单价  商品数量
+        BigDecimal price = new BigDecimal(100);
+        BigDecimal count = new BigDecimal(3);
+        // 计算商品总价
+        BigDecimal totalPrice = price.multiply(count);
+        System.out.println("商品总价为:"+totalPrice);
+        // 折扣率 0.9
+        BigDecimal discount = new BigDecimal("0.8");
+        //折扣金额
+        BigDecimal discountPrice = totalPrice.multiply(discount);
+//                .setScale(2,BigDecimal.ROUND_HALF_UP);
+        System.out.println(discountPrice);
+        // 满减金额
+        /**
+         * bigdecimal 比较方式有两种方式
+         * 一种把bigdecmail 转成基本数据类型进行比较  intValue  longValue
+         *
+         * bigDecimal当中compareTo(bigDecimail) 方法进行比较
+         */
+        BigDecimal finalPrice = new BigDecimal(0);
+        if(discountPrice.intValue() >= 100 && discountPrice.intValue() < 200){
+            finalPrice = discountPrice.subtract(new BigDecimal(10));
+        }else if(discountPrice.intValue() >= 200 && discountPrice.intValue() < 500){
+            finalPrice = discountPrice.subtract(new BigDecimal(30));
+        }else if(discountPrice.intValue() >= 500){
+            finalPrice = discountPrice.subtract(new BigDecimal(100));
+        }
+        System.out.println("最终金额:"+ finalPrice);
+        // 税费计算  最终金额 * 税率
+        BigDecimal taxPrice = finalPrice.multiply(new BigDecimal(0.09)).
+                setScale(2,BigDecimal.ROUND_HALF_UP);
+        System.out.println("税费"+ taxPrice);
+
+        // 总金额 = 最终金额 + 税费
+        System.out.println("订单总金额"+finalPrice.add(taxPrice));
+    }
+}

+ 13 - 0
java-base-project10/day15/src/com/sf/_02_bigdecmail/Test2.java

@@ -0,0 +1,13 @@
+package com.sf._02_bigdecmail;
+
+import java.math.BigDecimal;
+
+public class Test2 {
+    public static void main(String[] args) {
+        System.out.println(0.01+0.09);
+
+        BigDecimal num = new BigDecimal("0.01");
+        BigDecimal num1 = new BigDecimal("0.09");
+        System.out.println(num.add(num1));
+    }
+}

+ 22 - 0
java-base-project10/day15/src/com/sf/_03_str/Test.java

@@ -0,0 +1,22 @@
+package com.sf._03_str;
+
+public class Test {
+    public static void main(String[] args) {
+//        String name = "zhangsan";
+//        name = "lisi";
+//        System.out.println(name);
+        String str = "123";
+        String str1 = "123";
+
+        String str2 = new String("123");
+        String str3 = new String("123");
+        // == 针对对象默认比较地址信息
+        System.out.println(str == str1);    // true
+        System.out.println(str2 == str3);   //false
+        System.out.println(str == str2);    //false
+        System.out.println(str1 == str3);   //false
+        // String 当中equals 比较字符串的内容是否相同
+        System.out.println(str.equals(str2)); //true
+        System.out.println(str.equals(str1)); //true
+    }
+}

+ 79 - 0
java-base-project10/day15/src/com/sf/_03_str/Test1.java

@@ -0,0 +1,79 @@
+package com.sf._03_str;
+
+public class Test1 {
+    public static void main(String[] args) {
+        /**
+         * 查看字符串的长度
+         * length();
+         *
+         * 注册时候限制最小要几个字符最多几个字符
+         */
+        String name = "zhangsan";
+        System.out.println("字符串长度为:"+ name.length());
+        /**
+         * 查询字符数组中对应字符
+         * char charAt(int index)
+         */
+        System.out.println("对应位置字符为:"+name.charAt(0));
+
+        /**
+         * 获取字符串第一次出现索引位置
+         * indexOf(String str)
+         * 作用: 利用这个方法判断字符串是否包含某一段内容
+         */
+        System.out.println("ang在name当中索引位置:"+ name.indexOf("xxxx"));
+
+        /**
+         * 判断字符串是否以什么前缀开始的
+         * boolean startWith(String prefix)
+         */
+        System.out.println("字符串是否是zhang 开始的"+name.startsWith("zhang"));
+        /**
+         * 字符串比较 如果比较内容是否相同实用equals 比较地址是否相同==
+         * String 当中重写了 Object equals 方法默认比较就是地址值是否相同
+         *
+         * equalsIgnoreCase : 忽略大小写比较
+         */
+        String str = "abcDeF";
+        String str1 = "abcdEf";
+        System.out.println(str.equals(str1));
+        System.out.println(str.equalsIgnoreCase(str1));
+
+        /**
+         * String toUpperCase() 把字符串转成大写
+         * String toLowerCase() 把字符串转成小写
+         */
+        String str3 = "abEfDx";
+        System.out.println(str3.toUpperCase());
+        System.out.println(str3.toLowerCase());
+
+        /**
+         * 字符串截取
+         * String substring(beingIndex);
+         * String substring(beginIndex,int endIndex);
+         */
+        String str4 = "fanjialong";
+        System.out.println(str4.substring(3));
+        System.out.println(str4.substring(3,6));
+
+        /**
+         * 字符串替换
+         * String replace(String oldStr,String new Str)
+         *
+         */
+        String str5 = "zhangsan";
+        System.out.println(str5.replace("zhang","li"));
+
+        /**
+         * trim() 去除前面和后面空格
+         */
+        String str6 = "      wangwu       ";
+
+        System.out.println(str6.trim());
+
+        /**
+         * contains : 判断是否包含某一段字符串
+         */
+        System.out.println(str6.contains("wang1"));
+    }
+}

+ 53 - 0
java-base-project10/day15/src/com/sf/_03_str/Test2.java

@@ -0,0 +1,53 @@
+package com.sf._03_str;
+
+import java.util.Scanner;
+
+public class Test2 {
+    /**
+     * 需求1:手机号码,屏蔽中间四位
+     * 18631112222;
+     */
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入一个手机号码");
+        String phone = scanner.next();
+
+        if(phone.length() != 11){
+            System.out.println("手机号码比如是11 位");
+            return;
+        }
+
+        // 可以做手机号码格式校验
+        boolean flag = isValidPhoneNumber(phone);
+        if(!flag){
+            System.out.println("手机号码不合法");
+            return;
+        }
+        /**
+         * 字符串当中有个叫正则表达式
+         * 正则表示他可以按照一定规则 和字符串进行比较
+         */
+        //
+        String prefix = phone.substring(0, 3);
+        String suffix = phone.substring(7);
+        System.out.println("加密手机号码:"+prefix+"****" +suffix);
+
+    }
+
+    public static boolean isValidPhoneNumber(String phone) {
+        // 判空:如果传入null或空字符串,直接返回false
+        if (phone == null || phone.trim().isEmpty()) {
+            return false;
+        }
+
+        // 手机号正则表达式:
+        // ^1 表示以1开头
+        // [3-9] 表示第二位是3、4、5、6、7、8、9中的一个
+        // \\d{9} 表示后面跟9位任意数字
+        // $ 表示结束,确保总长度是11位
+        String phoneRegex = "^1[3-9]\\d{9}$";
+
+        // 使用matches方法匹配正则,匹配成功返回true,否则false
+        return phone.trim().matches(phoneRegex);
+    }
+}

+ 20 - 0
java-base-project10/day15/src/com/sf/_03_str/Test3.java

@@ -0,0 +1,20 @@
+package com.sf._03_str;
+
+public class Test3 {
+    public static void main(String[] args) {
+        statisticsCharCount("abceacab",'a');
+    }
+
+    public static void statisticsCharCount(String name,char target){
+        // 字符串 -> 字符数组   abacad
+        //[a,b,a,c,a,d]
+        char[] chars = name.toCharArray();
+        int count = 0;
+        for (char ch : chars) {
+            if(ch == target){
+                count++;
+            }
+        }
+        System.out.println("目标字符出现次数:"+ count);
+    }
+}

+ 46 - 0
java-base-project10/day15/src/com/sf/_03_str/Test4.java

@@ -0,0 +1,46 @@
+package com.sf._03_str;
+
+public class Test4 {
+    public static void main(String[] args) {
+//        String str = "abc,bcd";
+//        /**
+//         * split(切割规则)
+//         */
+//        String[] strs = str.split(",");
+//        for (String s : strs) {
+//            System.out.println(s);
+//        }
+
+        /**
+         * 需求3: 有字符串str =    hello word     ,要求打印出来HELLO java
+         */
+//        String str = "   hello word   ";
+//        String trimStr = str.trim();
+////        String newStr = trimStr.replace("hello word", "HELLO java");
+//        String[] strs = trimStr.split(" ");
+//        String s = strs[0].toUpperCase();
+//        System.out.println(s +" "+ "java");
+
+        /**
+         * 我&爱$中*国
+         *
+         * 要求: 要我 爱 中 国 都从字符串中提取出来 使用split
+         * 最终打印出来我-爱-中-国
+         */
+        String str = "我&爱$中^国";
+        // 我    爱$中*国
+        String[] strs1 = str.split("&");
+        String wo = strs1[0];
+
+        //爱  中*国
+        String[] strs2 = strs1[1].split("\\$");
+        String ai = strs2[0];
+        //中  国
+        String[] strs3 = strs2[1].split("\\^");
+        String zhong = strs3[0];
+        String guo = strs3[1];
+        System.out.println(wo +"-"+ai+"-"+zhong +"-"+guo);
+
+
+    }
+}