Browse Source

完成BankAccount和Employee作业

main
baihuijuan 3 weeks ago
parent
commit
64e29c2c33
  1. 56
      w3/BankAccount.java
  2. 82
      w3/Employee.java

56
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;
}
}

82
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);
}
}
Loading…
Cancel
Save