diff --git a/W3/BankAccount.java b/W3/BankAccount.java new file mode 100644 index 0000000..984d5de --- /dev/null +++ b/W3/BankAccount.java @@ -0,0 +1,64 @@ +package w3; + +public class BankAccount { + // 1. 私有属性(封装) + private final String accountNumber; // 账户号(final 修饰,创建后不可修改) + private String ownerName; // 户主姓名(可修改) + private double balance; // 余额(只能通过存取款修改) + + // 2. 构造方法(创建账户必须提供账户号和户主姓名,初始余额为0) + public BankAccount(String accountNumber, String ownerName) { + this.accountNumber = accountNumber; + this.ownerName = ownerName; + this.balance = 0.0; // 初始余额为0 + } + + // 3. Getter 方法(对外提供查询) + public String getAccountNumber() { + return accountNumber; + } + + public String getOwnerName() { + return ownerName; + } + + public double getBalance() { + return balance; + } + + // 4. Setter 方法(仅允许修改户主姓名) + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + // 5. 存款操作(金额必须>0) + public void deposit(double amount) { + if (amount > 0) { + this.balance += amount; + System.out.println("存款成功!当前余额:" + this.balance); + } else { + System.out.println("存款失败:存款金额必须大于0!"); + } + } + + // 6. 取款操作(金额必须>0且不超过当前余额) + public void withdraw(double amount) { + if (amount > 0 && amount <= this.balance) { + this.balance -= amount; + System.out.println("取款成功!当前余额:" + this.balance); + } else if (amount <= 0) { + System.out.println("取款失败:取款金额必须大于0!"); + } else { + System.out.println("取款失败:余额不足!"); + } + } + + // 7. 打印账户信息(方便测试) + public void printAccountInfo() { + System.out.println("===== 账户信息 ====="); + System.out.println("账户号:" + accountNumber); + System.out.println("户主姓名:" + ownerName); + System.out.println("当前余额:" + balance); + System.out.println("===================="); + } +} \ No newline at end of file diff --git a/W3/BankAccountTest.java b/W3/BankAccountTest.java new file mode 100644 index 0000000..754dad0 --- /dev/null +++ b/W3/BankAccountTest.java @@ -0,0 +1,30 @@ +package w3; + +public class BankAccountTest { + public static void main(String[] args) { + // 1. 创建一个银行账户 + BankAccount account = new BankAccount("622202123456789", "客户A"); + + // 2. 打印初始信息 + account.printAccountInfo(); + + // 3. 测试存款 + account.deposit(1000); + account.printAccountInfo(); + + // 4. 测试取款 + account.withdraw(300); + account.printAccountInfo(); + + // 5. 测试修改户主姓名 + account.setOwnerName("客户A-新版"); + System.out.println("修改后户主姓名:" + account.getOwnerName()); + + // 6. 测试异常情况(取款金额>余额) + account.withdraw(800); + // 测试异常情况(存款金额<=0) + account.deposit(-500); + // 测试异常情况(取款金额<=0) + account.withdraw(-100); + } +} \ No newline at end of file diff --git a/W3/BankAccount运行截图.png b/W3/BankAccount运行截图.png new file mode 100644 index 0000000..4c8daa6 Binary files /dev/null and b/W3/BankAccount运行截图.png differ diff --git a/W3/Employee.java b/W3/Employee.java new file mode 100644 index 0000000..a8bb844 --- /dev/null +++ b/W3/Employee.java @@ -0,0 +1,85 @@ +package w3; + +public class Employee { + private static String companyName; + private String id; + private String name; + private String department; + private double salary; + private static final double MIN_SALARY = 2000; + + static { + // 这里改成通用公司名,无任何个人信息 + companyName = "科技有限公司"; + } + + public Employee(String id, String name, String department, double salary) { + this.id = id; + this.name = name; + this.department = department; + setSalary(salary); + } + + public static String getCompanyName() { + return companyName; + } + + public static void setCompanyName(String companyName) { + Employee.companyName = companyName; + } + + 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 >= MIN_SALARY) { + this.salary = salary; + } else { + System.out.println("工资不能低于最低工资标准 " + MIN_SALARY); + this.salary = MIN_SALARY; + } + } + + public void raiseSalary(double percent) { + double newSalary = this.salary * (1 + percent / 100); + if (newSalary >= MIN_SALARY) { + this.salary = newSalary; + } else { + this.salary = MIN_SALARY; + } + } + + public void printInfo() { + System.out.println("公司:" + companyName); + System.out.println("工号:" + id); + System.out.println("姓名:" + name); + System.out.println("部门:" + department); + System.out.println("工资:" + String.format("%.2f", salary)); + System.out.println("------------------------"); + } +} \ No newline at end of file diff --git a/W3/EmployeeTest.java b/W3/EmployeeTest.java new file mode 100644 index 0000000..fb91f43 --- /dev/null +++ b/W3/EmployeeTest.java @@ -0,0 +1,26 @@ +package w3; + +public class EmployeeTest { + public static void main(String[] args) { + // 创建两个员工对象(通用信息) + Employee emp1 = new Employee("1001", "员工A", "开发部", 5000); + Employee emp2 = new Employee("1002", "员工B", "测试部", 2800); + + // 打印初始信息 + System.out.println("===== 第一个员工信息 ====="); + emp1.printInfo(); + + System.out.println("===== 第二个员工信息 ====="); + emp2.printInfo(); + + // 涨薪操作 + emp1.raiseSalary(10); + System.out.println("===== 涨薪10%后员工1信息 ====="); + emp1.printInfo(); + + // 修改部门 + emp2.setDepartment("产品部"); + System.out.println("===== 修改部门后员工2信息 ====="); + emp2.printInfo(); + } +} \ No newline at end of file diff --git a/W3/Employee运行截图.png b/W3/Employee运行截图.png new file mode 100644 index 0000000..2c0f854 Binary files /dev/null and b/W3/Employee运行截图.png differ