ATM系统——Java实现
package ATM; import java.util.ArrayList; public class Client { ArrayList<Customer> customerList=new ArrayList<Customer>(); Manager m=new Manager(976279,"000000"); public Client() { customerList.clear(); } public boolean addcustom(Customer e) { customerList.add(e); for(int i=0;i<customerList.size();i++) { if(customerList.get(i).getId()==e.getId()) { return true; } } return false; } public void deletecustom(int id) { for(int i=0;i<customerList.size();i++) { if(customerList.get(i).getId()==id) { customerList.remove(i); break; } } } public boolean checkcustom(int id,String password) { for(int i=0;i<customerList.size();i++) { // System.out.println(customlist.get(i).getId()+" "+customlist.get(i).getpassword()+id+password); if(customerList.get(i).getId()==id) { if(customerList.get(i).getpassword().equals(password)) { // System.out.println("right"); return true; } } } return false; } public void refreshcustomfile()throws Exception { java.io.File file=new java.io.File("custom.txt"); if(file.exists()) { System.out.println("File already exists."); file.delete(); refreshcustomfile(); } java.io.PrintWriter output=new java.io.PrintWriter(file); for(int i=0;i<customerList.size();i++) { output.println(customerList.get(i).getId()+" "+customerList.get(i).getpassword()+" "+customerList.get(i).getBalance() +customerList.get(i).getdateCreated()+customerList.get(i).getAnnualInterestRate()); } output.close(); } }
package ATM; import java.util.Date; public class Customer { private int id; private double balance; private String passWord; private double annualInterestRate = 0; private Date dateCreatedDate; Customer() { } Customer(int id,double balance,String passWord){ this.id = id; this.balance = balance; this.passWord =passWord; } void setpassword(String password) { this.passWord=password; } String getpassword() { return passWord; } int getId() { return id; } void setId(int id) { this.id = id; } double getBalance() { return balance; } void setBalance(double balance) { this.balance = balance; } double getAnnualInterestRate() { return annualInterestRate; } void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } Date getdateCreated() { return dateCreatedDate; } double getMonthlyInterestRate() { return annualInterestRate/12; } boolean withDraw(double count) { if(balance<count){ return false; } else { balance -= count; if (balance >= 0) return true; else return false; } } void deposit(double count) { balance += count; } }
package ATM; public class Manager { private int id; private String passWord; Manager(int nid,String npassword) { id=nid; passWord=npassword; } public int getId() { return id; } public String getPassWord() { return passWord; } public void setPassWord(String newpassword) { passWord=newpassword; } public void setCustomer(int newid,int balance,String newpassword) { @SuppressWarnings("unused") Customer a=new Customer(newid,balance,newpassword); } public void deleteCustom(Customer a) { a.setId(0); a.setBalance(0); a.setBalance(0); } }
package ATM; import javax.swing.JOptionPane; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.text.Text; import javafx.scene.image.ImageView; import javafx.scene.image.Image; import javafx.stage.Stage; import javafx.scene.layout.*; import javafx.geometry.*; import javafx.event.*; import javafx.scene.control.*; public class GUI extends Application { private ATMpane atm=new ATMpane(); public void start(Stage priamrystage) { Scene scene=new Scene(atm.getatm(),600,600); priamrystage.setTitle("ATM"); priamrystage.setScene(scene); priamrystage.show(); } class ATMpane extends GridPane { Client users=new Client(); int id; TextField t1= new TextField(); TextField t2= new TextField(); public ATMpane() { Text text1=new Text(" 欢迎使用ATM系统!"); this.add(text1, 0, 0); Image imageManager=new Image("image/admin.png"); Image imageCustomer=new Image("image/manageusers.png"); Button b8=new Button("",new ImageView(imageCustomer)); Button b9=new Button("",new ImageView(imageManager)); //this.add(new ImageView(imageCustomer), 0, 1); //this.add(new ImageView(imageManager), 0, 2); this.add(b8, 0, 1); this.add(b9, 0, 2); setAlignment(Pos.CENTER); setPadding(new Insets(50,50,50,50)); setHgap(25); setVgap(25); b8.setOnAction(e->managerStart()); b9.setOnAction(e->customerStart()); } public void start() { this.getChildren().clear(); Text text1=new Text(" 欢迎使用ATM系统!"); this.add(text1, 0, 0); Image imageManager=new Image("image/admin.png"); Image imageCustomer=new Image("image/manageusers.png"); Button b8=new Button("",new ImageView(imageCustomer)); Button b9=new Button("",new ImageView(imageManager)); this.add(b8, 0, 1); this.add(b9, 0, 2); setAlignment(Pos.CENTER); setPadding(new Insets(50,50,50,50)); setHgap(25); setVgap(25); b8.setOnAction(e->managerStart()); b9.setOnAction(e->customerStart()); } public void managerStart() { this.getChildren().clear(); Button b10=new Button("确定"); add(new Label("用户名"),0,0); add(t1, 1, 0); add(new Label("密码"), 0, 1); add(t2, 1, 1); add(b10,1,2); b10.setOnAction(e->{ id=Integer.parseInt(t1.getText()); String password=t2.getText(); if((users.m.getId()==id)&&(users.m.getPassWord().equals(password))) { managerUser(); } else JOptionPane.showMessageDialog(null, "用户名或密码输入错误"); }); } public void managerUser() { this.getChildren().clear(); setAlignment(Pos.CENTER); setPadding(new Insets(50,50,50,50)); setHgap(25); setVgap(25); Button b11=new Button("开户"); Button b12=new Button("销户"); Button b1=new Button("退出"); Image imageCreate=new Image("image/user-blue-add2.png"); Image imageDelete=new Image("image/user-blue-delete.png"); Image imageLogout=new Image("image/sign-out.png"); this.add(new ImageView(imageCreate), 0, 0); this.add(new ImageView(imageDelete), 0, 1); this.add(new ImageView(imageLogout), 0, 2); this.add(b11, 1, 0); this.add(b12, 1, 1); this.add(b1, 1, 2); b11.setOnAction(e->{ this.getChildren().clear(); setAlignment(Pos.CENTER); setPadding(new Insets(50,50,50,50)); setHgap(25); setVgap(25); t1.clear(); t2.clear(); add(new Label("用户名"),0,0); add(t1, 1, 0); add(new Label("密码"), 0, 1); add(t2, 1, 1); Button b13=new Button("确定"); add(b13,1,2); b13.setOnAction(y->reset()); }); b1.setOnAction(e->start()); b12.setOnAction(e->{ int id=Integer.parseInt(JOptionPane.showInputDialog("输入注销账号")); users.deletecustom(id); managerUser(); }); } public void customerStart() { this.getChildren().clear(); t1.clear(); t2.clear(); Button b2=new Button("确定"); Button b15=new Button("退出"); setAlignment(Pos.CENTER); setPadding(new Insets(50,50,50,50)); setHgap(25); GridPane.setHalignment(b15, HPos.CENTER); setVgap(25); add(new Label("用户名"),0,0); add(t1, 1, 0); add(new Label("密码"), 0, 1); add(t2, 1, 1); add(b15, 1, 2); add(b2,0,2); b15.setOnAction(e->start()); b2.setOnAction(e->{login();}); } public void login() { id=Integer.parseInt(t1.getText()); String password=t2.getText(); if(users.checkcustom(id, password)) { doing(); } else { JOptionPane.showMessageDialog(null, "用户名或密码输入错误"); } } public void reset() { id=Integer.parseInt(t1.getText()); String password=t2.getText(); double balance=Double.parseDouble(JOptionPane.showInputDialog("请输入金额")); Customer e=new Customer(id,balance,password); if( users.addcustom(e)) JOptionPane.showMessageDialog(null, "注册成功"); else JOptionPane.showMessageDialog(null, "注册失败"); managerUser(); } public ATMpane getatm() { return this; } public void doing() { this.getChildren().clear(); Button b3=new Button("余额查询"); Button b4=new Button("存钱"); Button b5=new Button("取钱"); Button b6=new Button("退出"); Image imageSubtract=new Image("image/033.png"); Image imageAdd=new Image("image/032.png"); Image imageSreach=new Image("image/035.png"); Image imageexit=new Image("image/049.png"); this.add(new ImageView(imageAdd), 0, 0); this.add(new ImageView(imageSubtract), 0, 1); this.add(new ImageView(imageSreach), 0, 2); this.add(new ImageView(imageexit), 0, 3); this.add(b3, 1, 2); this.add(b4, 1, 0); this.add(b5, 1, 1); this.add(b6, 1, 3); b3.setOnAction(e->{ for(int i=0;i<users.customerList.size();i++) { if(users.customerList.get(i).getId()==id) { JOptionPane.showMessageDialog(null, "余额为:"+users.customerList.get(i).getBalance()); } } }); b4.setOnAction(e->{ for(int i=0;i<users.customerList.size();i++) { if(users.customerList.get(i).getId()==id) { double balance=Double.parseDouble(JOptionPane.showInputDialog("请输入金额")); users.customerList.get(i).deposit(balance); try{ users.refreshcustomfile(); JOptionPane.showMessageDialog(null, "操作成功"); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "操作失败"); } } } }); b5.setOnAction(e->{ for(int i=0;i<users.customerList.size();i++) { if(users.customerList.get(i).getId()==id) { double balance=Double.parseDouble(JOptionPane.showInputDialog("请输入金额")); users.customerList.get(i).withDraw(balance); try{ users.refreshcustomfile(); JOptionPane.showMessageDialog(null, "操作成功"); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "操作失败"); } } } }); b6.setOnAction(e->customerStart()); } } public static void main(String[] args) { Application.launch(args); } }