java編寫簡單的ATM存取系統_java

來源:互聯網
上載者:User

新手練手必備~

密碼賬戶為:

先建立賬戶類:

複製代碼 代碼如下:

package cn.Atm;
/**
* @author 偶my耶
*/
import java.io.*;
import com.project.project;
public class Account {
  private String number=null;
  private String name=null;
  private String password=null;
  private double money=0.0;
  public Account(String number,String name,String password,double money){
      this.number=number;
      this.name=name;
      this.password=password;
      this.money=money;
  }
  public String getNumber() {
      return number;
  }
  public void setNumber(String number) {
      this.number = number;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }
  public double getMoney() {
      return money;
  }
  public void setMoney(double money) {
      this.money = money;
  }
  protected void  sub_Balance(double mon) {
      money-=mon;   
  }
  protected void add_Balancen(double mon) {
      money+=mon;   
  }
}

然後建立操作類:

裡麵包含各種操作方法:

複製代碼 代碼如下:

package cn.Atm;
/**
 * @author 偶my耶
 */
import java.io.*;

import com.project.project;
public class ATM {
    Account act;

    public ATM(){
    act=new Account("0000", "test", "0000", 2000);
    }
    /*************歡迎介面*********************/
    protected void Welcome(){
        String str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
        System.out.print(str+"\n");
        System.out.print(
                "1.取款"+"\n"+
                "2.查詢"+"\n"+
                "3.存款"+"\n"+
                "4.退出"+"\n"
                );
    System.out.print(str+"\n");
    }
    /************登陸系統********************/
    protected void Load_Sys() throws Exception{
        String card,pwd;
        int counter=0;
        BufferedReader  br=new BufferedReader(new InputStreamReader(System.in));
        do{
            System.out.println("請輸入你的卡號");
            card=br.readLine();
            System.out.println("請輸入你的密碼");
            pwd=br.readLine();
            if (!isRight(card,pwd)) {
                System.out.println("你的卡號或密碼有誤");
                counter++;
            }
            else {
                Welcome();
                SysOpter();
            }
        }while(counter<5);
        System.exit(1);
    }
    /**********系統操作提示******************/
    protected void SysOpter() throws Exception {
        int num;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請選擇你要操作的項目(1--4)");
        num=br.read();
        switch (num) {
        case 49:
            GetBalance();
            break;
        case 50:
            Inqu_Iofo();
            break;
        case 51:
            AddBalance();
            break;
        case 52:
            Exit_Sys();
            break;
        }
    }
    /**********資訊查詢*******************/
    protected void Inqu_Iofo() throws Exception{
        // TODO Auto-generated method stub
        String str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
        System.out.print(str+"\n");
        System.out.print(
                "帳號"+act.getNumber()+"\n"+
                "姓名"+act.getName()+"\n"+
                "餘額"+act.getMoney()+"\n"+
                str+"\n"
                );
    SysOpter();
    }
   
    /**********存款******************/
    public void AddBalance() throws Exception{
        // TODO Auto-generated method stub
        String str=null,str1;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("請輸入存款數目");
            str=br.readLine();
            double qu=Double.valueOf(str).doubleValue();
            act.add_Balancen(qu);
            System.out.println("存款成功,你的賬戶餘額為"+act.getMoney());
            Welcome();
            SysOpter();
        } while (true);
    }
    /**********取款********************/
    public void GetBalance() throws Exception{
        // TODO Auto-generated method stub
        String str=null,str1;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("請輸入取款數目");
            str=br.readLine();
            double qu=Double.valueOf(str).doubleValue();
            if(qu>act.getMoney()){
                System.out.println("餘額不足請重新輸入");
            }
            else {
                act.sub_Balance(qu);
                System.out.println("取款成功你的賬戶餘額未"+act.getMoney());
                Welcome();
                SysOpter();
            }
        } while (true);
    }
    /*********退出******************/
    protected void Exit_Sys () {
        // TODO Auto-generated method stub
        System.out.println("安全退出!");
        System.exit(1);
    }
   
    /**********卡號和密碼是否正確********************/
    protected boolean isRight (String card, String pwd) {
        // TODO Auto-generated method stub
        if (act.getNumber().equals(card)&&act.getPassword().equals(pwd)) {
            return true;
        } else {
            return false;
        }
   
    }
    public static void main(String[] args) throws Exception {
        ATM atm=new ATM();
//        atm.Welcome();
        atm.Load_Sys();
        }
}

代碼僅供參考

運行結果如下

 

好了,小夥伴們可以自己做個ATM機了^_^

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.