目录
版本1:
设计Account1类,包含:
设计测试类ATMMachine1:
版本2:
扩展Account1类为Account2类:
设计测试类ATMMachine2,其主菜单如下:
版本3:
uml啥的找不到了,太久远了,有什么不懂得评论或者私聊问我吧。
版本1:
设计Account1类,包含:
■ 一个名为id 的int 类型的私有数据域(默认值为0),长度为6位。
■ 一个名为balance的double类型的私有数据域(默认值为0)。
■ 一个名为annualInterestRate 的double类型的私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
■ 一个名为dateCreated的Date类型的私有数据域存储账户的开户日期。
■ 一个能创建默认账户的无参构造方法。
■ 一个能创建带特定id和初始余额的构造方法,初始余额不能为负数。
■ id、balance和annualInterestRate 的访问器和修改器。
■ dateCreated的访问器。
■ 一个名为getMonthlyInterestRate的方法返回月利率。
■ 一个名为withDraw的方法从账户提取特定金额。
■ 一个名为deposit的方法向账户存人特定金额。
■ double类型的数据域保留2位小数。
■ 成员方法和数据域应进行基本的合理性检查。
package version1;
import java.util.Date;
class Account1{
Account1() {
id=0;
balance=0;
}
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
Account1(int id,double balance) {
this.id=id;
this.balance=balance;
}
int getId() {
return id;
}
void setId(int id) {
String ID = id + "";
if (ID.length() == 6)//此条件用于验证id是否合法
this.id = id;
}
double getBalance() {
return balance;
}
void setBalance(double balance) {
if (balance >= 0)//验证余额是否大于0
this.balance = balance;
}
double getAnnualInterestRate() {
return Math.round(annualInterestRate * 100) / 100.0;//保留两位小数
}
void setAnnualInterestRate(double annualInterestRate) {
if (annualInterestRate < 1 && annualInterestRate > 0)//验证年利率是否大于0小于1
this.annualInterestRate = annualInterestRate;
}
Date getDateCreated() {
return dateCreated;
}
double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
double getMonthlyInterest() {
return balance*(annualInterestRate/100/12);//返回月利率
}
public void withdraw(double money){
if(balance < money){
System.out.println("The balance you entered is wrong");
}//判断取款金额是否大于余额
else
{
balance=balance-money;;
}
}
public void deposit(double money)
{
if (money>0)
balance=balance+money;//判断存款金额是否大于零
else
System.out.println("The money you entered is wrong");
}
}
设计测试类ATMMachine1:
■ 创建一个有100个账户的数组,其id 为0,1,2,...99, 并初始化收支为1000美元。
■ 主菜单如下:
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
package version1;
import java.util.Scanner;
public class ATMMachine1 {
public static void main(String[] args) {
Account1[] account = new Account1[100];
//按照要求创建有100个账号的数组
for (int i = 0; i < 100; i++) {
account[i]=new Account1();
account[i].setId(100000 + i);
account[i].setBalance(1000);
}//按要求设置id,并且设置余额为1000
for (int i = 0; i < 99; i++) {
try {
Scanner sc=new Scanner(System.in);
boolean flag=true;
//让用户自行选择自己需要的服务
while(flag){
System.out.println("\t\t\t**********Main menu*********");
System.out.println("\t\t\t\t1:check balance");
System.out.println("\t\t\t\t2:withdraw");
System.out.println("\t\t\t\t3:deposit");
System.out.println("\t\t\t\t4:exit");
System.out.println("\t\t\t****************************");
try{
System.out.print("Enter your choice:");//让用户输入自己的选择
int input=sc.nextInt();
switch(input){
case 1:
System.out.println("The balance is "+account[i].getBalance()); break;
case 2:
Scanner m=new Scanner(System.in);
System.out.print("Enter the money you want to withdraw");//输入取钱的金额
double money1 = m.nextDouble();
account[i].withdraw(money1);
System.out.println("The money you withdrew is "+money1);
break;
case 3:
m=new Scanner(System.in);
System.out.print("Enter the money you want to deposit:");//存钱
double money2 = m.nextDouble();
account[i].deposit(money2);
System.out.println("The money you deposited is "+money2);
break;
case 4:
//flag=false;
System.out.println("Exited");//退出
break;
default:
System.out.println("You enter a wrong number");//输入了错误的选择
break;
}
}
catch(Exception e){
System.out.println("Exception!");
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException!");
}
}
}
}
版本2:
扩展Account1类为Account2类:
■ Account2类继承Account1类。
■ 为Account2类新增一个名为password的String类型的私有数据域存储账号密码。password只能为字母或数字,长度不能小于6且不能大于10。
■ 为Account2类新增一个名为name的String类型的私有数据域存储客户名字。
■ 为Account2类新增一个名为transactions的ArrayList类型的新数据域,其为客户存储交易记录。这要求新建一个名为Transaction的类,类的定义请参照教材第10版P381。每笔交易都是Transaction类的一个实例。
■ 新增一个带初始余额的构造方法,其id随机产生,但不能与当前系统的id重复。若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。
■ 重写方法withDraw,要求支取的金额为100的整数倍,并且当日支取金额不能超过5000,支取金额不允许透支。每进行一次操作应向transactions数组线性表添加一笔交易。
■ 重写方法deposit,要求每进行一次操作应向transactions数组线性表添加一笔交易。
■ 新增一个方法changePassword,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码
package version2;
import java.util.Date;
class Account1 {
Account1() {
id=0;
balance=0;
}
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
Account1(int id, double balance) {
this.id=id;
this.balance=balance;
}
int getId() {
return id;
}
void setId(int id) {
String ID = id + "";
if (ID.length() == 6)//此条件用于验证id是否合法
this.id = id;
}
double getBalance() {
return balance;
}
void setBalance(double balance) {
if (balance >= 0)//验证余额是否大于0
this.balance = balance;
}
double getAnnualInterestRate() {
return Math.round(annualInterestRate * 100) / 100.0;//保留两位小数
}
void setAnnualInterestRate(double annualInterestRate) {
if (annualInterestRate < 1 && annualInterestRate > 0)//验证年利率是否大于0小于1
this.annualInterestRate = annualInterestRate;
}
Date getDateCreated() {
return dateCreated;
}
double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
double getMonthlyInterest() {
return balance*(annualInterestRate/100/12);//返回月利率
}
public void withdraw(double money){
if(balance < money){
System.out.println("The balance you entered is wrong");
}//判断取款金额是否大于余额
else
{
balance=balance-money;;
}
}
public void deposit(double money)
{
if (money>0)
balance=balance+money;//判断存款金额是否大于零
else
System.out.println("The money you entered is wrong");
}
}
package version2;
import java.util.*;
/**
*
*/
public class Transaction {
/**
* Default constructor
*/
public Transaction() {
}
private java.util.Date date;
private char type;
private double amount;
private double balance;
private String description;
/**
* @param type
* @param amount
* @param balance
* @param description
*/
public Transaction(char type, double amount, double balance, String description) {
date=new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public java.util.Date getDate() {
return date;
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
}
package version2;
import java.io.Serializable;
import java.util.*;
public class Account2 extends Account1 implements Serializable {
private String password;
private String name;
private java.util.ArrayList<Transaction> transactions;
public Account2() {
int id = (int) (Math.random() * 900000 + 100000);
if (id != getId())
setId(id);
}
public Account2(double balance) {
super.setId((int)(Math.random()));
this.password=password;
this.name=name;
try{
if(balance>0){
super.setBalance(balance);
}
}
catch(Exception e){
System.out.println("The balance you entered is wrong!");
}
}
@Override
public void withdraw(double money) {//取款
double sum = 0;
if (money % 100 != 0) {//判断取款数是不是100的倍数
System.out.println("The money you entered is wrong!");
} else {
if(super.getBalance()<money){
System.out.println("There is insufficient balance in the account");
}
else{
sum = sum + money;
if(sum>5000){//判断每日取款是否超过了5000
System.out.println("The total amount withdrawn today has exceeded 5000 yuan");
}
else{
super.setBalance(super.getBalance()-money);
}
}
}
transactions.add(new Transaction('W',money,super.getBalance(),
"withdraw "+super.getBalance()));//记录本次用户行为
}
@Override
public void deposit(double money)
{
super.setBalance(getBalance()+money);
transactions.add(new Transaction('D',money,super.getBalance(),
"deposit "+super.getBalance()));//记录本次用户行为
}
public void changePassword(String oldPassword, String newPassword1,String newPassword2) {
if (this.password.equals(oldPassword) && newPassword1.equals(newPassword2))//判断旧的密码是否正确 以及两次输入的密码是否相同
this.password=newPassword1;
else if (!this.password.equals(oldPassword))
System.out.println("The old password you entered is wrong!");
else if (!newPassword1.equals(newPassword2))
System.out.println("The two new password you entered is unequal!");
else
this.password=newPassword1;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password=password;
}
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public ArrayList<String> getTransaction(){
java.util.ArrayList<String> str=new ArrayList<>();
for (int i=0;i<transactions.size();i++){
str.add( transactions.get(i).getDescription()+transactions.get(i).getDate());
}
return str;
}
}
设计测试类ATMMachine2,其主菜单如下:
Main menu
0:create a account
1: check balance
2: withdraw
3: deposit
4:details of the transaction
5: change password
6:exit
■ 若用户选择新建一个账号,则应提示用户输入账号password、balance和annualInterestRate,其中id随机产生。新产生的账户应序列化到名为accounts.dat的文件中。所有账户只能通过这种方式产生。
■ 所有用户操作结果应同步到accounts.dat文件中相应账户中。
■ 所有用户操作应有友好、简介的提示语。文章来源:https://www.toymoban.com/news/detail-479395.html
package version2;
import java.io.*;
import java.util.Scanner;
public class ATMMachine2 {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
Account2 account2=new Account2();
boolean flag=true;
while (flag){
System.out.println("**********Main menu**********");
System.out.println("0:create a account");
System.out.println("1:check balance");
System.out.println("2:withdraw");
System.out.println("3:deposit");
System.out.println("4:details of the transaction");
System.out.println("5:change password");
System.out.println("6:exit");
System.out.println("Please enter the choice you want:");
int choice= sc.nextInt();
switch (choice){
case 0: System.out.print("Enter the balance(balance>0):");
double balance = sc.nextDouble();
account2.setBalance(balance);
System.out.print("Enter the annualInterestRate(0<||<1:");
double annualInterestRate = sc.nextDouble();
account2.setAnnualInterestRate(annualInterestRate);
System.out.print("Enter the password(Password can only " +
"be letters or numbers, " +
"and the length cannot be less than 6 and not more than 10.):");
String password = sc.next();
account2.setPassword(password);
ObjectOutputStream output1 = new ObjectOutputStream(new FileOutputStream("accounts.dat"));
output1.writeObject(account2);//将对象写入accounts.dat文件
break;
case 1:
System.out.println("The balance is:" + account2.getBalance());
break;
case 2:
try (ObjectInputStream read = new ObjectInputStream(new FileInputStream("accounts.dat"))) {
System.out.print("Enter the amount you want withdraw(The withdrawal amount " +
"must be an integral multiple of 100):");
int amount = sc.nextInt();
account2.withdraw(amount);
ObjectOutputStream output2 = new ObjectOutputStream(new FileOutputStream("accounts.dat"));
output2.writeObject(account2);
}
break;
case 3:try (ObjectInputStream read = new ObjectInputStream(new FileInputStream("accounts.dat"))) {
System.out.print("Enter the amount you want deposit(存款金额必须为100的整倍数):");
int amount = sc.nextInt();
account2.deposit(amount);
}
ObjectOutputStream output3 = new ObjectOutputStream(new FileOutputStream("accounts.dat"));
output3.writeObject(account2);//将对象写入accounts.dat文件
break;
case 4:
for (int i=0;i<account2.getTransaction().size();i++){
System.out.println(account2.getTransaction().get(i));
}
break;
case 5:
try (ObjectInputStream read = new ObjectInputStream(new FileInputStream("accounts.dat"))) {
System.out.print("Enter your old password" + ":");
String oldPassword = sc.next();
System.out.print("Enter your new password(新密码的长度为6到10位,且必须位字母或数字):");
String newPassword1 = sc.next();
System.out.println("Enter your old password again");
String newPassword2 = sc.next();
account2.changePassword(oldPassword, newPassword1,newPassword2);
}
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("accounts.dat"))) {
output.writeObject(account2);//将对象写入accounts.dat文件
}
break;
case 6:
System.exit(1);
break;
}
}
}
}
版本3:
请参照银行的ATM机界面,在Account2类的基础上开发一个GUI界面的ATM系统。要求界面应模拟小键盘,并且账户信息读、写于文件accounts.dat。文章来源地址https://www.toymoban.com/news/detail-479395.html
package version3;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.io.*;
public class ATMMachine3 extends Application{
Account2 account2=new Account2();
TextField textField1 = new TextField();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
TextField textField4 = new TextField();
TextField textField5 = new TextField();
TextField textField6 = new TextField();
TextField textField7 = new TextField();
TextField textField8 = new TextField();
//创建空文本域
Button bt1=new Button("exit");
@Override
public void start(Stage primaryStage){
GridPane pane = new GridPane();//创建一个GridPane类型的面板
pane.setAlignment(Pos.CENTER);//将面板对齐方式设置为中心对齐
pane.setPadding(new Insets(12.5, 13.5, 11.5, 14.5));//设置画板四边间距为12.5,13.5,11.5,14.5
pane.setHgap(6.0);
pane.setVgap(6.0);//指定面板中两个相邻结点的水平和垂直距离为6.0
Label MMenu = new Label("Main menu");//设置一个名为Main menu的面板
pane.add(MMenu, 0, 0);//将MMenue添加到列表中
Button createAccount = new Button("create account");//定义一个名为createAccount的按钮
createAccount.setOnAction(actionEvent -> {//为createAccount注册事件驱动处理器,
// 产生另一个GridPane类型的面板(输入余额,输入密码,输入年利率)
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(12.5, 13.5, 12.5, 14.5));//设置画板四边间距为12.5,13.5,11.5,14.5
pane1.setHgap(6.0);
pane1.setVgap(6.0);//指定面板中两个相邻结点的水平和垂直距离为6.0
Label EBalance=new Label("Enter balance:");
pane1.add(EBalance, 0, 0);//
pane1.add(textField1, 1, 0);
pane1.add(new Label("Enter annualInterestRate:"), 0, 1);
pane1.add(textField2, 1, 1);
pane1.add(new Label("Enter password:"), 0, 2);
pane1.add(textField3, 1, 2);
Button button = new Button("create account");//设置一个名为create account的按钮
button.setOnAction(actionEvent1 -> {//为create account注册事件驱动处理器,
// 功能为当将用户输入写入定义的account2对象,并将account2对象写入名为account.dat的文件之中
String balance = textField1.getText();
double balance1 = Double.parseDouble(balance);//将字符串转换成double类型
account2.setBalance(balance1);//将余额写入定义的account2对象中
String annualInterestRate = textField2.getText();
double annualInterestRate1 = Double.parseDouble(annualInterestRate);
account2.setAnnualInterestRate(annualInterestRate1);//将年利率写入定义的account2对象中
String password = textField3.getText();
account2.setPassword(password);//将密码写入定义的account2对象中
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("account.dat"));
output.writeObject(account2);//将对象写入accounts.dat文件
} catch (FileNotFoundException e) {
System.out.print("FileNotFoundException!");
} catch (IOException e) {
System.out.print("IOException!");
}
Stage stage1=new Stage();
Button bt2=new Button("login was successful\n");//如果注册成功则弹出
Scene scene=new Scene(bt2,400,300);
stage1.setTitle("success");
stage1.setScene(scene);
stage1.show();
}
);
pane1.add(button, 1, 3);
bt1.setOnAction(actionEvent1 -> System.exit(1));//为exit注册事件驱动处理器,exit的功能为终止程序
pane1.add(bt1, 1, 4);
GridPane.setHalignment(bt1, HPos.RIGHT);//水平向右对齐
GridPane.setHalignment(button, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setTitle("create account");
primaryStage1.setScene(scene);
primaryStage1.show();
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(new File("account.dat"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(objectInputStream.readObject());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
pane.add(createAccount, 0, 1);
Button checkBalance = new Button("check balance");//定义一个名为checkBalance的按钮
checkBalance.setOnAction(actionEvent -> {//为checkBalance注册事件驱动处理器,功能为产生一个拥有显示余额功能的的面板
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(10.5, 11.5, 12.5, 13.5));
pane1.setHgap(5.5);
pane1.setVgap(5.5);
pane1.add(new Label("your balance is:"), 0, 0);
Text text = new Text(account2.getBalance() + "");
pane1.add(text, 1, 0);
bt1.setOnAction(actionEvent1 -> System.exit(1));//为exit注册事件驱动处理器,exit的功能为终止程序
pane1.add(bt1, 1, 1);
GridPane.setHalignment(bt1, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setTitle("change password");
primaryStage1.setScene(scene);
primaryStage1.show();
});
pane.add(checkBalance, 0, 2);
Button withdraw = new Button("withdraw");//定义一个名为withdraw的按钮//为withdraw注册事件驱动处理器,功能为产生一个拥有可以输入取款金额功能的的面板
withdraw.setOnAction(actionEvent -> {
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(10.5, 11.5, 12.5, 13.5));
pane1.setHgap(5.5);
pane1.setVgap(5.5);
pane1.add(new Label("Enter withdrawal amount:"), 0, 0);
pane1.add(textField4, 1, 0);
Button button = new Button("Enter");//定义一个名为enter的按钮
button.setOnAction(actionEvent1 -> {//为enter注册事件驱动处理器,功能为将余额减去用户取出的金额中,并且transactions中在添加一个记录
try {
ObjectInputStream read = new ObjectInputStream(new FileInputStream("account.dat"));
} catch (IOException ex) {
System.out.print("IOException");
}
String amount = textField4.getText();
int amount1 = Integer.parseInt(amount);
account2.withdraw(amount1);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("account.dat"));
output.writeObject(account2);//将对象写入accounts.dat文件
} catch (FileNotFoundException ex) {
System.out.print("FileNotFoundException!");
} catch (IOException ex) {
System.out.print("IOException!");
}
});
pane1.add(button, 1, 1);
bt1.setOnAction(actionEvent1 -> System.exit(1));
pane1.add(bt1, 1, 2);
GridPane.setHalignment(button, HPos.RIGHT);
GridPane.setHalignment(bt1, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setScene(scene);
primaryStage1.setTitle("withdraw");
primaryStage1.show();
});
pane.add(withdraw, 0, 3);
Button deposit = new Button("deposit");//定义一个名为deposit的按钮
deposit.setOnAction(actionEvent -> {//为deposit注册事件驱动处理器,功能为产生一个拥有可以输入存款金额功能的的面板
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane1.setHgap(6.0);
pane1.setVgap(6.0);
pane1.add(new Label("Enter deposit amount:"), 0, 0);
pane1.add(textField5, 1, 0);
Button button = new Button("Enter");//定义一个名为enter的按钮
button.setOnAction(actionEvent1 -> {//为enter注册事件驱动处理器,功能为将用户输入的金额存入余额中,并且transactions中在添加一个记录
try {
ObjectInputStream read = new ObjectInputStream(new FileInputStream("account.dat"));
} catch (IOException ex) {
System.out.print("IOException!");
}
String amount = textField5.getText();
int amount1 = Integer.parseInt(amount);
account2.deposit(amount1);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("account.dat"));
output.writeObject(account2);//将对象写入accounts.dat文件
} catch (FileNotFoundException ex) {
System.out.print("FileNotFoundException!");
} catch (IOException ex) {
System.out.print("IOException!");
}
});//
pane1.add(button, 1, 1);
bt1.setOnAction(actionEvent1 -> System.exit(1));//为exit注册事件驱动处理器,功能为终止程序
pane1.add(bt1, 1, 2);
GridPane.setHalignment(button, HPos.RIGHT);
GridPane.setHalignment(bt1, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setScene(scene);
primaryStage1.setTitle("deposit");
primaryStage1.show();
});
pane.add(deposit, 0, 4);
Button detailOfTransaction = new Button("detail of transactions");
detailOfTransaction.setOnAction(actionEvent -> {
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(10.5, 11.5, 12.5, 13.5));
pane1.setHgap(5.5);
pane1.setVgap(5.5);
pane1.add(new Label("details of the transaction:"), 0, 0);
int count = 1;
for (int i=0;i<account2.getTransaction().size();i++){
Text text=new Text(account2.getTransaction().get(i));
pane1.add(text,0,i+1);
}
bt1.setOnAction(actionEvent1 -> System.exit(1));//为exit注册事件驱动处理器,功能为终止程序
pane1.add(bt1, 1, 1);
GridPane.setHalignment(bt1, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setTitle("change password");
primaryStage1.setScene(scene);
primaryStage1.show();
});
pane.add(detailOfTransaction, 0, 5);
Button changePassword = new Button("change password");//定义一个名为changepassword的按钮
pane.add(changePassword, 0, 6);
changePassword.setOnAction(actionEvent -> {//为changepassword注册事件驱动处理器,功能为产生一个具有旧密码输入和新密码输入功能的面板
GridPane pane1 = new GridPane();
pane1.setAlignment(Pos.CENTER);
pane1.setPadding(new Insets(10.5, 11.5, 12.5, 13.5));
pane1.setHgap(5.5);
pane1.setVgap(5.5);
pane1.add(new Label("old password:"), 0, 0);
pane1.add(textField6, 1, 0);
pane1.add(new Label("new password1:"), 0, 1);
pane1.add(textField7, 1, 1);
pane1.add(new Label("new password2:"), 0, 2);
pane1.add(textField8,1,2);
Button button = new Button("Enter");//定义一个名为enter的按钮
button.setOnAction(actionEvent1 -> {//为enter注册事件驱动处理器,功能为修改用户密码
try {
ObjectInputStream read = new ObjectInputStream(new FileInputStream("account.dat"));
} catch (IOException ex) {
System.out.print("IOException!");
}
String oldPassword = textField6.getText();
String newPassword1 = textField7.getText();
String newPassword2=textField8.getText();
account2.changePassword(oldPassword, newPassword1,newPassword2);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("account.dat"));
output.writeObject(account2);//将对象写入accounts.dat文件
} catch (FileNotFoundException ex) {
System.out.print("FileNotFoundException");
} catch (IOException ex) {
System.out.print("IOException!");
}
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(new File("account.dat"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(objectInputStream.readObject());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
/* try {
ObjectInputStream input=new ObjectInputStream(new FileInputStream("account.dat"));
input.readObject(new FileInputStream("account.dat"));
} catch (IOException e) {
e.printStackTrace();
}*/
});
pane1.add(button, 1, 2);
bt1.setOnAction(actionEvent1 -> System.exit(1));
pane1.add(bt1, 1, 3);
GridPane.setHalignment(bt1, HPos.RIGHT);
GridPane.setHalignment(button, HPos.RIGHT);
Scene scene = new Scene(pane1);
Stage primaryStage1 = new Stage();
primaryStage1.setTitle("change password");
primaryStage1.setScene(scene);
primaryStage1.show();
});
bt1.setOnAction(actionEvent -> System.exit(1));//为exit注册事件驱动处理器,exit的功能为终止程序
pane.add(bt1, 1, 7);
GridPane.setHalignment(bt1, HPos.RIGHT);
GridPane.setHalignment(MMenu, HPos.CENTER);
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("ATM Machine");
primaryStage.setScene(scene);
primaryStage.show();
}
}
到了这里,关于【面向对象程序设计】账户类(Java、JavaFX)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!