1. 程式人生 > 實用技巧 >模擬ATM程式碼(有一點小問題)

模擬ATM程式碼(有一點小問題)

一、測試要求:

1、按照測試內容要求完成程式的設計與程式設計;

2、建立學號姓名資料夾,如:“信1705-1班20173425陳歡”,將源程式檔案、accountinformation.txt、accountlist.txt儲存在資料夾中,壓縮成rar檔案提交。

3、以班級為單位上交最終考試結果。

二、 資料結構測試要求:(8分)

ATM機的賬戶記錄Account有賬戶的唯一性標識(8個數字的組合),使用者的姓名,操作日期(Date),操作型別,賬戶密碼(六位的數字,可以用0開頭),當前的餘額(可以為0)。

1、定義Account類,其中包括七個私有變數(accountID,accountname,operatedate,operatetype,accountpassword, accountbalance,)。(3分)

各成員的含義如下:

變數accountID為字串型別String,用於儲存學生的使用者賬號(由八位數字組成)。

變數accountname為字串型別String,用於儲存賬戶的名稱。

變數operatedate為字串型別String,用於儲存操作的時間,由十位字元組成,顯示格式為“2018-09-20”。

變數operatetype為int型別,用於儲存操作賬戶的型別,具體描述為“1”表示存款,“2”表示取款,“3”表示轉賬匯款,“4”表示修改賬戶密碼,“5”表示查詢餘額。

變數accountpassword為字串型別String,用於使用者密碼,由六位數字組成。

變數accountbalance

為整數型別int,用於儲存賬戶餘額,預設為0。

變數amount為整數型別int,表示操作流水金額。

2、對每個變數定義get()(讀取變數資訊)和set()(設定變數資訊)的方法。(3分)

3、定義accountinformation.txt作為賬戶基本資訊庫,基本資訊包括accountID,accountname,accountpassword, accountbalance,要求事先實現至少儲存五個賬戶的資訊,定義accountlist.txt作為賬戶操作流水資訊資料庫,操作流水資訊包括(accountID,accountname,operatedate,operatetype, amount)。

(要求將學號作為帳號,將考生資訊作為第一條記錄)。(2分)

三、功能要求:(12分)

該程式模擬ATM的功能設計,當用戶插卡後顯示,輸入密碼介面,使用者輸入正確密碼(使用者輸入錯誤密碼,則提示該卡已被鎖定,無法操作),則彈出選擇介面:存款、取款、轉賬匯款、修改密碼、查詢餘額。

Account.java

package jiang;

public class Account {
    private String accountID;
    private String accountname;
    private String operatedate;
    private int operatetype;
    private String accountpassword;
    private int accountbalance;
    private int amount;
    public String getAccountID() {
        return accountID;
    }
    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }
    public String getAccountname() {
        return accountname;
    }
    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }
    public String getOperatedate() {
        return operatedate;
    }
    public void setOperatedate(String operatedate) {
        this.operatedate = operatedate;
    }
    public int getOperatetype() {
        return operatetype;
    }
    public void setOperatetype(int operatetype) {
        this.operatetype = operatetype;
    }
    public String getAccountpassword() {
        return accountpassword;
    }
    public void setAccountpassword(String accountpassword) {
        this.accountpassword = accountpassword;
    }
    public int getAccountbalance() {
        return accountbalance;
    }
    public void setAccountbalance(int accountbalance) {
        this.accountbalance = accountbalance;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public Account(){
        
    }
    
}
View Code

AccountManager.java

package jiang;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
import java.util.Scanner;
 
public class AccountManager {

