Java编程练习之类的继承

1.创建银行卡类,并分别设计两个储蓄卡和信用卡子类。

 import javax.swing.plaf.BorderUIResource;
 import java.util.Scanner;

class Card {
      int Id;   //银行卡;
      int password;  //密码;
      double balance=2000; //账户存款金额;
      String AccountType;

      public boolean Login(String AccountType){
          boolean result=false;
         // String AccountType1="储蓄卡";
         // String AccountType2="信用卡";
          Scanner sc=new Scanner(System.in);

          for(int i=0;i<4;i++) {
              if (AccountType.equals("储蓄卡")) {
                  System.out.println("请您输入储蓄卡密码:");
                  password=sc.nextInt();
                  if(result=(password==123456)){
                      System.out.println("储蓄卡密码输入正确,正在加载中,请您耐心等待……");
                      return result;
                  }else if(i<2&&password!=123456){
                      System.out.println("储蓄卡密码输入错误");
                  }else{
                      System.out.println("储蓄卡密码连续3次输入错误,您的账号已被锁定,请您24小时后重试");
                  }


              } else if (AccountType.equals("信用卡")) {
                  System.out.println("请您输入信用卡密码:");
                  password=sc.nextInt();
                  if(result=(password==234567)){

                      System.out.println("信用卡密码输入正确,正在加载中,请您耐心等待……");
                      return result;
                  }else if(i<2&&password!=234567){
                      System.out.println("信用卡密码输入错误");
                  }else{
                      System.out.println("信用卡密码连续3次输入错误,您的账号已被锁定,请您24小时后重试");
                  }
              } else {
                  System.out.println("本系统暂不支持此类银行卡的登陆!");
              }

          }
          return false;

     }
      //存款方法
      public  void Savemoney(){
          double money;
          Scanner m=new Scanner(System.in);
          System.out.println("请输入存款金额:");
          money=m.nextDouble();
          System.out.println("请确认存款金额:"+money+"元。");

          balance=balance+money;
          //System.out.println("恭喜您存款成功,当前账户的存款金额为:"+balance+"元。");
          System.out.println("恭喜您存款成功,当前账户的存款金额为:"+balance+"元。");
      }

}
class Card2 extends Card{
    double money;
    double maxOverdraf=20000;//最大透支额度;
    void Withdraw(){
        double money;
        Scanner m1=new Scanner(System.in);
        System.out.println("请输入取款金额:");
        money=m1.nextDouble();
        if(money<=balance+maxOverdraf){
            System.out.println("请您确认取款金额:" + money + "元。");
            double account=balance-money;

             System.out.println("account:"+account);
            if(account<=0){
                System.out.println("当前账户剩余金额为:" + 0+ "元。");
                maxOverdraf=maxOverdraf+account;
//                System.out.println("money:"+money);
//                System.out.println("balance:"+balance);
//                System.out.println("maxOverdraf:"+maxOverdraf);
                System.out.println("当前账户剩余透支金额为:" + maxOverdraf + "元。");
            }else{
//                System.out.println("money:"+money);
//                System.out.println("balance:"+balance);
//                System.out.println("maxOverdraf:"+maxOverdraf);
                System.out.println("当前账户剩余金额为:" + account + "元。");
                System.out.println("当前账户剩余透支金额为:" + maxOverdraf + "元。");
            }
        }else{
            double account=money-balance-maxOverdraf;
            System.out.println("您的账户已透支,透支金额为:" + account+ "元,您取款失败。");
        }
    }

}
 class Card1 extends Card {  //储蓄卡
    double money;

    private void Withdraw() {
        double r;
        double money;
        Scanner m=new Scanner(System.in);
        System.out.println("请输入取款金额:");
        money=m.nextDouble();
        if (money <= balance) {
            System.out.println("请您确认取款金额:" + money + "元。");
            balance = balance - money;
            System.out.println("恭喜您取款成功,账户剩余金额为" + balance + "元。");
        } else {
            System.out.println("请确认取款金额:"+money+"元。");
            r= money-balance;
            System.out.println("您的账户余额不足,还差"+r+"元。");
        }
    }
public static void main(String[] args){
         String yewu;
         String AccountType;  //银行卡账户类型;

         Card1 c1=new Card1();
         Card2 c2=new Card2();
         Scanner sc1=new Scanner(System.in);
         System.out.println("请您选择银行卡类型:");
         AccountType=sc1.nextLine();
         if(AccountType.equals("储蓄卡")){
         boolean cL=c1.Login(AccountType);
         if(cL) {
             System.out.println("请您选择业务类型:");
             yewu = sc1.nextLine();
             if(yewu.equals("取款")){
                // int money=sc1.nextInt();
                 c1.Withdraw();
             }else if(yewu.equals("存款")){
                 //int money=sc1.nextInt();
                 c1.Savemoney();
             }else{
                 System.out.println("本系统暂不支持此业务!");
             }
         }else{
             System.out.println("储蓄卡登陆失败。");
         }

         }else if(AccountType.equals("信用卡")){
             boolean cL1= c2.Login(AccountType);
             if(cL1) {
                 System.out.println("请您选择业务类型:");
                 yewu = sc1.nextLine();
                 if(yewu.equals("取款")){
                     // int money=sc1.nextInt();
                     c2.Withdraw();
                 }else if(yewu.equals("存款")){
                     //int money=sc1.nextInt();
                     c2.Savemoney();
                 }else{
                     System.out.println("本系统暂不支持此业务!");
                 }
             }else{
                 System.out.println("储蓄卡登陆失败。");
             }

         }else{
             System.out.println("本系统暂不支持此银行卡类型!");
        }
    }
         }

储蓄卡子类效果:


信用卡子类效果:

Java编程练习之类的继承_第1张图片

Java编程练习之类的继承_第2张图片

你可能感兴趣的:(Java,java,开发语言)