创建一个名为“Account”的抽象类,有三个为“CheckingAccount”、“SavingsAccount”和“BusinessAccount”的子类。通过类似ATM的程序
package 银行账户管家;
public abstract class Account {
String name;//账户名
Double money;//存款
String pasword;//密码
//还款
public void repay(Double rem){}
//借出
public void loan(Double lem){}
//查询存款
public double get_money(){return money;}
}
package 银行账户管家;
import java.util.Scanner;
public class BusinessAccount extends Account {
Double credit;//利率
Double limitmoney;//贷款额度
BusinessAccount(String n,String p,Double m){
name=n;
pasword=p;
money=m;
credit=0.1;
limitmoney=money*0.5;
}
//存钱
public void repay(Double remoney){
money+=remoney;
limitmoney=money*0.5;
}
//取款
public void loan(Double lemoney){
if(lemoney<limitmoney+money){
money-=lemoney;
limitmoney=money*0.5;
}
else
System.out.println("您的账户余额不够");
}
public double get_money(){
System.out.println("请输入存款时间");
Scanner sc= new Scanner(System.in);
Integer period=sc.nextInt();
money=get_credit_money(period);
return money;
}
public double get_credit_money(Integer days){
Integer m=days/30;
Integer d=days%30;
money+=money*credit*m;
money+=money*(credit/100)*d;
limitmoney=money*0.5;
return money;
}
}
package 银行账户管家;
public class CheckingAccount extends Account {
CheckingAccount(String n,String p,Double m){
name=n;
pasword=p;
money=m;
}
public void repay(Double remoney){
money+=remoney;
}
public void loan(Double lemoney){
if(lemoney<money)
money-=lemoney;
else
System.out.println("您的余额不足");
}
public double get_money(){
return money;
}
}
package 银行账户管家;
import java.util.Scanner;
public class SavingsAccount extends Account {
Double credit;
Integer limitperm;
Integer times;
SavingsAccount(String n,String p,Double m){
name=n;
pasword=p;
money=m;
credit=0.02;
limitperm=2;
times=0;
}
//还款
public void repay(Double remoney){
times++;
if(times<=limitperm)
money+=remoney;
else
money=money+remoney-(times-limitperm)*2;
}
//取钱
public void loan(Double lemoney){
times++;
if(times<=limitperm){
if(lemoney<money)
money-=lemoney;
else
System.out.println("您的账户余额不够");
}
else
money=money-lemoney-(times-limitperm)*2;
}
//查询余额
public double get_money(){
System.out.println("请输入存款时间");
Scanner sc= new Scanner(System.in);
Integer period=sc.nextInt();
money=get_credit_with_money(period);
return money;
}
//计算利息
public double get_credit_with_money(Integer p){
Integer m=p/30;
Integer day=p%30;
money+=money*credit*m;
money+=money*(credit/90)*day;
return money;
}
}
package 银行账户管家;
import java.util.Scanner;
public class ATM {
public static void main(String args[]){
System.out.println("请输入账号密码以及存款");
Scanner sc=new Scanner(System.in);
String name=sc.next();
String pasword=sc.next();
Double money=sc.nextDouble();
System.out.println("请选择账户类型:1-CheckingAccount 2-SavingsAccount 3-BusinessAccount");
Integer choice=sc.nextInt();
switch (choice){
case 1:
CheckingAccount checkingAccount=new CheckingAccount(name,pasword,money);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
Integer ch=sc.nextInt();
while(ch!=4){
switch (ch){
case 1:
System.out.println("存钱数额为:");
Double mon=sc.nextDouble();
checkingAccount.repay(mon);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
ch=sc.nextInt();
break;
case 2:
System.out.println("取款数额为:");
Double m=sc.nextDouble();
checkingAccount.loan(m);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
ch=sc.nextInt();
break;
case 3:
Double mo=checkingAccount.get_money();
System.out.println("本账户余额为: "+mo);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
ch=sc.nextInt();
break;
}
}
break;
case 2:
SavingsAccount savingsAccount=new SavingsAccount(name,pasword,money);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
Integer cho=sc.nextInt();
while(cho!=4){
switch (cho){
case 1:
System.out.println("存钱数额为:");
Double mon=sc.nextDouble();
savingsAccount.repay(mon);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
cho=sc.nextInt();
break;
case 2:
Double m=sc.nextDouble();
savingsAccount.loan(m);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
cho=sc.nextInt();
break;
case 3:
Double mo=savingsAccount.get_money();
System.out.println("本账户余额为: "+mo);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
cho=sc.nextInt();
break;
}
}
break;
case 3:
BusinessAccount businessAccount=new BusinessAccount(name,pasword,money);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
Integer choi=sc.nextInt();
while (choi!=4){
switch (choi){
case 1:
System.out.println("存钱数额为:");
Double mone=sc.nextDouble();
businessAccount.repay(mone);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
choi=sc.nextInt();
break;
case 2:
System.out.println("存钱数额为:");
Double mon=sc.nextDouble();
businessAccount.loan(mon);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
choi=sc.nextInt();
break;
case 3:
Double mo=businessAccount.get_money();
System.out.println("本账户余额为: "+mo);
System.out.println("请选择操作:1-存钱 2-取钱 3-查询余额 4-退出");
choi=sc.nextInt();
break;
}
}
break;
}
}
}