From 64e29c2c33902faf95f1cefdb2715b009120b8c6 Mon Sep 17 00:00:00 2001 From: baihuijuan <3078948726@qq.com> Date: Tue, 24 Mar 2026 23:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90BankAccount=E5=92=8CEmployee?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/BankAccount.java | 56 +++++++++++++++++++++++++++++++ w3/Employee.java | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 w3/BankAccount.java create mode 100644 w3/Employee.java diff --git a/w3/BankAccount.java b/w3/BankAccount.java new file mode 100644 index 0000000..a0cfc88 --- /dev/null +++ b/w3/BankAccount.java @@ -0,0 +1,56 @@ +public class BankAccount { + private final String accountNumber; + private String ownerName; + private double balance; + + public BankAccount(){ + this("000000","未命名"); + } + + public BankAccount(String accountNumber, String ownerName) { + this.accountNumber = accountNumber; + this.ownerName = ownerName; + this.balance=0.0; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + public void deposit(double amount) { + if(amount>0){ + this.balance += amount; + System.out.println("存款成功,当前余额:" +this.balance); + }else{ + System.out.println("存款必须大于0"); + } + } + + public void withdraw(double amount) { + if(amount<=0){ + System.out.println("取款金额必须大于0"); + }else if(amount>this.balance){ + System.out.println("金额不足,当前余额:"+this.balance); + }else { + this.balance -= amount; + System.out.println("取款成功!当前余额:" + this.balance); + } + } + + // 查询余额的方法 + public double getBalance() { + return this.balance; + } + + // 获取账户号的方法(因为账户号不可修改,只提供getter) + public String getAccountNumber() { + return this.accountNumber; + } + + // 获取户主姓名的方法 + public String getOwnerName() { + return this.ownerName; + } +} + + diff --git a/w3/Employee.java b/w3/Employee.java new file mode 100644 index 0000000..d511349 --- /dev/null +++ b/w3/Employee.java @@ -0,0 +1,82 @@ +public class Employee { + private String id; + private String name; + private String department; + private double salary; + + // 全参构造方法 + public Employee(String id, String name, String department, double salary) { + this.id = id; + this.name = name; + this.department = department; + setSalary(salary); + } + + // getter和setter + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + if (salary >= 2000) { + this.salary = salary; + } else { + System.out.println("工资不能低于2000!"); + } + } + + // 涨薪方法 + public void raiseSalary(double percent) { + double newSalary = salary * (1 + percent / 100); + setSalary(newSalary); + } +} + +// 测试类 +class TestEmployee { + public static void main(String[] args) { + Employee emp1 = new Employee("1001", "张三", "技术部", 5000); + Employee emp2 = new Employee("1002", "李四", "销售部", 3000); + + System.out.println("=== 初始信息 ==="); + System.out.println(emp1.getName() + " - " + emp1.getDepartment() + " - 工资:" + emp1.getSalary()); + System.out.println(emp2.getName() + " - " + emp2.getDepartment() + " - 工资:" + emp2.getSalary()); + + System.out.println("\n=== 涨薪后 ==="); + emp1.raiseSalary(10); + System.out.println(emp1.getName() + "涨薪后工资:" + emp1.getSalary()); + + System.out.println("\n=== 修改部门 ==="); + emp2.setDepartment("市场部"); + System.out.println(emp2.getName() + "新部门:" + emp2.getDepartment()); + + System.out.println("\n=== 测试非法工资 ==="); + emp2.setSalary(1500); + } +} + +