1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| package com.demo;
import java.util.*; class Cust { private String name; private int id; private String pwd; private int balance; int selfNum; static String bankName = "建设银行"; static int allNum = 0; public Cust(){}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getPwd() { return pwd; }
public void setPwd(String pwd) { this.pwd = pwd; }
public int getBalance() { return balance; }
public void setBalance(int balance) { this.balance = balance; }
public Cust(String name, int id, String pwd, int balance) { this.name = name; this.id = id; this.pwd = pwd; this.balance = balance; allNum++; selfNum = allNum; }
public void getMoney(int price) { if (price > 0) { if (price <= balance) { this.balance -= price; System.out.println("成功取款" + price + "元\n"); } else { System.out.println("余额不足!"); } } else { System.out.println("金额非法"); } }
public void saveMoney(int price) { if (price > 0) { this.balance += price; System.out.println("成功存款" + price + "元\n"); } else { System.out.println("金额非法"); } }
public void search() { System.out.println("所属银行:" + bankName); System.out.println("用户姓名:" + name); System.out.println("用户账号:" + id); System.out.println("账户余额:" + balance); System.out.println("您是本银行" + allNum + "个顾客中的第" + selfNum + "个顾客\n"); }
public void changePWD() { Scanner scanner = new Scanner(System.in); System.out.println("请从键盘上键入新密码"); String password = scanner.nextLine(); if (password.equals(pwd)) { System.out.println("输入的密码不能与原密码相同\n"); } else { this.pwd = password; System.out.println("修改成功\n"); } }
public static double sum(Cust... c) { double sum = 0; for (int i = 0; i < allNum; i++) { sum += c[i].balance; } return sum; }
public static void sort(Cust... c) { Cust temp; for (int i = 0; i < allNum - 1; i++) { for (int j = 0; j < allNum - i - 1; j++) { if (c[j].balance < c[j + 1].balance) { temp = c[j]; c[j] = c[j + 1]; c[j + 1] = temp; } } } for (int i = 0; i < allNum; i++) { System.out.println(c[i].name + "," + c[i].id + "," + c[i].balance); } } }
class VIPCust extends Cust{ private static int overdraftLimit = 3000; public VIPCust(String name, int id, String pwd, int balance) { super(name,id,pwd,balance); } @Override public void getMoney(int price) { if(this.getBalance() +overdraftLimit>price){ super.getMoney(price); } else { System.out.println("余额不足,无法取款"); } } public void transfer(Cust recipient,int price) { if(this.getBalance()+overdraftLimit>=price){ recipient.setBalance(recipient.getBalance()+price); this.setBalance(this.getBalance()-price); System.out.println("成功转账"+price+"元\n"); }else { System.out.println("余额不足,转账失败"); } } } public class Test { public static void main(String[] args) { Cust Customer1 = new Cust("李四",100,"123456",10000); Cust Customer2 = new Cust("张三",105,"114514",9000); Cust Customer3 = new Cust("优秀素质",101,"1919810",5000); VIPCust VIP_Customer1 = new VIPCust("伍佰",1,"233333",50000); VIPCust VIP_Customer2 = new VIPCust("阿陈",2,"122222",30000); Customer1.search(); Customer2.search(); Customer3.search(); VIP_Customer1.search(); VIP_Customer2.search(); Customer1.changePWD(); Customer2.getMoney(2000); Customer3.saveMoney(3000); VIP_Customer1.transfer(VIP_Customer2,5000); VIP_Customer2.transfer(Customer1,1000); Cust.sort(Customer1,Customer2,Customer3,VIP_Customer1,VIP_Customer2); double sum = Cust.sum(Customer1,Customer2,Customer3,VIP_Customer1,VIP_Customer2); System.out.println("\n所有账户余额为"+sum); } }
|