Java realizes simple banking business

Title: Realize the function of depositing and withdrawing money to check the balance

Yesterday the .net teacher assigned to use c# to write a homework for depositing and withdrawing money in a bank, but I have never heard of the online class (to be honest, I feel that there are not many classes in the class), so I can only I wrote one with the java I learned before.

 /**
 * 2020-03-21 23:07:02
 * 接口:
 */
package 抽象类与接口;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/*接口是抽象类的一种,只包含常量和方法的定义,而没有变量和具体
 * 方法的定义,而没有变量和具体方法的实现
 */
interface Dataoption//定义接口
{
    
    
    public void dataselect();//查询数据
    public void dataadd();//添加数据
    public void datadel();//删除数据
}
class Datamanagement implements  Dataoption//实现接口的类
{
    
    
    //private
    double totle=100.00;
    @Override
    public void dataselect() {
    
    
        // TODO Auto-generated method stub
        System.out.println("请输入您要存入金额:");
        Scanner z = new Scanner(System.in);
        //  double q=1000.00;
        totle=(z.nextDouble()+totle);
        System.out.println("您当前余额是:"+ totle);
    }
    @Override
    public void dataadd() {
    
    
        // TODO Auto-generated method stub
        System.out.println("请输入您要取出金额:");
        Scanner z = new Scanner(System.in);
        totle=totle-z.nextDouble();
        if(totle>=0) {
    
    
            System.out.println("您当前余额是:" + totle);
        }else
        {
    
    
            System.out.println("您的余额不足");
        }
    }
    @Override
    public void datadel() {
    
    
        // TODO Auto-generated method stub
        System.out.println(totle);
    }
}
public class  Interface {
    
    
    public static void main(String[] args) {
    
    
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
         Dataoption data = (Dataoption) new  Datamanagement();
        // TODO Auto-generated method stub
        System.out.println("欢迎您,请输入您的卡号。");
        String str;
        Scanner in = new Scanner(System.in);
        str = in.next();
        System.out.println("欢迎您,请输入您的密码。");
        String s;
        Scanner sca = new Scanner(System.in);
        s = sca.next();
        if (str.equals("1234567890") &&s.equals("123456")) {
    
    
            System.out.println("操作成功");
            while(true) {
    
    
                System.out.println("请选择一下操作:1:存钱,2:取钱,3:查询余额,4:退出");
                Scanner s3 = new Scanner(System.in);
                int s4 = s3.nextInt();
                switch (s4) {
    
    
                    case 1:
                        data.dataselect();
                        break;
                    case 2:
                        data.dataadd();
                        break;
                    case 3:
                        data.datadel();
                        break;
                    case 4:
                        break;
                    default:
                        System.out.println("请选择正确的操作谢谢");
                }
                if(s4==4)
                    break;
            }
            System.out.println("再见!");
        } else {
    
    
            System.out.println("操作失败");
        }
    }
}


My level is limited, don't spray.
I defined an interface, defined three abstract methods, and then defined an implementation (inheritance) class to rewrite the abstract methods in the interface (note here, all abstract methods in the interface must be implemented!) , First define a global variable whose access scope is private, that is, the balance of your account before the operation.
1:
Deposit money is the Scanner class instantiation object for keyboard input of the amount of money you want to deposit, and the amount of money is added to the balance before the operation, which is the current balance.
2: Withdraw
money Just like depositing money, enter the amount you want to withdraw and subtract it from the balance.
3: Balance
Direct query totle will do.
I assume that the card number 1234567890 and the password 123456 are
entered correctly and you can select the relevant operations for deposit and withdrawal.
Insert picture description here
Then you can exit the program, thank you for watching!

Guess you like

Origin blog.csdn.net/qq_42678668/article/details/105019937