对象

 

例:

ATM机的程序实现

ATM机主程序:

package com.lovo.bean;

import java.util.Scanner;

public class ATMMachine {

    private UserInf [] allUsers = new UserInf [5];
    
    private int cash = 200000;
    
    public final int MAX_CASH = 1000000;
    
    UserInf theUser = new UserInf();
    
    public ATMMachine(){
        this.allUsers[0] = new UserInf("1224059","asw234",10000);
        this.allUsers[1] = new UserInf("1224060","asw345",10000);
        this.allUsers[2] = new UserInf("1224061","asw456",10000);
        this.allUsers[3] = new UserInf("1224062","asw567",10000);
        this.allUsers[4] = new UserInf("1224063","asw678",10000);
    }
    
    //运行
    public void run(){
        this.welcome();
        boolean flag = this.login();
        if(flag){
            while(true){
                int choice = this.chooseMenu();
                switch (choice) {
                case 1:
                    this.qurey();
                    break;

                case 2:
                    this.storeMoney();
                    break;
                    
                case 3:
                    this.getMoney();
                    break;

                case 4:
                    this.bankTransfer();
                    break;
                    
                case 5:
                    this.changePwd();
                    break;
                    
                case 6:
                    this.login();
                    break;

                case 7:
                    this.exit();
                    break;
                    
                default:
                    System.out.println("输入错误!");
                    break;
                }
            }
        }else {
            System.out.println("您的卡被没收,请带上有效证件到前台处理!");
        }
    }
    
    //欢迎界面
    private void welcome(){
        System.out.println("***欢迎光临!***");
    }
    
    //登录
    private boolean login(){
        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入您的卡号:");
            String inputCardNum = scan.next();
            System.out.println("请输入您的密码:");
            String inputPwd = scan.next();
            
            for (int j = 0; j < this.allUsers.length; j++) {
                if(inputCardNum.equals(this.allUsers[j].getCardNum()) && 
                        inputPwd.equals(this.allUsers[j].getPassword())){
                    System.out.println("登录成功!");
                    this.theUser = this.allUsers[j];
                    return true;
                }
            }
            System.out.println("卡号或密码错误,您还有"+(2-i)+"请重新输入!");
        }
        return false;
    }
    
    //选择菜单
    private int chooseMenu(){
        Scanner scan = new Scanner(System.in);
        System.out.println("1: 查询余额");
        System.out.println("2: 存款");
        System.out.println("3: 取款");
        System.out.println("4: 转账");
        System.out.println("5: 修改密码");
        System.out.println("6: 退出,重新登录");
        System.out.println("7: 退出系统");
        System.out.println("请输入您的操作:");
        int a = scan.nextInt();
        return a;
    }
    
    //查询余额
    private void qurey(){
        System.out.println("您的余额为:"+theUser.getAccount());
        
    }
    
    //存款
    private void storeMoney(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请放入金额");
        int inputMoney = scan.nextInt();
        if(inputMoney > 0 && inputMoney % 100 == 0){
            if((inputMoney + this.cash) <= this.MAX_CASH){
                this.cash = this.cash + inputMoney;
                theUser.setAccount(theUser.getAccount()+inputMoney);
                System.out.println("存款成功!");
            }else{
                System.out.println("金额太大,无法存储,请到前台存储!");
            }
        }else{
            System.out.println("放入金额中有非100元人民币的钞票,无法存储,请到前台办理!");
        }
        
    }
    
    //取款
    private void getMoney(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要取的金额:");
        int inputGetMoney = scan.nextInt();
        if(inputGetMoney > 0 && inputGetMoney % 100 == 0){
            if(inputGetMoney <= this.cash){
                if(inputGetMoney <= theUser.getAccount()){
                    theUser.setAccount(theUser.getAccount() - inputGetMoney);
                    this.cash = this.cash - inputGetMoney;
                    System.out.println("取款成功!");
                }else{
                    System.out.println("您的余额已不足!");
                }
            }else {
                System.out.println("超出最大取款值,无法取出,请重新输入!");
            }
        }
        else{
            System.out.println("输入金额不是100的倍数,无法取出,请重新输入!");
        }
    }
    //转账
    private void bankTransfer(){
        Scanner scan = new Scanner(System.in);
        System.out.println("输入要转入的账户");
        String inputCardNum = scan.next();
        
        boolean flag = false;
        
        for (int i = 0; i < allUsers.length; i++) {
            if(!(inputCardNum.equals(theUser.getCardNum())) && 
                    inputCardNum.equals(allUsers[i].getCardNum())){
                System.out.println("输入要转账的金额");
                int money = scan.nextInt();
                if(money < 0){
                    System.out.println("您输入的金额为负数,输入有误,请重新输入!");
                }else if(money > this.theUser.getAccount()){
                    System.out.println("您的余额不足,转账失败!");
                }else {
                    theUser.setAccount((theUser.getAccount() - money));
                    allUsers[i].setAccount(allUsers[i].getAccount() + money);
                    System.out.println("转账成功!");
                    return;
                }
            }
        }
        if(flag){
            System.out.println("找不到你要转入的账户,请重新操作!");
        }
        
    }
    
    //修改密码
    private void changePwd(){
        Scanner scan = new Scanner(System.in);
        
        System.out.println("请输入新密码:");
        String passWord = scan.next();
        theUser.setPassword(passWord);
        System.out.println("修改成功,请重新登录!");
        login();
    }
    
    //退出
    private void exit(){
        System.exit(-1);
    }
    
}
/*
定义一个用户类
*/
package com.lovo.bean; public class UserInf { private String cardNum; private String password; private double account; public UserInf(){ } public UserInf(String cardNum, String password, double account) { this.cardNum = cardNum; this.password = password; this.account = account; } public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getAccount() { return account; } public void setAccount(double account) { this.account = account; } }

 实现:

package com.lovo.test;

import com.lovo.bean.ATMMachine;

public class TestMain {

 public static void main(String[] args) {
  ATMMachine atm = new ATMMachine();
  atm.run();
 }
}

原文地址:https://www.cnblogs.com/134-hw/p/6130758.html