一:前言
本人作为一个java初学者,最近在学习过程中成功做出了一些个人认为还算可以的成果,另外在csdn上发现这方面的资料较少,所以心血来潮发了这篇文章,希望可以给和我一样的萌新一些帮助!另外也欢迎大佬们作出指正。
二:总览
主要由基于ATM机的一个主类,围绕衍生出账号类(Account),登录类(login),图像类(view),和其他的一些包括交互,功能等一些方面的类。
具体简介已经在代码中,不多赘叙。
附上主要界面图(也刚好对应后续的包):
三:具体代码
1:aTM包
(1)Account类
package myATM.aTM;
import myATM.otherConduct.Record;
public class Account
{
private String Register;
private String Passport;
private int Money=100;
private String userName;
private Record record=new Record();
public String getRegister() {
return Register;
}
public void setRegister(String register) {
this.Register = register;
}
public String getPassport() {
return Passport;
}
public void setPassport(String passport) {
this.Passport = passport;
}
public int getMoney() {
return Money;
}
public void setMoney(int money) {
this.Money = money;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
//获取记录
public Record getRecord() {
return record;
}
}
(2)ATM类
package myATM.aTM;
import myATM.otherConduct.DisplayAlert;
import myATM.otherConduct.GetCurrentTime;
import myATM.otherConduct.RandomName;
import java.util.Objects;
public class ATM
{
private static int accountNumber=10;
private static Account[] account=new Account[accountNumber];
private static int currentAccountNumber=0;
private static RandomName randomName=new RandomName();
//登录
public static int logAccount(String register,String passport)
{
int i; //i代表这是第几个账号
for(i=1;i<=currentAccountNumber;i++)
{
if(account[i-1].getRegister().equals(register))
break;
}
if(i>currentAccountNumber)
{
DisplayAlert.display("输入的账号错误或者未注册!");
return 0;
}
else
{
if(account[i-1].getPassport().equals(passport))
{
account[i-1].getRecord().addRecord(STR."登录账户 操作时间:\{GetCurrentTime.getCurrentTime()}");
return i;
}
else
{
DisplayAlert.display("输入的密码错误!");
return 0;
}
}
}
//随机生成一个用户名
public static String generateRandomName()
{
return randomName.randName().toString();
}
//注册一个新账号,并设置密码和用户名
public static void RegisterAccount(String register,String username,String passport)
{
if(currentAccountNumber<10)
{
account[currentAccountNumber]=new Account();
account[currentAccountNumber].setRegister(register);
account[currentAccountNumber].setPassport(passport);
account[currentAccountNumber].setUserName(username);
account[currentAccountNumber].getRecord().addRecord(STR."注册账户 时间:\{GetCurrentTime.getCurrentTime()}");
currentAccountNumber++;
}
else{
DisplayAlert.display("当前的账号数已经有10个了!");
}
}
//修改某一个账号的密码
public static void changeAccountPassport(int n,String passport)
{
account[n-1].setPassport(passport);
account[n-1].getRecord().addRecord(STR."修改密码 时间:\{GetCurrentTime.getCurrentTime()}");
}
//修改某一个账号的用户名
public static void changeUsername(int n,String username)
{
account[n-1].setUserName(username);
account[n-1].getRecord().addRecord(STR."修改用户名 时间:\{GetCurrentTime.getCurrentTime()}");
}
//给账号存款
public static void addMoney(int n,int money)
{
account[n-1].setMoney(account[n-1].getMoney()+money);
account[n-1].getRecord().addRecord(STR."存入 \{money} 时间:\{GetCurrentTime.getCurrentTime()}");
}
//给某个账号提款
public static void cutMoney(int n,int money)
{
if(account[n-1].getMoney()<money)
{
DisplayAlert.display("当前账户余额不足");
}
else
{
account[n-1].setMoney(account[n-1].getMoney()-money);
account[n-1].getRecord().addRecord(STR."提款 \{money} 时间:\{GetCurrentTime.getCurrentTime()}");
}
}
//转账,从n账号转到m账号
public static void transMoney(int n,int m,int money)
{
if(account[n-1].getMoney()<money)
{
DisplayAlert.display(STR."账户 \{account[n-1].getRegister()} 的余额不足");
}
else
{
account[n-1].setMoney(account[n-1].getMoney()-money);
account[m-1].setMoney(account[m-1].getMoney()+money);
account[n-1].getRecord().addRecord(STR."向账户 \{account[m-1].getUserName()} 转出 \{money} 时间:\{GetCurrentTime.getCurrentTime()}");
account[m-1].getRecord().addRecord(STR."账户 \{account[n-1].getUserName()} 转入 \{money} 时间:\{GetCurrentTime.getCurrentTime()}");
}
}
//传入当前登录的账号
public static Account getAccount(int i) {
return account[i-1];
}
public static int getCurrentAccountNumber() {
return currentAccountNumber;
}
//通过账号知道这是第几个账号
public static int getTheAccount(String register)
{
for(int i=1;i<=currentAccountNumber;i++){
if(Objects.equals(account[i - 1].getRegister(), register))
return i;
}
return 0;
}
}
(3)ATMRegisterAction类
package myATM.aTM;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import myATM.MainApp;
import myATM.otherConduct.DisplayAlert;
import java.io.IOException;
public class ATMRegisterAction {
@FXML
private TextField register;
@FXML
private TextField passport;
@FXML
private TextField username;
//随机生成一个用户名
@FXML
public void randomName()
{
username.setText(ATM.generateRandomName());
}
//注册
@FXML
public void Register()
{
ATM.RegisterAccount(register.getText(), username.getText(), passport.getText());
DisplayAlert.display("注册成功!",STR."欢迎新用户:\{ATM.getAccount(ATM.getCurrentAccountNumber()).getUserName()}",STR."当前账户数目:\{ATM.getCurrentAccountNumber()}");
}
//返回登陆界面
@FXML
public void Return() throws IOException {
MainApp.setRoot("login/ATMLogin.fxml");
}
}
(4)ATMRegisterView(fxml文件)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myATM.aTM.ATMRegisterAction">
<children>
<Label layoutX="120.0" layoutY="70.0" prefHeight="25.0" prefWidth="50.0" text="账号:">
<font>
<Font size="16.0" />
</font></Label>
<Label layoutX="120.0" layoutY="140.0" prefHeight="25.0" prefWidth="71.0" text="用户名:">
<font>
<Font size="16.0" />
</font></Label>
<Label layoutX="120.0" layoutY="210.0" prefHeight="25.0" prefWidth="50.0" text="密码:">
<font>
<Font size="16.0" />
</font></Label>
<TextField fx:id="register" layoutX="192.0" layoutY="71.0" />
<TextField fx:id="username" layoutX="192.0" layoutY="141.0" />
<TextField fx:id="passport" layoutX="192.0" layoutY="211.0" />
<Button layoutX="375.0" layoutY="141.0" mnemonicParsing="false" onAction="#randomName" text="随机" />
<Button layoutX="241.0" layoutY="276.0" mnemonicParsing="false" onAction="#Register" prefHeight="25.0" prefWidth="50.0" text="注册" />
<Label layoutX="375.0" layoutY="215.0" prefHeight="15.0" prefWidth="125.0" text="(别忘记密码噢)" textFill="#e40b0b" />
<Button layoutX="464.0" layoutY="310.0" mnemonicParsing="false" onAction="#Return" text="返回">
<font>
<Font name="System Bold Italic" size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
2:login包
(1)LoginAction类
package myATM.login;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import myATM.aTM.ATM;
import myATM.otherConduct.DisplayAlert;
import myATM.MainApp;
import myATM.view.ViewAction;
import java.io.IOException;
public class LoginAction {
@FXML
private TextField register;
@FXML
private TextField passport;
//登录
@FXML
public void login() throws IOException
{
System.out.println(ATM.logAccount(register.getText(),passport.getText()));
if(ATM.logAccount(register.getText(),passport.getText())!=0){
ViewAction.setTheAccount(ATM.logAccount(register.getText(),passport.getText()));
MainApp.setRoot("view/ATMView.fxml");
System.out.println(ATM.logAccount(register.getText(),passport.getText()));
}
}
//注册账号
@FXML
public void register() throws IOException {
MainApp.setRoot("aTM/ATMRegisterView.fxml");
}
//忘记密码
@FXML
public void forgetPassport()
{
DisplayAlert.display("忘记密码了?","不是哥们,这都能忘记???","冷静一下,自己好好想一下");
}
}
(2)ATMLogin(fxml文件)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myATM.login.LoginAction">
<children>
<Label layoutX="100.0" layoutY="100.0" prefHeight="27.0" prefWidth="61.0" text="账号:">
<font>
<Font size="20.0" />
</font></Label>
<Label layoutX="100.0" layoutY="180.0" prefHeight="27.0" prefWidth="61.0" text="密码:">
<font>
<Font size="20.0" />
</font></Label>
<Label layoutX="14.0" layoutY="14.0" prefHeight="27.0" prefWidth="134.0" text="Welcom to use!" textFill="#3f8f18" wrapText="true">
<font>
<Font name="System Bold Italic" size="16.0" />
</font></Label>
<Button layoutX="240.0" layoutY="239.0" mnemonicParsing="false" onAction="#login" text="登录" textFill="#c03a3a" />
<Button layoutX="451.0" layoutY="274.0" mnemonicParsing="false" onAction="#register" text="注册" />
<Button layoutX="451.0" layoutY="311.0" mnemonicParsing="false" onAction="#forgetPassport" text="忘记密码" />
<TextField fx:id="register" alignment="TOP_LEFT" layoutX="153.0" layoutY="102.0" prefHeight="23.0" prefWidth="227.0" />
<TextField fx:id="passport" layoutX="153.0" layoutY="182.0" prefHeight="23.0" prefWidth="227.0" />
</children>
</AnchorPane>
3:otherConduct包
(1) DisplayAlert类
package myATM.otherConduct;
import javafx.scene.control.Alert;
public class DisplayAlert {
private static Alert alert=new Alert(Alert.AlertType.INFORMATION);
//显示警示框
public static void display(String a)
{
alert.setTitle("警告!");
alert.setHeaderText("出现错误!");
alert.setContentText(a);
alert.show();
}
public static void display(String a,String b,String c)
{
alert.setTitle(a);
alert.setHeaderText(b);
alert.setContentText(c);
alert.show();
}
}
(2)DisPlayDialog类
package myATM.otherConduct;
import javafx.scene.control.TextInputDialog;
public class DisplayDialog {
private static TextInputDialog dialog=new TextInputDialog();
//通过对话框输入金额
public static int inputMoney()
{
dialog.setTitle("输入");
dialog.setHeaderText("提示");
dialog.setContentText("请输入金额:");
return Integer.valueOf(dialog.showAndWait().get());
}
//通过对话框输入要转移到的账户
public static String inputAccount()
{
dialog.setTitle("输入");
dialog.setHeaderText("提示");
dialog.setContentText("请输入要转款的账号:");
return dialog.showAndWait().get();
}
//通过对话框知道要修改的新密码
public static String inputPassport()
{
dialog.setTitle("输入");
dialog.setHeaderText("提示");
dialog.setContentText("请输入新密码:");
return dialog.showAndWait().get();
}
//对话框获取新的用户名
public static String inputUsername()
{
dialog.setTitle("输入");
dialog.setHeaderText("提示");
dialog.setContentText("请输入新的用户名:");
return dialog.showAndWait().get();
}
}
(3)GetCurrentTime类
package myATM.otherConduct;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class GetCurrentTime {
public static String getCurrentTime()
{
LocalDateTime now=LocalDateTime.now();
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(dateTimeFormatter);
}
}
(4)RandomName类
package myATM.otherConduct;
import java.util.Random;
public class RandomName {
//由于如果用Unicode码来随机生成的名字非常怪异且不符合中国人的常见姓名,所以我选择用纯手打先来确定常见姓名组合
private static final char[] surName={'李','刘','王','张','黄','宋','陈','赵','肖','谢'}; //0~9
private static final char[] name1={'文','武','斌','宗','仁','知','朝','培','元','雅','正','天','吉','欣'}; //0~13
private static final char[] name2={'明','河','新','智','增','羽','珍','坤','鸢','知','桓','谯','子','冀'}; //0~13
public StringBuilder randName()
{
Random rand=new Random();
int length=rand.nextInt(2,4);
StringBuilder name=new StringBuilder();
name.append(surName[rand.nextInt(0, 10)]);
name.append(name1[rand.nextInt(0, 14)]);
if(length==3) name.append(name2[rand.nextInt(0, 14)]);
return name;
}
}
(5)Record类
package myATM.otherConduct;
public class Record
{
private int maxRecord=1000;
private int currentRecord=0;
private String[] theRecord=new String[maxRecord];
//获取新的操作记录
public void addRecord(String record)
{
if(currentRecord<=100)
{
theRecord[currentRecord]= "";
theRecord[currentRecord] = record;
currentRecord++;
}
else{
DisplayAlert.display("当前记录已经满了!");
}
}
//删除操作记录
//直接删除最近的一条
public void deleteRecord()
{
if(currentRecord>0) {
currentRecord--;
theRecord[currentRecord]="";
}
else{
DisplayAlert.display("当前没有任何记录!");
}
}
//删除特定的一条(由于链表学的不是很扎实,先用数组实现)
public void deleteRecord(int n)
{
if(theRecord[n-1]!=null)
{
for (int i = n; i <= currentRecord; i++) {
theRecord[i - 1] = theRecord[i];
}
}
else{
DisplayAlert.display(STR."没有第\{n}条记录");
}
}
//获取最新的一条操作记录
public String getTheRecord(int i) {
return theRecord[i];
}
public int getCurrentRecord() {
return currentRecord;
}
}
4:view包
(1)ViewAction类
package myATM.view;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import myATM.MainApp;
import myATM.aTM.ATM;
import javafx.scene.control.TextField;
import myATM.otherConduct.DisplayAlert;
import myATM.otherConduct.DisplayDialog;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class ViewAction implements Initializable
{
@FXML
private ListView<String> listView;
private ObservableList<String> observableList= FXCollections.observableArrayList();
private static int theAccount;
//传入账号
public static void setTheAccount(int i) {
ViewAction.theAccount=i;
}
@FXML
private TextField register;
@FXML
private TextField money;
@FXML
private TextField username;
//初始化数据
@Override
public void initialize(URL location, ResourceBundle resources)
{
register.setText(ATM.getAccount(theAccount).getRegister());
money.setText(STR."\{ATM.getAccount(theAccount).getMoney()}");
username.setText(ATM.getAccount(theAccount).getUserName());
}
//存款
@FXML
public void keepMoney() {
ATM.addMoney(theAccount,DisplayDialog.inputMoney());
money.setText(STR."\{ATM.getAccount(theAccount).getMoney()}");
}
//提款
@FXML
public void withdrawal() {
ATM.cutMoney(theAccount,DisplayDialog.inputMoney());
money.setText(STR."\{ATM.getAccount(theAccount).getMoney()}");
}
//转账
@FXML
public void transfer() {
ATM.transMoney(theAccount,ATM.getTheAccount(DisplayDialog.inputAccount()),DisplayDialog.inputMoney());
money.setText(STR."\{ATM.getAccount(theAccount).getMoney()}");
}
//查询账号操作记录
@FXML
public void inquireRecord()
{
for(int i=0;i<ATM.getAccount(theAccount).getRecord().getCurrentRecord();i++){
observableList.add(ATM.getAccount(theAccount).getRecord().getTheRecord(i));
}
listView.setItems(observableList);
}
//返回登录界面
@FXML
public void Return() throws IOException {
MainApp.setRoot("login/ATMLogin.fxml");
}
//修改密码
@FXML
public void modifyPassport() {
ATM.changeAccountPassport(theAccount,DisplayDialog.inputPassport());
}
//修改用户名
@FXML
public void modifyUsername() {
ATM.changeUsername(theAccount,DisplayDialog.inputUsername());
username.setText(ATM.getAccount(theAccount).getUserName());
}
//修改余额(滑稽)
@FXML
public void modifyMoney()
{
DisplayAlert.display("你在想什么?","不是哥们?!","这你都想改?");
}
//删除记录
@FXML
public void deleteRecord()
{
ATM.getAccount(theAccount).getRecord().deleteRecord();
inquireRecord();
}
//删除特定的记录
@FXML
public void deleteSpecialRecord(){
DisplayAlert.display("提醒","作者技术不足!!!","敬请期待!");
}
}
(2)ATMView(fxml文件)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myATM.view.ViewAction">
<children>
<Label layoutX="356.0" prefHeight="41.0" prefWidth="243.0" text="Please choose your need service!" textFill="#c76d0c">
<font>
<Font name="System Bold Italic" size="14.0" />
</font>
</Label>
<Button layoutX="348.0" layoutY="60.0" mnemonicParsing="false" onAction="#keepMoney" prefHeight="40.0" prefWidth="85.0" text="存款" textFill="#10ef31">
<font>
<Font name="System Bold" size="20.0" />
</font></Button>
<Button layoutX="500.0" layoutY="60.0" mnemonicParsing="false" onAction="#withdrawal" prefHeight="40.0" prefWidth="85.0" text="取款" textFill="#0cdb73">
<font>
<Font name="System Bold Italic" size="20.0" />
</font></Button>
<Button layoutX="348.0" layoutY="130.0" mnemonicParsing="false" onAction="#transfer" prefHeight="40.0" prefWidth="85.0" text="转账" textFill="#03d4bc">
<font>
<Font name="System Bold Italic" size="20.0" />
</font></Button>
<Button layoutX="424.0" layoutY="210.0" mnemonicParsing="false" onAction="#Return" prefHeight="40.0" prefWidth="85.0" text="返回" textFill="#0a72e9">
<font>
<Font name="System Bold Italic" size="20.0" />
</font></Button>
<Button layoutX="500.0" layoutY="130.0" mnemonicParsing="false" onAction="#inquireRecord" prefHeight="40.0" prefWidth="85.0" text="查询" textFill="#0fb9e4">
<font>
<Font name="System Bold Italic" size="20.0" />
</font></Button>
<Label layoutX="45.0" layoutY="4.0" prefHeight="34.0" prefWidth="113.0" text="Your message:" textFill="#d90a0a">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="54.0" prefHeight="34.0" prefWidth="61.0" text="账户:">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="123.0" text="用户名:">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="189.0" text="余额:">
<font>
<Font size="16.0" />
</font>
</Label>
<TextField fx:id="register" layoutX="77.0" layoutY="59.0" />
<TextField fx:id="username" layoutX="77.0" layoutY="121.0" />
<TextField fx:id="money" layoutX="77.0" layoutY="188.0" />
<Button layoutX="246.0" layoutY="119.0" mnemonicParsing="false" onAction="#modifyUsername" prefHeight="23.0" prefWidth="85.0" text="修改用户名" textAlignment="CENTER" wrapText="true">
<font>
<Font name="System Bold Italic" size="12.0" />
</font>
</Button>
<Button layoutX="246.0" layoutY="59.0" mnemonicParsing="false" onAction="#modifyPassport" text="修改密码">
<font>
<Font name="System Bold Italic" size="12.0" />
</font>
</Button>
<Button layoutX="246.0" layoutY="189.0" mnemonicParsing="false" onAction="#modifyMoney" text="修改">
<font>
<Font name="System Bold Italic" size="12.0" />
</font></Button>
<ListView fx:id="listView" layoutX="27.0" layoutY="250.0" prefHeight="147.0" prefWidth="380.0" />
<Label layoutX="15.0" layoutY="230.0" text="记录:" textFill="#ac8747" />
<Button layoutX="424.0" layoutY="270.0" mnemonicParsing="false" onAction="#deleteRecord" text="删除">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button layoutX="424.0" layoutY="312.0" mnemonicParsing="false" onAction="#deleteSpecialRecord" text="删除这条">
<font>
<Font name="System Bold Italic" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
5:主函数
package myATM;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApp extends Application
{
private static Stage primayStage;
private static AnchorPane root;
@Override
public void start(Stage primaryStage) throws Exception
{
MainApp.primayStage=primaryStage;
MainApp.primayStage.setTitle("ATM机 作者:cz Huang");
initialize();
primaryStage.show();
}
//切换页面
public static void setRoot(String rooter) throws IOException
{
root= FXMLLoader.load(MainApp.class.getResource(rooter));
Scene scene=new Scene(root);
primayStage.setScene(scene);
}
//初始化页面
public void initialize() throws IOException
{
root=FXMLLoader.load(MainApp.class.getResource("login/ATMLogin.fxml"));
Scene scene=new Scene(root);
primayStage.setScene(scene);
}
public static void main(String[] args){
launch(args);
}
}
四:小结
相信认真看完代码的读者能轻松发现,其实代码中还存在很多bug, 的确,由于本人写这篇文章的时间过急,一些bug还没来得及修改,但所幸无伤大雅,对整体的影响不会太大。
最后,感谢每一位读者,后续我可能还会发布一个我之前写的一个类似的简单播放器,有兴趣的读者可以关注我。
想要直接获取源码可以私聊我