From e17519f1c13419f1069cf3c467cae68b896b7079 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Thu, 29 Sep 2022 22:25:43 +0800 Subject: [PATCH] =?UTF-8?q?2022/09/29=2022:25=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E8=AF=BE=E4=BD=9C=E4=B8=9A=20?= =?UTF-8?q?=E9=93=B6=E8=A1=8C=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/modules.xml | 1 + Week5/Week5.iml | 11 ++ Week5/src/Administrator.java | 34 ++++ Week5/src/Employee.java | 74 +++++++ Week5/src/SalesPerson.java | 37 ++++ Week5/src/TestDriver.java | 19 ++ Week5/src/bank/BankAccount.java | 36 ++++ Week5/src/bank/CheckingAccount.java | 27 +++ Week5/src/bank/Customer.java | 61 ++++++ Week5/src/bank/Driver.java | 295 ++++++++++++++++++++++++++++ Week5/src/bank/SavingAccount.java | 27 +++ 11 files changed, 622 insertions(+) create mode 100644 Week5/Week5.iml create mode 100644 Week5/src/Administrator.java create mode 100644 Week5/src/Employee.java create mode 100644 Week5/src/SalesPerson.java create mode 100644 Week5/src/TestDriver.java create mode 100644 Week5/src/bank/BankAccount.java create mode 100644 Week5/src/bank/CheckingAccount.java create mode 100644 Week5/src/bank/Customer.java create mode 100644 Week5/src/bank/Driver.java create mode 100644 Week5/src/bank/SavingAccount.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 1687a9f..230551d 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -5,6 +5,7 @@ + diff --git a/Week5/Week5.iml b/Week5/Week5.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week5/Week5.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week5/src/Administrator.java b/Week5/src/Administrator.java new file mode 100644 index 0000000..cd5baac --- /dev/null +++ b/Week5/src/Administrator.java @@ -0,0 +1,34 @@ +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 8:54 + */ +public class Administrator extends Employee{ + private Double bonus; + + public Administrator(String id, String name, String department, Double baseSalary, Double bonus) { + super(id, name, department, baseSalary); + this.bonus = bonus; + } + + public Double getBonus() { + return bonus; + } + + public void setBonus(Double bonus) { + this.bonus = bonus; + } + + @Override + public void updateSalary() { + super.updateSalary(); + super.setBaseSalary(super.getBaseSalary()+this.bonus); + } + + @Override + public String toString() { + return "Administrator{" + + "bonus=" + bonus + + "," + super.toString() + + '}'; + } +} diff --git a/Week5/src/Employee.java b/Week5/src/Employee.java new file mode 100644 index 0000000..d60ee1d --- /dev/null +++ b/Week5/src/Employee.java @@ -0,0 +1,74 @@ +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 8:51 + */ +public class Employee { + private String id; + private String name; + private String department; + private Double baseSalary; + + public Employee() { + id = "00000"; + name="未知"; + department="销售"; + baseSalary=1000.0; + } + + public Employee(String id, String name, String department, Double baseSalary) { + this.id = id; + this.name = name; + this.department = department; + this.baseSalary = baseSalary; + } + + 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 getBaseSalary() { + return baseSalary; + } + + public void setBaseSalary(Double baseSalary) { + this.baseSalary = baseSalary; + } + + @Override + public String toString() { + return "Employee{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", department='" + department + '\'' + + ", baseSalary=" + baseSalary + + '}'; + } + + public void displayEmployee(){ + System.out.println(this); + } + + public void updateSalary(){ + this.baseSalary = this.baseSalary*1.0725; + } +} diff --git a/Week5/src/SalesPerson.java b/Week5/src/SalesPerson.java new file mode 100644 index 0000000..167ac94 --- /dev/null +++ b/Week5/src/SalesPerson.java @@ -0,0 +1,37 @@ +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:05 + */ +public class SalesPerson extends Employee{ + private Double yearlySales; + + public SalesPerson() { + } + + public SalesPerson(String id, String name, String department, Double baseSalary, Double yearlySales) { + super(id, name, department, baseSalary); + this.yearlySales = yearlySales; + } + + public Double getYearlySales() { + return yearlySales; + } + + public void setYearlySales(Double yearlySales) { + this.yearlySales = yearlySales; + } + + @Override + public void updateSalary() { + super.updateSalary(); + super.setBaseSalary(super.getBaseSalary()+this.yearlySales*0.1); + } + + @Override + public String toString() { + return "SalesPerson{" + + "yearlySales=" + yearlySales + + "," + super.toString() + + '}'; + } +} diff --git a/Week5/src/TestDriver.java b/Week5/src/TestDriver.java new file mode 100644 index 0000000..41a8659 --- /dev/null +++ b/Week5/src/TestDriver.java @@ -0,0 +1,19 @@ +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:08 + */ +public class TestDriver { + public static void main(String[] args) { + Administrator administrator = new Administrator("001", "Tom", "董事会", 4000.0, 2000.0); + SalesPerson salesPerson = new SalesPerson("002", "Lily", "销售", 5000.0, 20000.0); + + System.out.println(administrator); + System.out.println(salesPerson); + + administrator.updateSalary(); + System.out.println(administrator.getBaseSalary()); + + salesPerson.updateSalary(); + System.out.println(salesPerson.getBaseSalary()); + } +} diff --git a/Week5/src/bank/BankAccount.java b/Week5/src/bank/BankAccount.java new file mode 100644 index 0000000..3c6ec4b --- /dev/null +++ b/Week5/src/bank/BankAccount.java @@ -0,0 +1,36 @@ +package bank; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:37 + */ +public class BankAccount { + private String accountNum; + private Double balance; + + public BankAccount(String accountNum, Double balance) { + this.accountNum = accountNum; + this.balance = balance; + } + + public String getAccountNum() { + return accountNum; + } + + public void setAccountNum(String accountNum) { + this.accountNum = accountNum; + } + + public Double getBalance() { + return balance; + } + + public void setBalance(Double balance) { + this.balance = balance; + } + + @Override + public String toString() { + return "卡号:" + accountNum + ",余额:" + balance; + } +} diff --git a/Week5/src/bank/CheckingAccount.java b/Week5/src/bank/CheckingAccount.java new file mode 100644 index 0000000..7e31c58 --- /dev/null +++ b/Week5/src/bank/CheckingAccount.java @@ -0,0 +1,27 @@ +package bank; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:39 + */ +public class CheckingAccount extends BankAccount{ + private Double serviceCharge; + + public CheckingAccount(String accountNum, Double balance, Double serviceCharge) { + super(accountNum, balance); + this.serviceCharge = serviceCharge; + } + + public Double getServiceCharge() { + return serviceCharge; + } + + public void setServiceCharge(Double serviceCharge) { + this.serviceCharge = serviceCharge; + } + + @Override + public String toString() { + return super.toString() + ",服务费:" + this.serviceCharge; + } +} diff --git a/Week5/src/bank/Customer.java b/Week5/src/bank/Customer.java new file mode 100644 index 0000000..630db3a --- /dev/null +++ b/Week5/src/bank/Customer.java @@ -0,0 +1,61 @@ +package bank; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:36 + */ +public class Customer { + private String SSN;//身份证号 + private String name; + private CheckingAccount checkingAccount; + private SavingAccount savingAccount; + + public Customer(String SSN, String name) { + this.SSN = SSN; + this.name = name; + } + + public String getSSN() { + return SSN; + } + + public void setSSN(String SSN) { + this.SSN = SSN; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public CheckingAccount getCheckingAccount() { + return checkingAccount; + } + + public void setCheckingAccount(CheckingAccount checkingAccount) { + this.checkingAccount = checkingAccount; + } + + public SavingAccount getSavingAccount() { + return savingAccount; + } + + public void setSavingAccount(SavingAccount savingAccount) { + this.savingAccount = savingAccount; + } + + @Override + public String toString() { + String info = "身份证号:" + this.SSN + ",姓名:" + this.name + "\n"; + if (savingAccount != null) { + info += "存储卡信息:\n" + savingAccount + "\n"; + } + if (checkingAccount != null) { + info += "信用卡信息:\n" + checkingAccount + "\n"; + } + return info; + } +} diff --git a/Week5/src/bank/Driver.java b/Week5/src/bank/Driver.java new file mode 100644 index 0000000..3222963 --- /dev/null +++ b/Week5/src/bank/Driver.java @@ -0,0 +1,295 @@ +package bank; + +import java.io.IOException; +import java.util.Scanner; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 10:02 + */ +public class Driver { + private static Customer customer = null; + + public static void main(String[] args) throws IOException { + while (true) { + int opt = menu(); + switch (opt) { + case 1: + create(); + break; + case 2: + save(); + break; + case 3: + withDraw(); + break; + case 4: + consume(); + break; + case 5: + repay(); + break; + case 6: + settle(); + break; + case 7: + balance(); + break; + case 8: + return; + } + System.out.println("按任意键继续..."); + System.in.read(); + } + } + + /** + * 检查菜单选项输入合法性 + * @param low 选项下界 + * @param high 选项上界 + * @return 合法选项值 + */ + public static int checkInput(int low, int high) { + Scanner scanner = new Scanner(System.in); + if (scanner.hasNextInt()) { + int opt = scanner.nextInt(); + if (opt >= low && opt <= high) { + return opt; + } + System.out.println("输入超限,请输入" + low + "-" + high + "的数字"); + } else { + System.out.println("输入数据类型错误,请输入" + low + "-" + high + "的数字"); + scanner.next(); + } + return -1; + } + + public static int menu() { + System.out.println("欢迎使用银行系统"); + System.out.println("1.开户"); + System.out.println("2.存款"); + System.out.println("3.取款"); + System.out.println("4.消费"); + System.out.println("5.还款"); + System.out.println("6.银行结算"); + System.out.println("7.查询余额"); + System.out.println("8.退出"); + System.out.print(">>>请选择(1-8):"); + int opt = -1; + while (opt == -1) { + opt = checkInput(1,8); + } + return opt; + } + + /** + * 主菜单选项1 子菜单 + */ + public static void create() { + if (customer != null && customer.getCheckingAccount() != null && customer.getSavingAccount() != null){ + System.out.println("用户 " + customer.getName() + "(" + customer.getSSN() + ")" + " 信用卡、存储卡已全部开户!无需再次开户"); + return; + } + System.out.println("请选择开卡类型"); + System.out.println("1.信用卡"); + System.out.println("2.存储卡"); + System.out.println("3.返回"); + System.out.print(">>>请选择(1-3):"); + int opt = checkInput(1,3); + switch (opt){ + case 1://信用卡开户 + createCheckingAccount(); + break; + case 2://存储卡开户 + createSavingAccount(); + break; + case 3://返回上一级 + return; + } + } + + /** + * 主菜单选项1 子菜单1 信用卡开户 + */ + public static void createCheckingAccount() { + Scanner scanner = new Scanner(System.in); + if (customer == null){ + System.out.print("请输入SSN:"); + String SSN = scanner.nextLine(); + System.out.print("请输入姓名:"); + String name = scanner.nextLine(); + customer = new Customer(SSN, name); + } + if (customer.getCheckingAccount() == null){ + System.out.print("请输入卡号:"); + String accountNum = scanner.nextLine(); + System.out.print("请输入服务费:"); + double serviceCharge = scanner.nextDouble(); + customer.setCheckingAccount(new CheckingAccount(accountNum, 0.0, serviceCharge)); + System.out.println("成功创建信用卡账户!"); + System.out.println(customer.getCheckingAccount()); + } else { + System.out.println("信用卡已开户!无需创建"); + } + } + + /** + * 主菜单选项1 子菜单2 存储卡开户 + */ + public static void createSavingAccount() { + Scanner scanner = new Scanner(System.in); + if (customer == null){ + System.out.print("请输入SSN:"); + String SSN = scanner.nextLine(); + System.out.print("请输入姓名:"); + String name = scanner.nextLine(); + customer = new Customer(SSN, name); + } + if (customer.getSavingAccount() == null) { + System.out.print("请输入卡号:"); + String accountNum = scanner.nextLine(); + System.out.print("请输入利率:"); + double interestRate = scanner.nextDouble(); + customer.setSavingAccount(new SavingAccount(accountNum, 0.0, interestRate)); + System.out.println("成功创建存储卡账户!"); + System.out.println(customer.getSavingAccount()); + } else { + System.out.println("存储卡已开户!无需创建"); + } + } + + /** + * 主菜单选项2 存款 + */ + public static void save(){ + if (customer != null) { + SavingAccount savingAccount = customer.getSavingAccount(); + if (savingAccount != null) { + Scanner scanner = new Scanner(System.in); + System.out.print("请输入存款金额:"); + double balance = scanner.nextDouble(); + savingAccount.setBalance(savingAccount.getBalance()+balance); + System.out.println("成功存款 " + balance + " 元"); + } else { + System.out.println("存储卡未开户!"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } + + /** + * 主菜单选项3 取款 + */ + public static void withDraw(){ + if (customer != null) { + SavingAccount savingAccount = customer.getSavingAccount(); + if (savingAccount != null) { + Scanner scanner = new Scanner(System.in); + System.out.print("请输入取款金额:"); + double balance = scanner.nextDouble(); + Double accountBalance = savingAccount.getBalance(); + if (accountBalance > balance){ + savingAccount.setBalance(accountBalance - balance); + System.out.println("成功取款 " + balance + " 元"); + } else System.out.println("取款失败,余额不足"); + } else { + System.out.println("存储卡未开户!"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } + + /** + * 主菜单选项4 消费 + */ + public static void consume(){ + if (customer != null) { + CheckingAccount checkingAccount = customer.getCheckingAccount(); + if (checkingAccount != null) { + Scanner scanner = new Scanner(System.in); + System.out.print("请输入消费刷卡金额:"); + double consumeMoney = scanner.nextDouble(); + checkingAccount.setBalance(checkingAccount.getBalance() - consumeMoney); + System.out.println("成功消费 " + consumeMoney + " 元"); + } else { + System.out.println("信用卡未开户!"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } + + /** + * 主菜单选项5 还款 + */ + public static void repay(){ + if (customer != null) { + CheckingAccount checkingAccount = customer.getCheckingAccount(); + if (checkingAccount != null) { + if (checkingAccount.getBalance() >= 0){ + System.out.println("无需还款!"); + return; + } + Scanner scanner = new Scanner(System.in); + System.out.print("请输入还款金额:"); + double repayMoney = scanner.nextDouble(); + checkingAccount.setBalance(checkingAccount.getBalance() + repayMoney); + System.out.println("成功还款 " + repayMoney + " 元"); + } else { + System.out.println("信用卡未开户!"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } + + /** + * 主菜单选项6 银行结算 + */ + public static void settle(){ + if (customer != null) { + SavingAccount savingAccount = customer.getSavingAccount(); + if (savingAccount != null) { + Double oldBalance = savingAccount.getBalance(); + double interestBalance = oldBalance * savingAccount.getInterestRate(); + savingAccount.setBalance(oldBalance + interestBalance); + System.out.println("存储卡 " + savingAccount.getAccountNum() + " 结算已完成"); + System.out.println("根据利率 " + savingAccount.getInterestRate() + " 获得利息 " + interestBalance + " 元"); + } + + CheckingAccount checkingAccount = customer.getCheckingAccount(); + if (checkingAccount != null) { + checkingAccount.setBalance(checkingAccount.getBalance() - checkingAccount.getServiceCharge()); + System.out.println("信用卡 " + checkingAccount.getAccountNum() + " 结算已完成"); + System.out.println("已扣除服务费 " + checkingAccount.getServiceCharge() + " 元"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } + + /** + * 主菜单选项7 查询信用卡余额,存储卡余额 + */ + public static void balance(){ + if (customer != null) { + CheckingAccount checkingAccount = customer.getCheckingAccount(); + if (checkingAccount != null) { + System.out.println("信用卡 " + checkingAccount.getAccountNum() + " 余额:" + checkingAccount.getBalance() + " 元"); + } else { + System.out.println("信用卡 未开户"); + } + + SavingAccount savingAccount = customer.getSavingAccount(); + if (savingAccount != null) { + System.out.println("存储卡 " + savingAccount.getAccountNum() + " 余额:" + savingAccount.getBalance() + " 元"); + } else { + System.out.println("存储卡 未开户"); + } + } else { + System.out.println("用户未开户!请先执行功能1"); + } + } +} diff --git a/Week5/src/bank/SavingAccount.java b/Week5/src/bank/SavingAccount.java new file mode 100644 index 0000000..89e2a33 --- /dev/null +++ b/Week5/src/bank/SavingAccount.java @@ -0,0 +1,27 @@ +package bank; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/28 - 9:44 + */ +public class SavingAccount extends BankAccount{ + private Double interestRate; + + public SavingAccount(String accountNum, Double balance, Double interestRate) { + super(accountNum, balance); + this.interestRate = interestRate; + } + + public Double getInterestRate() { + return interestRate; + } + + public void setInterestRate(Double interestRate) { + this.interestRate = interestRate; + } + + @Override + public String toString() { + return super.toString() + ",利率:" + this.interestRate; + } +}