Home > Java > Java Tutorial > body text

Java sample code to implement ATM withdrawal project

黄舟
Release: 2017-06-04 09:20:09
Original
3549 people have browsed it

This article mainly introduces the implementation code of java to implement ATM withdrawal project in detail. It has certain reference value. Interested friends can refer to it

Project requirements:

1. The user needs to enter the account password from the console. If the account or password is incorrect, an exception will be reported

2. The amount of daily withdrawals is limited (100, 300 00), otherwise an exception will be reported

3. Each withdrawal must be recorded and displayed during the next withdrawal

Idea:

1 , first generate some users in the "bank category" (skipping the registration link)

 2. You can use List to collect withdrawal logs

 3. You can useMapThe collection establishes a "key-value relationship" between "user name" and corresponding user information

4. Use while loop to control the process

Project implementation code:

Management class (including main method):

import java.util.Scanner;
public class Manager {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    //创建BankServer()类对象
    BankServer server = new BankServer();
    //创建User类对象,先令其为空
    User user = null;
    //调用creatAccount()方法生成四个用户
    server.creatAccount();
    
    //while循环控制流程
    while(true){
      System.out.println("请输入您的账号:");
      String id = input.nextLine();
      System.out.println("请输入您的密码:");
      String password = input.nextLine();
      try {
        user=server.chick(id, password);//调用chick()方法,让user有意义
        System.out.println("===========欢迎进入银行取款系统===========");
        System.out.println("您的账户余额为:"+user.getMoney());
        while(true){
          System.out.println("请输入您的取款金额(必须大于100,小于30000):");
          double money = input.nextDouble();
          server.getMoney(user, money);
          input.nextLine();
          System.out.println("请问您需要继续取款吗? Y or N");
          String selec = input.nextLine();
          if(selec.equalsIgnoreCase("N")){
            System.out.println("欢迎使用!");
            break;
          }
        }
      } catch (Exception e) {
        System.out.println(">>> 账号/密码错误!请重新登录输入 <<<");
      }
    }
  }

}
Copy after login

User class (User class):

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class User {
  private double balance;//用户账户金额
  private String id;//用户账号名
  private String passward;//用户密码
  private List list=new ArrayList<>() ;//用于存储用户取款记录的日志集合
  
  //无参构造方法
  public User() {
    super();
  }
  //有参构造方法
  public User(String id, String passward,double money) {
    super();
    this.balance = money;
    this.id = id;
    this.passward = passward;
  }
  
  //各个属性的set和get方法
  public double getMoney() {
    return balance;
  }
  public void setMoney(int money) {
    this.balance = money;
  }
  public String getAccount() {
    return id;
  }
  public void setAccount(String account) {
    this.id = account;
  }
  public String getPassward() {
    return passward;
  }
  public void setPassward(String passward) {
    this.passward = passward;
  }
  public List getList() {
    return list;
  }
  public void setList(List list) {
    this.list = list;
  }
  public double getBalance() {
    return balance;
  }
  public void setBalance(double balance) {
    this.balance = balance;
  }
  
  /**
   * 因为账户的密码被装在map集合中,不方便直接访问,所以在User类中定义一个检查密码是否正确的方法
   * 其返回一个布尔类型的值
   * @param password
   * @return
   */
  public boolean checkPassword(String password){
    if(password.equals(this.passward)){
      return true;
    }else{
      return false;
    }
  }
  
  
  /**
   * 与上面的方法同理,判断取款金额是否合理的方法,返回一个布尔类型的值
   * @param money
   * @return
   * @throws Exception
   */
  public boolean getMoney(double money) throws Exception {

    if (money < 100 || money > 5000) {  //规定每次取款金额的范围,超过这个范围则抛出异常
      throw new Exception("取款金额不合理!取款金额必须大于100,小于5000");
    }
    if (this.balance < money) {//这个判断条件被许多人忽略,当账户内余额不足时,抛出异常
      throw new Exception("您的余额不足!");
    }
    //从帐号余额中扣除相应金额
    this.balance -= money;
    
    //将取款记录到日志中
    String logStr = User.currentTime()+"\t取款 " + money +"元, 余额:"+this.balance;
    this.list.add(logStr);  //将记录添加进list集合中
    return true;  //返回true
    
  }
  /**
   * 获取当前时间的方法
   * @return
   */
  public static String currentTime(){
    long ct = System.currentTimeMillis();
    Date d = new Date(ct);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:ss:mm");
    return sdf.format(d);
  }
}
Copy after login

Bank Service class (BankServer class):

import java.util.HashMap;
import java.util.Map;
/**
 * 银行服务类,实现用户的创建
 * @author 
 *
 */
public class BankServer {
  //声明四个常量表示账户用户名
  final String AC1 = "1111-111111-1111AA";
  final String AC2 = "2222-222222-2222BB";
  final String AC3 = "3333-333333-3333CC";
  final String AC4 = "4444-444444-4444DD";
  //声明四个常量,用于存储密码,每个账户有一个专属的密码
  final String PASSWORD1 = "111111";
  final String PASSWORD2 = "222222";
  final String PASSWORD3 = "333333";
  final String PASSWORD4 = "444444";

  //声明一个静态常量规定初始金额为100000元
  public static final Double BALANCE = 100000.00;
  
  //创建一个map集合用于存储用户账号和对应的账户信息
  Map map = new HashMap<>();
  
  /**
   * 生成用户的方法,将用户存入map集合中
   */
  public void creatAccount(){
    map.put(AC1, new User(AC1,PASSWORD1,BALANCE));
    map.put(AC2, new User(AC2,PASSWORD2,BALANCE));
    map.put(AC3, new User(AC3,PASSWORD3,BALANCE));
    map.put(AC4, new User(AC4,PASSWORD4,BALANCE));
  }
  
  /**
   * 比较用户从控制台输入的数据是否和银行预先存储的账户一样
   * @param id
   * @param password
   * @return
   * @throws Exception
   */
  public User chick(String id,String password) throws Exception{
    User Account = null;  //此时的Account对象为null
    //密码已经被放进map集合中,不好取出,所以需要在User类中定义一个checkPassword方法,其返回的是布尔类型的值(详情见User类)
    if(map.containsKey(id) ){  
      Account=map.get(id);  //如果账户名对上了,则调用map集合的get()方法给Account对象赋值
      if(Account.checkPassword(password)){
        System.out.println("登录成功!");  //如果密码也对上了,提示“登录成功”,否则抛出异常
      }else{
        throw new Exception("密码错误");
      }
    }else{
      throw new Exception("账号/密码错误!");
    }
    return Account;    //返回一个Account对象
  } 
  /**
   * 这是一个取钱并将记录存入list集合的方法
   * @param loginUA
   * @param money
   */
  public void getMoney(User user, double money) {
    try {
      //用户余额已经被放进map集合中,同理,在User类中定义一个getMoney()方法,其返回的是布尔类型的值
      if(user.getMoney(money)){
        System.out.println("取款成功,用户当前余额:"+user.getBalance());
        System.out.println("==============================");
        //输出日志
        for (String str : user.getList()) {
          System.out.println(str);
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}
Copy after login

Run result 1 (account and password entered correctly):

Run result 2 Incorrect account and password entered ):

The above is the detailed content of Java sample code to implement ATM withdrawal project. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!