    //全域性定義
    static Account a=new Account();
    static Scanner s=new Scanner(System.in);
    //選擇
    public static void main(String[]args) {
        try {
            fileout();
            waterout();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            if(loginAccount()&&loginPwd()) {
            Menu();
            String option;
            do {
            option=s.next();
            switch(option) {
            case"1":    handmoney();break;
            case"2":    getmoney();break;
            case"3":    moneygogogo();break;
            case"4":    update();break;
            case"5":    moneyhere();break;
            case "q":              ;break;
            default:    System.out.println("錯誤,請重新輸入!\n");
            }}while(!"q".equals(option));
            System.out.println("成功退出系統!\n");}
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //讀取賬戶基本資訊庫
    public static void fileout() throws FileNotFoundException {
        try {
        FileReader fr=new FileReader("accountinformation.txt");
        Scanner sc=new Scanner(fr);
        while(sc.hasNext()) {
        a.setAccountID(sc.next());
        a.setAccountname(sc.next());
        a.setAccountpassword(sc.next());
        a.setAccountbalance(sc.nextInt());
        }
        sc.close();
        fr.close();
        } catch (IOException e) {
            System.out.println("檔案匯入失敗");
        }
    }
    //讀取賬戶流水資訊庫
    public static void waterout() throws FileNotFoundException {
        try {
        FileReader fr=new FileReader("accountlist.txt");
        Scanner sc=new Scanner(fr);
        while(sc.hasNext()) {
        a.setAccountID(sc.next());
        a.setAccountname(sc.next());
        a.setOperatedate(sc.next());
        a.setOperatetype(sc.nextInt());
        a.setAmount(sc.nextInt());
        }
        sc.close();
        fr.close();
        } catch (IOException e) {
            System.out.println("檔案匯出失敗");
        }
    }
    //匯入賬戶基本資訊庫
    public static void filein() throws IOException {
        int i;
        FileReader fr=new FileReader("accountinformation.txt");
        FileWriter fw=new FileWriter("accountinformation.txt");
        i=fr.read();
        while(i!=-1) {
            fw.write(i);
            fr.read();
        }
        fr.close();
        fw.close();
    }
    //匯入賬戶基本資訊庫
    public static void waterin() throws IOException {
        int i;
        FileReader fr=new FileReader("accountlist.txt");
        FileWriter fw=new FileWriter("accountlist.txt");
        i=fr.read();
        while(i!=-1) {
            fw.write(i);
            fr.read();
        }
        fr.close();
        fw.close();
    }
    //列印選單
    public static void Menu(){
    StringBuffer s=new StringBuffer("\n");
    s.append("\t*************************************************\n");
    s.append("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
    s.append("\t*************************************************\n");
    s.append("\t\t\t1.存款;\n");
    s.append("\t\t\t2.取款;\n");
    s.append("\t\t\t3.轉賬匯款;\n");
    s.append("\t\t\t4.修改密碼;\n");
    s.append("\t\t\t5.查詢餘額;\n");
    s.append("\t*************************************************\n");
    s.append("\t(輸入q退出系統)\n");
    s.append("請操作\n");
    System.out.println(s);
    }
    //列印登陸賬號介面
    public static boolean loginAccount() throws IOException  {
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t請輸入您的賬號:\n");
            String checkAccount = s.next();
            if (a.getAccountID().equals (checkAccount)){
                System.out.println("輸入賬號成功,系統跳轉到輸入密碼介面\n");
                return true ;
          }else {
                   System.out.println("該賬號不存在\n");
                   return false ;
                }
    }
    //列印登陸密碼介面
    public static boolean loginPwd() throws IOException {
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        for (int i = 3 ;i>0;i--){
            System.out.println("\t\t請輸入您的密碼:\n");
            String checkPwd = s.next();
            if (a.getAccountpassword().equals (checkPwd)){
                System.out.println("輸入密碼成功,系統跳轉到主介面!\n");
                return true ;
          }else {
                if ( i ==1 ){
                    System.out.println("該賬號三次錄入密碼錯誤,該卡已被系統沒收,請與工行及時聯絡處理\n");
                   return false ;
                }
                System.out.println("密碼錄入錯誤!今日剩餘次數:"+ (i-1));
           }
        }
        return false;
   
    }
    //存款
    private static void handmoney() {
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t請輸入存款金額:\n");
        int number=s.nextInt();
        a.setAccountbalance(a.getAccountbalance() + number);
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t當前賬戶存款操作成功\n");
        System.out.println("\t\t當前賬戶餘額為"+a.getAccountbalance()+"元\n");
        System.out.println("\t*************************************************\n");
        Menu();
        System.out.println("\t\t請繼續操作\n");
    }
    //取款
    public static void getmoney() {
        a.setAccountbalance(a.getAccountbalance());
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t當前賬戶每日可以支取2萬元\n");
        System.out.println("\t\t1.100元\n");
        System.out.println("\t\t2.500元\n");
        System.out.println("\t\t3.1000元\n");
        System.out.println("\t\t4.1500元\n");
        System.out.println("\t\t5.2000元\n");
        System.out.println("\t\t6.5000元\n");
        System.out.println("\t\t7.其他金額\n");
        System.out.println("\t\t8.退卡\n");
        System.out.println("\t\t9.返回\n");
        System.out.println("\t*************************************************\n");
        int num1=100,num2=500,num3=1000,num4=1500,num5=2000,num6=5000;
        int str=s.nextInt();
        if (a.getAccountbalance() <str ){
          System.out.println("賬戶餘額不足!!\n");
          str=0;
        }else if(str==1){
            a.setAccountbalance(a.getAccountbalance() - num1);
        }else if(str==2){
            a.setAccountbalance(a.getAccountbalance() - num2);
        }else if(str==3){
            a.setAccountbalance(a.getAccountbalance() - num3);
        }else if(str==4){
            a.setAccountbalance(a.getAccountbalance() - num4);
        }else if(str==5){
            a.setAccountbalance(a.getAccountbalance() - num5);
        }else if(str==6){
            a.setAccountbalance(a.getAccountbalance() - num6);
        }else if(str==7){
             
            str=s.nextInt();
            a.setAccountbalance(a.getAccountbalance() - str);
        }else if(str==8){
            System.out.println("退卡成功\n");
        }else if(str==9){
            System.out.println("返回成功\n");
        }
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t當前賬戶存款操作成功\n");
        System.out.println("\t\t當前賬戶餘額為"+a.getAccountbalance()+"元\n");
        System.out.println("\t*************************************************\n");
         Menu();
        }
 
    //轉賬匯款
    public static void moneygogogo() {
        a.setAccountbalance(a.getAccountbalance());
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t請輸入轉賬賬戶\n");
        String str=s.next();
        if(a.getAccountID().equals(str)) {
            System.out.println("\t*************************************************\n");
            System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
            System.out.println("\t*************************************************\n");
            System.out.println("\t\t請輸入轉賬金額\n");
            int num=s.nextInt();
            if(a.getAccountbalance()<=num){
                System.out.println("賬戶餘額不足\n");
            }else {
                a.setAccountbalance(a.getAccountbalance() - num);
                System.out.println("\t*************************************************\n");
                System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
                System.out.println("\t*************************************************\n");
                System.out.println("\t\t當前賬戶向"+a.getAccountID()+"成功轉賬"+str+"元");
                System.out.println("\t\t當前賬戶餘額為"+a.getAccountbalance()+"元\n");
                System.out.println("\t*************************************************\n");
            }
        }
        Menu();
    }
    //修改密碼
    public static void update() throws IOException {
        fileout();
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.println("\t\t請輸入當前密碼:\n");
        String str=s.next();
        if(!a.getAccountpassword().equals(str)) {
            System.out.println("當前密碼錄入錯誤");
        }else {
            System.out.println("\t\t請輸入修改密碼:\n");
            String str1=s.next();
            System.out.println("\t\t請輸入確認密碼:\n");
            String str2=s.next();
            if(!str1.equals(str2)) {
                System.out.println("修改密碼與確認密碼不一致\n");
            }else {
                System.out.println("\t*************************************************\n");
                System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
                System.out.println("\t*************************************************\n");
                System.out.println("\t\t當前賬戶密碼修改成功");
                System.out.println("\t*************************************************\n");
            }
        }
        Menu();
    }
    //查詢餘額
    public static void moneyhere() {
        System.out.println("\t*************************************************\n");
        System.out.println("\t  歡迎全世界最有錢的人使用中國工商銀行自助櫃員系統\n");
        System.out.println("\t*************************************************\n");
        System.out.print("\t\t當前賬戶餘額為"+a.getAccountbalance()+"元\n");
        System.out.print("\t\t賬戶清單資訊為:\n");
        System.out.print("\t\t操作日期"+a.getOperatedate()+" 操作型別"+a.getOperatetype()+"操作金額"+a.getAmount()+"\n");
        System.out.println("\t*************************************************\n");
        Menu();
    }
}
View Code