import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;java
//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;dom
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;
}this
// 運行
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("您的帳戶被凍結!請到櫃檯處理!");
}
}orm
// 顯示歡迎
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("一、查詢;二、存款;三、取款;四、修改密碼;五、退出");
String choiceStr = scan.next();
if(choiceStr.matches("[1-5]")){
choice = Integer.parseInt(choiceStr);
}
return choice;
}get
// 查詢餘額
private void query() {
//直接顯示user對象中的account屬性值
System.out.println("您當前的餘額是:" + user.getAccount() + "元!");
}input
// 存錢
private void storeMoney() {
//一、接收用戶輸入
//二、判斷--不能爲0,不能爲負,不能不是100的倍數,不能超過存取上限,不能超過現金容量--給出提示
//三、判斷經過--加現金,加餘額
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之內的百元服務!");
}
}it
// 取款
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之內的百元服務!");
}
}io
// 修改密碼
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));
}
}