先是一个银行类:
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); } }