码迷,mamicode.com
首页 > 其他好文 > 详细

更进ATM

时间:2016-06-05 23:13:04      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

//ATM机
public class ATMMachine {
//所有用户信息
private UserInfo[] allUsers;
//当前操作用户信息
private UserInfo user;
// 现金
private int cash;
// 现金容量上限
public static final int MAX_CASH = 200000;
// 存取上限
public static final int GET_SET_MAX = 2000;

public ATMMachine() {
// 预载入用户信息---文件读操作
this.allUsers = FileOperation.readFile();
// this.allUsers = new UserInfo[5];
// for (int i = 0; i < allUsers.length; i++) {
// this.allUsers[i] = new UserInfo("zhang" + i, "123456" + i,
// 10000 + i * 1000);
// }
this.cash = 100000;
}

// 运行
public void run() {
this.welcome();
boolean flag = this.login();
if (flag) {
System.out.println("用户" + this.user.getUsername() + "于" +
this.getDate()+ "登录成功,欢迎您!");
while (true) {
int choice = this.choiceMenu();
switch (choice) {
case 1:
this.query();
break;
case 2:
this.storeMoney();
break;
case 3:
this.getMoney();
break;
case 4:
this.changePwd();
break;
case 5:
this.exit();
break;
default:
System.out.println("对不起,没有该选项!");
break;
}
}
} else {
System.out.println("您的账户被冻结!请到柜台处理!");
}
}

// 显示欢迎
private void welcome() {
System.out.println("******************************");
System.out.println("*********欢**迎**登**录*********");
System.out.println("******************************");
System.out.println("*********爱存不存银行ATM*********");
System.out.println("******************************");
System.out.println("*****************version1.0***");
}

// 登录
private boolean login() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String inputName = scan.next();
System.out.println("请输入密码:");
String inputPwd = scan.next();
for (UserInfo tmp : allUsers) {
if (tmp.getUsername().equals(inputName)
&& tmp.getPassword().equals(inputPwd)) {
this.user = tmp;
return true;
}
}
System.out.println("输入有误!您还有" + (2 - i) + "次机会");
}
return false;
}

// 选择菜单
private int choiceMenu() {
int choice = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请选择您要执行的操作:");
System.out.println("1、查询;2、存款;3、取款;4、修改密码;5、退出");

String choiceStr = scan.next();
if(choiceStr.matches("[1-5]")){
choice = Integer.parseInt(choiceStr);
}

return choice;
}

// 查询余额
private void query() {
//直接显示user对象中的account属性值
System.out.println("您当前的余额是:" + user.getAccount() + "元!");
}

// 存钱
private void storeMoney() {
//1、接收用户输入
//2、判断--不能为0,不能为负,不能不是100的倍数,不能超过存取上限,不能超过现金容量--给出提示
//3、判断通过--加现金,加余额
System.out.println("请输入您要存储的金额:");
String inputMoney = new Scanner(System.in).next();

if(inputMoney.matches("(20|1[0-9]|[1-9])00")){
int money = Integer.parseInt(inputMoney);
if(money + this.cash > this.MAX_CASH){
System.out.println("对不起,本机容量不足!");
}else{
this.cash += money;
this.user.setAccount(this.user.getAccount() + money);
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功存款" + money + "元!");
}
}else{
System.out.println("请输入有效金额!本机只提供2000以内的百元服务!");
}
}

// 取款
private void getMoney() {
System.out.println("请输入您要取出的金额:");
String inputMoney = new Scanner(System.in).next();
if(inputMoney.matches("(20|1[0-9]|[1-9])00")){
int money = Integer.parseInt(inputMoney);
if(money >= this.cash){
System.out.println("对不起,本机现金不足!");
}else if(money > this.user.getAccount() ){
System.out.println("对不起,您的账户上余额不足!");
}else{
this.user.setAccount(this.user.getAccount() - money);
this.cash -= money;
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功取款" + money + "元!");
}
}else{
System.out.println("请输入有效金额!本机只提供2000以内的百元服务!");
}
}

// 修改密码
private void changePwd() {
Scanner scan = new Scanner(System.in);
System.out.println("请输入老密码:");
String oldPwd = scan.next();
System.out.println("请输入新密码:");
String newPwd = scan.next();
System.out.println("再次输入新密码:");
String rePwd = scan.next();

if(!oldPwd.equals(this.user.getPassword())){
System.out.println("老密码输入错误!");
}else if(!newPwd.equals(rePwd)){
System.out.println("两次密码不一致!");
}else{
this.user.setPassword(newPwd);
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功修改密码!");
}


}

// 退出
private void exit() {
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}

//格式化日期
private String getDate(){
String date = "";
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
date = sdf.format(now);
return date;
}

}

 

 

 

 

 


//用户信息
public class UserInfo {
//账号
private String username;
//密码
private String password;
//余额
private float account;

public UserInfo(){

}

public UserInfo(String username, String password, float account) {
this.username = username;
this.password = password;
this.account = account;
}

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public float getAccount() {
return account;
}
public void setAccount(float account) {
this.account = account;
}
}

 

 

 

 

 

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class FileOperation {

//读文件
public static UserInfo[] readFile(){
//把文件中的数据先读到Properties容器中
Properties S=new Properties();
try {
S.load(new FileInputStream("User.properties"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把Properties中的字符串数据转换为UserInfo对象,放到UserInfo[]中
UserInfo[] B=new UserInfo[S.size()];
for(int i=0;i<S.size();i++){
String A=S.getProperty(Integer.toString(i));
String[] values = A.split("&");
UserInfo user=new UserInfo(values[0],values[1],Float.parseFloat(values[2]) );
B[i]=user;
}
return B;
}


//写文件
public static void writeFile(UserInfo[] allUsers){
//把UserInfo[]中的数据,放入到Properties中
Properties S=new Properties();
UserInfo[] A = allUsers;
for(int i=0;i<A.length;i++){
S.setProperty(Integer.toString(i), A[i].getUsername()+"&"+A[i].getPassword()+"&"+A[i].getAccount());
}
//把Properties中的数据写入文件中

try {
S.store(new FileOutputStream("User.properties"), null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

 

 

 

 

 

public class TestMain {

public static void main(String[] args) {
// TODO Auto-generated method stub

ATMMachine machine = new ATMMachine();
machine.run();

// System.out.println((int)(Math.random() * 10));


}

}

 

更进ATM

标签:

原文地址:http://www.cnblogs.com/zjh-/p/5561990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!