先是一個銀行類: 再是銀行卡類: 信用卡繼承自銀行卡類: 借記卡類沒有實現:寫了一部分。 ...
先是一個銀行類:
package com.Testabstract; import java.util.*; public class Bank { //ArrayList<String> account = new ArrayList<String>(); public static void main(String[] args){ CreditAccount credit = new CreditAccount(); DebitAccount debit = new DebitAccount(); Account account = new Account(); System.out.println("*********************************"); System.out.println("*"+" "+"*"); System.out.println("*"+" "+"*"); System.out.println("*"+" 歡迎使用銀行管理系統 "+"*"); System.out.println("*"+" "+"*"); System.out.println("*"+" "+"*"); System.out.println("*********************************"); Scanner sc = new Scanner(System.in); while(true){ System.out.print("請輸入指令(1:開戶 2:登錄 3:退出):"); byte sel = sc.nextByte(); switch(sel){ case 1: System.out.print("請選擇您要開戶的類型(1:借記卡 2:信用卡):"); byte select = sc.nextByte(); if(select==1){ System.out.print("您選擇了借記卡,"); debit.openAccount(); debit.showAccount(); }else{ System.out.print("您選擇了信用卡,"); credit.openAccount(); credit.showAccount(); } break; case 2: //應該使用name.startWith("")方法增加一個判定,如何賬戶的第一個數為1, //則應該使用借記卡的login()方法,如果第一個數為2,則調用信用卡的 //login();方法, account.login(); System.out.println("登錄成功,你的卡片類型為借記卡!"); while(true){ System.out.print("請輸入指令(1、存款 2、取款 3、退出)"); byte select1 = sc.nextByte(); switch(select1){ case 1: debit.deposit(); break; case 2: debit.withdraw(); break; case 3: break; } if(select1==3) break; } case 3: System.out.println("謝謝使用,再見!"); System.exit(0); } sc.close(); } } }
再是銀行卡類:
import java.util.*; public class Account { private int id = 1001; private String name = null; private String password = "12345678"; private int personID = 0; private String email = null; public double blance = 0; Scanner input = new Scanner(System.in); public Account(){ //無參構造方法 } public Account(int id,String name,String password,int personID,String email,double blance){ //構造方法 this.id = id; //傳參時一定要定義參數 this.name = name; this.password = password; this.email = email; this.blance = blance; } public int getID(){ //返回值是什麼類型的,方法的類型就是什麼。 return id; } public String getName(){ return name; } public String getPassword(){ return password; } public int getPersonID(){ return personID; } public String getEmail(){ return email; } public double getBlance(){ return blance; } public double deposit(){ //存款 System.out.print("請輸入存款金額:"); double money = input.nextDouble(); System.out.println("存款成功!"); blance = blance+money; System.out.println("您現在的存款金額為:"+getBlance()); return blance; } public double withdraw(){ //取款 System.out.print("請輸入取款金額:"); double money = input.nextDouble(); if(money>blance) System.out.println("對不起,您的餘額不足!"); else{ blance = blance-money; System.out.println("取款成功!你的預存款餘額還剩:"+getBlance()); } return blance; } public void showAccount(){ System.out.println("卡號:"+getID()); System.out.println("姓名:"+getName()); System.out.println("身份證號:"+getPersonID()); System.out.println("郵箱:"+getEmail()); System.out.println("預存款金額:"+getBlance()); } public void openAccount() { id = id+1; System.out.print("請輸入用戶名:"); name = input.next(); String word1; String word2; while(true){System.out.print("請輸入密碼:"); word1 = input.next(); if(word1.length()<8) System.out.println("密碼小於8位,請重新輸入!"); else break; } while(true){ System.out.print("請再次輸入密碼:"); word2 = input.next(); if(!word2.equals(word1)) System.out.println("兩次密碼不一致!請重新輸入!"); else break; } System.out.print("請輸入身份證號:"); personID = input.nextInt(); while(true){ System.out.print("請輸入郵箱:"); email = input.next(); if(!email.endsWith(".com")) System.out.println("郵箱格式不正確,請重新輸入!"); else break; } System.out.print("請輸入預存款金額:"); blance = input.nextDouble(); System.out.println("開戶成功!"); } public void login(){ while(true){System.out.print("請輸入賬號:"); int tempID = input.nextInt(); if(tempID!=id) System.out.println("賬號錯誤,請重新輸入!"); else break; } while(true){ System.out.print("請輸入密碼:"); String tempPassword = input.next(); if(!tempPassword.equals(password)) System.out.println("密碼錯誤!請重新輸入!"); else break; } } }
信用卡繼承自銀行卡類:
public class CreditAccount extends Account //信用卡類繼承Account類 { private double ceiling = 10000; public CreditAccount(){} public CreditAccount(int id,String name,String password,int personID,String email,double blance,double ceiling){ super(id,name,password,personID,email,blance); //super(name); //super(password); //super(personId); //super(email); //super(blance); this.ceiling = ceiling; } public double withdraw(double money,double ceiling){ if(blance+ceiling<money){ System.out.println("超過透支額度,請重新輸入!"); }else{ System.out.println("取款成功!"); blance = blance-money; } return blance; } }
借記卡類沒有實現:寫了一部分。
public class DebitAccount extends Account { public DebitAccount(){} public DebitAccount(int id,String name,String password,int personID,String email,double blance){ super(id,name,password,personID,email,blance); } }