一、實驗代碼如下:
1 package 實驗6; 2 3 import java.util.Scanner; 4 5 6 public class Account { 7 8 public int id; 9 public String name; 10 public long number; 11 public long time; 12 public int money; 13 14 //方法Account(),創建賬戶的賬號、姓名和余額等 15 public Account(int id, String name, long number,long time, int money) { 16 17 this.id = id; 18 this.name = name; 19 this.number = number; 20 this.time = time; 21 this.money = money; 22 } 23 24 25 26 //方法Display(),顯示賬戶的賬號、姓名和余額信息 27 public void Display(){ 28 System.out.println("賬戶:" + id); 29 System.out.println("姓名:" + name); 30 System.out.println("身份證號:" + number); 31 System.out.println("開戶時間" + time); 32 System.out.println("余額:" + money); 33 } 34 35 /*取款方法 takeMoney(),先讓用戶驗證去除金額是否小于余額, 36 取款成功后余額減除相應的金額*/ 37 public void takeMoney(){ 38 39 Scanner sc = new Scanner(System.in); 40 System.out.println("請輸入需要取款的金額:"); 41 int withdrawals = sc.nextInt(); 42 if(withdrawals <= money) { 43 money= money-withdrawals; 44 System.out.println("賬戶余額:" + money); 45 } 46 else { 47 System.out.println("當前余額不足!"); 48 } 49 } 50 51 52 /*存款方法 saveMoney(int moneys), 53 存款是直接傳入存款金額,賬戶余額增加相應的金額*/ 54 public void saveMoney(int inmoney){ 55 money = money + inmoney; 56 System.out.println("此次存款為:" + inmoney); 57 System.out.println("賬戶余額:" + money); 58 } 59 /*銷戶方法 finalize(), 60 利用析構函數,釋放內存空間*/ 61 protected void finalize() 62 { 63 64 System.out.println("Destructor called!"); 65 } 66 67 public static void main(String[] args) { 68 Account acc = new Account(10000,"張燦",123456,20190413,100000); 69 /* 70 acc.id = 10000; 71 acc.name = "小明"; 72 acc.number = 123456; 73 acc.time=20190413; 74 acc.money = 100000; 75 */ 76 Scanner sc = new Scanner(System.in); 77 while(true) { 78 System.out.println("---歡迎進入銀行賬戶操作系統---"); 79 System.out.println("---------1銀行賬戶信息--------"); 80 System.out.println("---------2取款操作------------"); 81 System.out.println("---------3存款操作------------"); 82 System.out.println("---------4銷戶操作------------"); 83 System.out.println("---------5退出系統------------"); 84 System.out.println("------------------------------"); 85 int choice = sc.nextInt(); 86 switch(choice) { 87 case 1: 88 System.out.println("---銀行賬戶信息---"); 89 acc.Display(); 90 break; 91 case 2: 92 System.out.println("---取款操作---"); 93 acc.takeMoney(); 94 break; 95 case 3: 96 System.out.println("---存款操作---"); 97 acc.saveMoney(1000); 98 break; 99 case 4: 100 System.out.println("---銷戶操作---"); 101 acc.finalize(); 102 break; 103 case 5: 104 System.exit(0); 105 break; 106 default: 107 System.out.println("您的選擇有誤!"); 108 break; 109 } 110 } 111 } 112 }
二、實驗結果:
---歡迎進入銀行賬戶操作系統---
---------1銀行賬戶信息--------
---------2取款操作------------
---------3存款操作------------
---------4銷戶操作------------
---------5退出系統------------
------------------------------
1
---銀行賬戶信息---
賬戶:10000
姓名:張燦
身份證號:123456
開戶時間20190413
余額:100000
---歡迎進入銀行賬戶操作系統---
---------1銀行賬戶信息--------
---------2取款操作------------
---------3存款操作------------
---------4銷戶操作------------
---------5退出系統------------
------------------------------
2
---取款操作---
請輸入需要取款的金額:
5000
賬戶余額:95000
---歡迎進入銀行賬戶操作系統---
---------1銀行賬戶信息--------
---------2取款操作------------
---------3存款操作------------
---------4銷戶操作------------
---------5退出系統------------
------------------------------
3
---存款操作---
請輸入需要存款的金額:
10000
賬戶余額:105000
---歡迎進入銀行賬戶操作系統---
---------1銀行賬戶信息--------
---------2取款操作------------
---------3存款操作------------
---------4銷戶操作------------
---------5退出系統------------
------------------------------
4
---銷戶操作---
Destructor called!
---歡迎進入銀行賬戶操作系統---
---------1銀行賬戶信息--------
---------2取款操作------------
---------3存款操作------------
---------4銷戶操作------------
---------5退出系統------------
------------------------------
5
三、實驗心得:
1.Java中類的封裝是面向對象的核心特性,是信息隱蔽思想的具體實現技術,感覺和C++中類的封裝有很多相似的地方。
2.此程序還應考慮到存的錢大于零,賬戶里的錢大于取的錢。?