diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_1/task.java b/java-lesson/src/main/java/cn/vscoder/experiment_1/task.java new file mode 100644 index 0000000..472b89c --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_1/task.java @@ -0,0 +1,130 @@ +package cn.vscoder.experiment_1; + +import java.util.Scanner; + +public class task { + // 循环标记 + private static boolean TAG = true; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + while (TAG) { + // 显示菜单 + menu(); + + int chose = check_choose_num(sc); + + switch (chose) { + case 404 -> System.out.println("输入错误,请重新输入阿拉伯数字"); + case 1 -> start(sc); + case 2 -> { + System.out.println("结束"); + TAG = false; + } + default -> System.out.println("选择错误,请重新选择!"); + } + } + } + + // TODO. 主函数 + public static void start(Scanner sc) { + // 存款金额 + double amount = check_amount_num(sc); + if (amount == 0) { + System.out.println("退出"); + return; + } + // 存款期限 + int time = check_time_num(sc); + if (time == 0) { + System.out.println("退出"); + return; + } + // 年利率 + int rate = check_rate_num(sc); + if (rate == 0) { + System.out.println("退出"); + return; + } + + // 计算总金额 + double total_amount = amount * Math.pow((1 + (rate * 0.01)), time); + System.out.printf("存款到期后总金额为: %.2f 元。\n", total_amount); + } + + // TODO. 查存款利率输入函数 + public static int check_rate_num(Scanner sc) { + while (true) { + System.out.println("请输入年利率(单位:%,输入0退出):"); + try { + String input = sc.next(); + int num = Integer.parseInt(input); + if (num > 100) { + System.out.println("请重新输入的利率小于100%!!!"); + continue; + } + return num; + } catch (Exception e) { + System.out.println("输入错误,请重新输入"); + } + } + } + + + // TODO. 检查存款期限输入函数 + public static int check_time_num(Scanner sc) { + while (true) { + System.out.println("请输入存款期限(单位:年,输入0退出):"); + try { + String input = sc.next(); + int num = Integer.parseInt(input); + if (num < 0) { + System.out.println("请重新输入的年份大于0!!!"); + continue; + } + return num; + } catch (Exception e) { + System.out.println("输入错误,请重新输入"); + } + } + } + + // TODO. 检查存款金额输入函数 + public static double check_amount_num(Scanner sc) { + while (true) { + System.out.println("请输入存款金额(单位:元,输入0退出):"); + try { + String input = sc.next(); + double num = Double.parseDouble(input); + + if (num < 0) { + System.out.println("请重新输入的金额大于0!!!"); + continue; + } + return num; + } catch (Exception e) { + System.out.println("输入错误,请重新输入"); + } + } + } + + // TODO. 检查菜单选择输入函数 + public static int check_choose_num(Scanner sc) { + System.out.println("请选择:"); + try { + String input = sc.next(); + return Integer.parseInt(input); + } catch (Exception e) { + return 404; + } + } + + // TODO. 菜单函数 + public static void menu() { + System.out.println("******存款利息计算器******"); + System.out.println("输入1:start"); + System.out.println("输入2:exit"); + System.out.println("************************"); + } +} \ No newline at end of file diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_2/Customer.java b/java-lesson/src/main/java/cn/vscoder/experiment_2/Customer.java new file mode 100644 index 0000000..7b8dbdb --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_2/Customer.java @@ -0,0 +1,159 @@ +package cn.vscoder.experiment_2; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * * 任务4-3:定义顾客类 + * * 目标:设计一个顾客类,能够购买食品和玩具两类商品,并输出相关信息。 + * * 步骤: + * * 1.声明顾客类Customer。 + * * 2.定义顾客属性,如姓名(String)、年龄(int)、联系方式(String)等。 + * * 3.提供构造方法初始化顾客信息。 + * * 4.实现购买商品的方法,接收商品列表和数量,计算总价值。 + * * 5.定义输出顾客信息、购买的商品信息、商品总价值的方法。 + * * 自定义反映商品折扣情况的方法,以及折扣后应付金额等信息的输出。 + */ + +public class Customer { + private String name; + + private String age; + + private char sex; + + private String phone; + + private boolean isVip = false; + + private Map products = new HashMap<>(); + + @Override + public String toString() { + return "Customer{" + + "姓名='" + name + '\'' + + ", 年龄='" + age + '\'' + + ", 性别='" + sex + '\'' + + ", 手机号='" + phone + '\'' + + ", \n=====================商品列表=====================\n" + + all_product() + + "\n总金额:" + all_product_price() + + "\n是否vip:" + (isVip ? "是\t享受优惠:整单8折" : "否") + + "\n实际支付:" + payment() + + "\n=================================================="; + } + + // 购买方法 + public double buy(Product product, int num) { + + add_product(product, num); + + // 返回该商品的总价格 + return product.getProduct_price() * num; + } + + // 将商品加入商品列表 + public void add_product(Product product, int num) { + // 判断商品是否存在列表中,不存在则创建 并 赋值,存在则累加 + if (!products.containsKey(product)) { + products.put(product, num); + } else { + int lastNum = products.get(product); + lastNum += num; + this.products.put(product, lastNum); + } + } + + // 获取所有商品列表 + public String all_product() { + // 获取到所有的商品key对象 + Set productSet = this.products.keySet(); + StringBuilder sb = new StringBuilder(); + // 遍历所有商品,输出商品信息 + for (Product product : productSet) { + int num = this.products.get(product); + sb.append("商品名:").append(product.getProduct_name()) + .append("\t单价:").append(product.getProduct_price()) + .append("\t购买数量:").append(num).append("\t金额:") + .append(String.format("%.2f", product.getProduct_price() * num)).append('\n'); + } + return sb.toString(); + } + + // 获取商品价格 + public String all_product_price() { + // 获取到所有的商品key对象 + Set productSet = this.products.keySet(); + double price = 0; // 设置价格进行累加 + // 遍历所有商品,取出单价 * 数量 并进行累加 + for (Product product : productSet) { + int num = this.products.get(product); + price += product.getProduct_price() * num; + } + return String.format("%.2f", price); + } + + // 获取实际支付金额 + public String payment() { + // 获取到总价格 + double price = Double.parseDouble(all_product_price()); + // 判断是否vip + if (isVip) { + price *= 0.8; + } + return String.format("%.2f", price); + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAge() { + return age; + } + + public void setAge(String age) { + this.age = age; + } + + public char getSex() { + return sex; + } + + public void setSex(char sex) { + this.sex = sex; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public boolean isVip() { + return isVip; + } + + public void setVip(boolean vip) { + isVip = vip; + } + + public Customer(String name, String age, char sex, String phone, boolean isVip) { + this.name = name; + this.age = age; + this.sex = sex; + this.phone = phone; + this.isVip = isVip; + } + + public Customer() { + } +} \ No newline at end of file diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_2/Food.java b/java-lesson/src/main/java/cn/vscoder/experiment_2/Food.java new file mode 100644 index 0000000..1385d72 --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_2/Food.java @@ -0,0 +1,66 @@ +package cn.vscoder.experiment_2; + + +/** + * * 任务2-2:定义食品类和玩具类 + * * 1.目标:在商品类的基础上,定义食品类和玩具类,并添加各自的特有属性和方法。 + * * 2.步骤: + * * 3.定义食品类Food继承自Product。 + * * 4.添加食品特有的属性:生产日期(Date)、保质期(int)、主要成分(String)。 + * * 5.实现设置和获取这些属性的方法,如setExpiryDate(Date date)、getMainIngredients()等。 + * * 6.重写toString()方法,输出食品的完整信息。 + */ + +public class Food extends Product{ + + private String production_date; // 生产日期 + + private int shelf_life_day; // 保质期 + + private String main_Ingredients; // 主要成分 + + + @Override + public String toString() { + return super.toString() + + ", 生产日期=" + production_date + + ", 保质期=" + shelf_life_day + + ", 主要成分='" + main_Ingredients + '\'' + + '}'; + } + + + public Food( String product_name, Double product_price, String product_producer, String product_color, String product_weight, String production_date, int shelf_life_day, String main_Ingredients) { + super( product_name, product_price, product_producer, product_color, product_weight); + this.production_date = production_date; + this.shelf_life_day = shelf_life_day; + this.main_Ingredients = main_Ingredients; + } + + public Food() { + } + + public String getProduction_date() { + return production_date; + } + + public void setProduction_date(String production_date) { + this.production_date = production_date; + } + + public int getShelf_life_day() { + return shelf_life_day; + } + + public void setShelf_life_day(int shelf_life_day) { + this.shelf_life_day = shelf_life_day; + } + + public String getMain_Ingredients() { + return main_Ingredients; + } + + public void setMain_Ingredients(String main_Ingredients) { + this.main_Ingredients = main_Ingredients; + } +} \ No newline at end of file diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_2/Product.java b/java-lesson/src/main/java/cn/vscoder/experiment_2/Product.java new file mode 100644 index 0000000..804b103 --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_2/Product.java @@ -0,0 +1,105 @@ +package cn.vscoder.experiment_2; + +/** + * * 任务2-1:定义商品类 + * * 目标:设计一个商品类,包含商品号、商品名、商品价格、生产商等基本属性,并定义自定义属性。 + * * 步骤: + * * 1.声明商品类Product。 + * * 2.定义基本属性:商品号(String)、商品名(String)、商品价格(double)、生产商(String)。 + * * 3.添加自定义属性,如颜色(String)、尺寸(String)等。 + * * 4.提供至少两个构造方法,包括全参数构造方法和默认构造方法。 + * * 5.实现修改商品价格的方法setPrice(double price)。 + * * 6.根据需要定义其他方法,如设置颜色setColor(String color)等。 + * * 7.重写toString()方法,用于输出商品的完整信息。 + */ + +public class Product { + + private static int ID = 1001; + + private int product_id; // 商品号 + + private String product_name; // 商品名 + + private Double product_price; // 商品价格 + + private String product_producer; // 生产商 + + private String product_color; // 商品颜色 + + private String product_weight; // 商品重量 + + public Product() { + } + + @Override + public String toString() { + return "Product Info{" + + "商品号='" + product_id + '\'' + + ", 商品名='" + product_name + '\'' + + ", 商品价格=" + product_price + + ", 生产商='" + product_producer + '\'' + + ", 商品颜色='" + product_color + '\'' + + ", 商品重量='" + product_weight + '\''; + } + + + public Product(String product_name, Double product_price, String product_producer, String product_color, String product_weight) { + this.product_name = product_name; + this.product_price = product_price; + this.product_producer = product_producer; + this.product_color = product_color; + this.product_weight = product_weight; + setProduct_id(); + } + + // id 自增 + private void setProduct_id() { + this.product_id = ID++; + } + + public int getProduct_id() { + return product_id; + } + + public String getProduct_name() { + return product_name; + } + + public Double getProduct_price() { + return product_price; + } + + public String getProduct_producer() { + return product_producer; + } + + public String getProduct_color() { + return product_color; + } + + public String getProduct_weight() { + return product_weight; + } + + + public void setProduct_name(String product_name) { + this.product_name = product_name; + } + + public void setProduct_price(Double product_price) { + this.product_price = product_price; + } + + public void setProduct_producer(String product_producer) { + this.product_producer = product_producer; + } + + public void setProduct_color(String product_color) { + this.product_color = product_color; + } + + public void setProduct_weight(String product_weight) { + this.product_weight = product_weight; + } +} \ No newline at end of file diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_2/Toy.java b/java-lesson/src/main/java/cn/vscoder/experiment_2/Toy.java new file mode 100644 index 0000000..9d7057b --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_2/Toy.java @@ -0,0 +1,62 @@ +package cn.vscoder.experiment_2; + +/** + * * 7.定义玩具类Toy继承自Product。 + * * 8.添加玩具特有的属性:型号(String)、材料(String)、安全级别(String)。 + * * 9.实现设置和获取这些属性的方法,如setModel(String model)、getMaterial()等。 + * * 10.重写toString()方法,输出玩具的完整信息。 + */ + +public class Toy extends Product { + + private String model; // 型号 + + private String material; // 材料 + + private String security_level; // 安全级别 + + @Override + public String toString() { + return super.toString() + + ", 型号='" + model + '\'' + + ", 材料='" + material + '\'' + + ", 安全级别='" + security_level + '\'' + + '}'; + } + + public Toy( String product_name, Double product_price, String product_producer, String product_color, + String product_weight, String model, String material, String security_level) { + super( product_name, product_price, product_producer, product_color, product_weight); + this.model = model; + this.material = material; + this.security_level = security_level; + } + + public Toy() { + super(); + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getMaterial() { + return material; + } + + public void setMaterial(String material) { + this.material = material; + } + + public String getSecurity_level() { + return security_level; + } + + public void setSecurity_level(String security_level) { + this.security_level = security_level; + } +} \ No newline at end of file diff --git a/java-lesson/src/main/java/cn/vscoder/experiment_2/shop.java b/java-lesson/src/main/java/cn/vscoder/experiment_2/shop.java new file mode 100644 index 0000000..255f90a --- /dev/null +++ b/java-lesson/src/main/java/cn/vscoder/experiment_2/shop.java @@ -0,0 +1,52 @@ +package cn.vscoder.experiment_2; + +/** + * 商品与顾客管理系统 + */ + +public class shop { + public static void main(String[] args) { + // 创建顾客 + Customer customer = + new Customer("zhangsan", + "18", '男', + "12345678", true); + // 创建商品1 + Product product1 = new Food("士力架", 3.5, + "玛氏公司", "black", "30g", + "2025-03-02", 30, "巧克力,花生"); + // 输出商品1 + System.out.println(product1); + + // 创建商品2 + Toy product2 = new Toy(); + product2.setProduct_name("托马斯火车"); + product2.setProduct_price(99.9); + product2.setProduct_producer("奥迪双钻"); + product2.setProduct_color("blue"); + product2.setProduct_weight("5kg"); + product2.setModel("T100"); + product2.setMaterial("塑料"); + product2.setSecurity_level("食品级"); + // 输出商品2 + System.out.println(product2); + + // 加购 + customer.buy(product1, 20); + customer.buy(product2, 2); + // 输出顾客1 + System.out.println(customer); + + // 创建顾客2 + Customer customer2 = + new Customer("lisi", + "20", '女', + "12333333333", false); + // 加购 + customer2.buy(product2, 3); + customer2.buy(product1, 5); + + // 输出顾客2 + System.out.println(customer2); + } +} \ No newline at end of file