java游戏ATM机

使用编程习题9.7中创建的Account类来模拟一台ATM机。创建一个有10个账户的数组,其id为0,1,…9,并初始化收支为100美元,系统提示用户输入一个id,就显示如运行实例所示的主菜单。—java语言程序设计基础篇10.7

原程序本是死循环,这里改成输入1继续。

import java.util.*;
public class nine107 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        accountatm atm=new accountatm();
        atm.init();
        System.out.print("enter the id:");
        int id=input.nextInt();
        atm.enterid(id);
    }

}
class accountatm{
    private int id;
    int count;
    int choice,count1=0;
    int mony;
    Scanner input=new Scanner(System.in);
    public int []a=new int[10];
    public void init(){
        for(int i=0;i<10;i++)
            a[i]=100;
    }
    public void enterid(int id) {
        this.id=id;
        count=0;
        while(this.id<0||this.id>10)
        {
            System.out.print("invalid id!plez enter new:");
            this.id=input.nextInt();
            if(count++==4)
            {
                System.out.println("times to upper limit!");
                System.exit(1);
            }       
        }
        ptmenu();
    }
        void ptmenu() {
        System.out.printf("1.check balance\n2.withdraw\n3.deposit\n4.exit\n");
        System.out.print("enter a choice:");
        choice=input.nextInt();
        while(choice<1||choice>4) {
            System.out.println("invalid choice!plez enter the choice: ");
            choice=input.nextInt();
            if(count1++==4)
                System.exit(1);
        }
        if(choice==1)
            checkbalance(id);
        else if(choice==2)
        {   
            id=input.nextInt();
            mony=input.nextInt();
            withdraw(id,mony);}
        else if(choice==3) {

            id=input.nextInt();
            mony=input.nextInt();
            deposit(id,mony);
        }

        else
        {
            System.out.println("at the end!");
            System.exit(1);
        }
    }
    public void checkbalance(int id) {
        System.out.println("mony :"+a[id]);
        System.out.print("enter 1 to continue:");
        if(input.nextInt()==1)
            ptmenu();
        else
            System.exit(1);
    }
    void withdraw(int id,int mony) {
        if(mony>a[id]) {
            System.out.println("over the limit!");
        }
        else
            a[id]-=mony;
        System.out.println(mony+" you get,the remaining :"+a[id]);
        System.out.print("enter 1 to continue:");
        if(input.nextInt()==1)
            ptmenu();
        else
            System.exit(1);

    }
    void deposit(int id,int mony) {
        a[id]+=mony;
        System.out.println(mony+" you save,the total : "+a[id]);
        if(input.nextInt()==1)
            ptmenu();
        else
            System.exit(1);
    }

}

猜你喜欢

转载自blog.csdn.net/j_linlian/article/details/79722012