Advertisement
yoyo106

ATM Java Project

Mar 13th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.74 KB | None | 0 0
  1. Main
  2. ==========================================================================================================================================
  3. package sample;
  4.  
  5. import javafx.application.Application;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.control.PasswordField;
  13. import javafx.scene.control.Label;
  14. import javafx.scene.layout.GridPane;
  15. import javafx.scene.layout.StackPane;
  16. import javafx.scene.text.Font;
  17. import javafx.stage.Stage;
  18.  
  19. public class Main<Textfield> extends Application  {
  20.  
  21.     //Logic Code Objects
  22.     Transactions trans = new Transactions();
  23.     History setTrue = new History();
  24.     Queue list = new Queue();
  25.  
  26.     Stage stage1,stage2;
  27.     Scene scene1,scene2;
  28.  
  29.     //scene 1 objects
  30.     Button loginButton= new Button();
  31.     Label cardNumberLabel;
  32.     PasswordField cardNumberField;
  33.  
  34.     //scene 2 objects
  35.     Button depositButton = new Button("Deposit");
  36.     Button withdrawButton = new Button("Withdraw");
  37.     Button balanceButton = new Button("Balance Inquiry");
  38.     Button nextButton = new Button("Next Transaction");
  39.     Button prevButton = new Button("Previous Transaction");
  40.     Button transactionsButton = new Button("Last 5 Transactions");
  41.     Button logoutButton = new Button("Logout");
  42.     Label label = new Label();
  43.  
  44.     public static void main(String[] args) {
  45.         launch(args);
  46.     }
  47.  
  48.     @Override
  49.     public void start(Stage primaryStage) throws Exception {
  50.         primaryStage.setTitle("Automated Teller Machine");
  51.  
  52.         LoginAuthentication authentication = new LoginAuthentication();
  53.         //AlertBox alertBox = new AlertBox();
  54.  
  55.         cardNumberLabel = new Label("Card Number: ");
  56.         cardNumberField = new PasswordField();
  57.         loginButton.setText("Login");
  58.         Label validationLabel = new Label();
  59.  
  60.         GridPane grid1,grid2;
  61.         grid2 = new GridPane();
  62.  
  63.         //scene 1 appearance
  64.         grid1 = new GridPane();
  65.         grid1.add(cardNumberLabel,0,0);
  66.         grid1.add(cardNumberField,1,0);
  67.         grid1.add(loginButton,1,1);
  68.         grid1.add(validationLabel,1,2);
  69.         grid1.setAlignment(Pos.CENTER);
  70.  
  71.         //scene 2 appearance
  72.         grid2.add(depositButton,0,0);
  73.         grid2.add(withdrawButton,0,1);
  74.         grid2.add(balanceButton,0,2);
  75.         grid2.add(logoutButton,2,3);
  76.         grid2.add(nextButton,1,0);
  77.         grid2.add(prevButton,1,1);
  78.         grid2.add(transactionsButton,2,0);
  79.         grid2.add(label,1,2);
  80.         grid2.setVgap(35);
  81.         grid2.setHgap(35);
  82.  
  83.         scene1 = new Scene(grid1,450,250);
  84.         scene2 = new Scene(grid2,450,250);
  85.  
  86.         loginButton.setOnAction(new EventHandler<ActionEvent>() {
  87.             @Override
  88.             public void handle(ActionEvent event) {
  89.                 String cardNumber = cardNumberField.getText();
  90.                 boolean valid = authentication.validate(cardNumber);
  91.                 if (valid)
  92.                     primaryStage.setScene(scene2);
  93.                 else
  94.                     AlertBox.displayLoginError();
  95.             }
  96.         });
  97.  
  98.         logoutButton.setOnAction(e->{
  99.             primaryStage.setScene(scene1);
  100.             AlertBox.displayExit();
  101.         });
  102.  
  103.         depositButton.setOnAction(e->{
  104.         double value = AlertBox.displayNumbers();
  105.         trans.setDeposit(value);
  106.         setTrue.setDeposit(true);
  107.         list.push(value);
  108.         label.setText("Successful operation.");
  109.         setTrue.setDeposit(false);
  110.         });
  111.  
  112.         withdrawButton.setOnAction(e->{
  113.             double value = Validation.checkValidity();
  114.             trans.setWithdraw(value);
  115.             setTrue.setWithdraw(true);
  116.             list.push(value);
  117.             label.setText("Successful operation.");
  118.             setTrue.setWithdraw(false);
  119.         });
  120.  
  121.         balanceButton.setOnAction(e-> label.setText("Current Balance: "+trans.getBalance()+"$"));
  122.  
  123.         nextButton.setOnAction(e->{
  124.             if(((list.getPtr()%5)+1)!=4){
  125.                 if(list.queue[(list.getPtr()+2)%5].equals(" ") || list.queue[(list.getPtr()+2)%5].isEmpty()){
  126.                     label.setText("Current balance: "+Transactions.getBalance()+"$");
  127.                     nextButton.setDisable(true);
  128.                 }
  129.  
  130.                 else
  131.                     label.setText(list.showNext());
  132.             }
  133.             else
  134.                 AlertBox.displayNextPrevError();
  135.         });
  136.  
  137.         prevButton.setOnAction(e->{
  138.             if(((list.getPtr()%5)+1) ==0)
  139.                 AlertBox.displayNextPrevError();
  140.             else{
  141.                 label.setText(list.showPrev());
  142.                 nextButton.setDisable(false);
  143.             }
  144.  
  145.         });
  146.  
  147.         transactionsButton.setOnAction(e-> AlertBox.displayTransactions());
  148.  
  149.         primaryStage.setScene(scene1);
  150.         primaryStage.show();
  151.     }
  152. }
  153. //TODO SET VALUE OF DEPOSIT AND WITHDRAW TO TRUE
  154. ==========================================================================================================================================
  155.  
  156. Transactions
  157. ==========================================================================================================================================
  158. package sample;
  159.  
  160. public class Transactions
  161. {
  162.     private static double deposit,withdraw,balance=0;
  163.  
  164.     public void setDeposit(double deposit) { balance = balance + deposit; }
  165.  
  166.     public void setWithdraw(double withdraw)
  167.     {
  168.         balance = balance - withdraw;
  169.     }
  170.  
  171.     public static double getBalance()
  172.     {
  173.         return balance;
  174.     }
  175. }
  176.  
  177. ==========================================================================================================================================
  178.  
  179. Queue
  180. ==========================================================================================================================================
  181. package sample;
  182.  
  183. public class Queue
  184. {
  185.     History lastTransaction = new History();
  186.     static String queue[] = new String[]{" "," "," "," "," "};
  187.  
  188.     public int getPtr() {
  189.         return ptr-1;
  190.     }
  191.  
  192.     int ptr=0;
  193.     int rear=0; // to insert
  194.     int size=0;
  195.     int index=-1;
  196.     public void push(double data)
  197.     {
  198.         if(lastTransaction.isWithdraw())
  199.         {
  200.             queue[rear%5] = "  Withdraw: "+data+"$";
  201.             rear = (rear + 1) % 5;
  202.             size++;
  203.             ++ptr;
  204.             ++index;
  205.         }
  206.         else if (lastTransaction.isDeposit())
  207.         {
  208.             queue[rear%5] ="  Deposit: "+data+"$";
  209.             rear++;
  210.             size++;
  211.             ++ptr;
  212.             ++index;
  213.         }
  214.     }
  215.     public int getSize()
  216.     {
  217.         if (size>5)
  218.             size=5;
  219.         return size;
  220.     }
  221.  
  222.     public String showPrev(){ return queue[(--ptr)%5]; }
  223.     public String showNext(){ return queue[(++ptr)%5]; }
  224.  
  225. }
  226.  
  227. ==========================================================================================================================================
  228.  
  229. History
  230. ==========================================================================================================================================
  231. package sample;
  232.  
  233. public class History
  234. {
  235.     private static boolean deposit = false;
  236.     private static boolean withdraw = false;
  237.     public static boolean isDeposit()
  238.     {
  239.         return deposit;
  240.     }
  241.  
  242.     public static void setDeposit(boolean d)
  243.     { deposit = d; }
  244.  
  245.     public static boolean isWithdraw()
  246.     { return withdraw; }
  247.  
  248.     public static void setWithdraw(boolean w)
  249.     { withdraw = w; }
  250. }
  251.  
  252. ==========================================================================================================================================
  253.  
  254. LoginAuthentication
  255. ==========================================================================================================================================
  256. package sample;
  257.  
  258. import java.util.HashMap;
  259.  
  260. public class LoginAuthentication {
  261.  
  262.     public boolean validate(String password){
  263.         boolean validationResult=false;
  264.         if (password.equals("1111"))
  265.             validationResult=true;
  266.         return validationResult;
  267.     }
  268. }
  269.  
  270. ==========================================================================================================================================
  271.  
  272. AlertBox
  273. ==========================================================================================================================================
  274. package sample;
  275.  
  276. import javafx.scene.paint.Color;
  277. import javafx.scene.text.Font;
  278. import javafx.stage.*;
  279. import javafx.scene.*;
  280. import javafx.scene.layout.*;
  281. import javafx.scene.control.*;
  282. import javafx.geometry.*;
  283. import javafx.event.ActionEvent;
  284.  
  285.  
  286.  
  287. public class AlertBox {
  288.  
  289.     public static void displayLoginError(){
  290.         Stage window1 = new Stage();
  291.  
  292.         window1.setTitle("Error!");
  293.         window1.initModality(Modality.APPLICATION_MODAL);
  294.         window1.setMinWidth(300);
  295.         window1.setMinHeight(200);
  296.  
  297.         Label label = new Label();
  298.         label.setText("Incorrect entry!\nRetry.");
  299.         label.setFont(new Font("Verdana",15));
  300.         label.setTextFill(Color.web("#b22222"));
  301.         Button closeButton = new Button("Close");
  302.         closeButton.setOnAction(e -> window1.close());
  303.  
  304.         VBox layout = new VBox(10);
  305.         layout.getChildren().addAll(label,closeButton);
  306.         layout.setAlignment(Pos.CENTER);
  307.  
  308.         Scene scene = new Scene(layout);
  309.         window1.setScene(scene);
  310.         window1.showAndWait();
  311.     }
  312.  
  313.     public static void displayNextPrevError(){
  314.         Stage window1 = new Stage();
  315.  
  316.         window1.setTitle("Error!");
  317.         window1.initModality(Modality.APPLICATION_MODAL);
  318.         window1.setMinWidth(300);
  319.         window1.setMinHeight(200);
  320.  
  321.         Label label = new Label();
  322.         label.setText("No further records!");
  323.         label.setFont(new Font("Verdana",15));
  324.         label.setTextFill(Color.web("#b22222"));
  325.         Button closeButton = new Button("Close");
  326.         closeButton.setOnAction(e -> window1.close());
  327.  
  328.         VBox layout = new VBox(10);
  329.         layout.getChildren().addAll(label,closeButton);
  330.         layout.setAlignment(Pos.CENTER);
  331.  
  332.         Scene scene = new Scene(layout);
  333.         window1.setScene(scene);
  334.         window1.showAndWait();
  335.     }
  336.  
  337.     public static double displayNumbers() {
  338.         Stage window = new Stage();
  339.         GridPane grid = new GridPane();
  340.  
  341.         window.initModality(Modality.APPLICATION_MODAL);
  342.         window.setMinHeight(200);
  343.         window.setMinWidth(300);
  344.  
  345.         window.setTitle("Transactions");
  346.  
  347.         TextField textField = new TextField();
  348.  
  349.         Button one = new Button("1");
  350.         Button two = new Button("2");
  351.         Button three = new Button("3");
  352.         Button four = new Button("4");
  353.         Button five = new Button("5");
  354.         Button six = new Button("6");
  355.         Button seven = new Button("7");
  356.         Button eight = new Button("8");
  357.         Button nine = new Button("9");
  358.         Button zero = new Button("0");
  359.         Button enterButton = new Button("Enter");
  360.         Label label = new Label("Enter value: ");
  361.  
  362.         grid.add(one, 0, 0);
  363.         grid.add(two, 1, 0);
  364.         grid.add(three, 2, 0);
  365.         grid.add(four, 0, 1);
  366.         grid.add(five, 1, 1);
  367.         grid.add(six, 2, 1);
  368.         grid.add(seven, 0, 2);
  369.         grid.add(eight, 1, 2);
  370.         grid.add(nine, 2, 2);
  371.         grid.add(zero, 1, 3);
  372.         grid.add(textField,3,2);
  373.         grid.add(enterButton, 3, 3);
  374.         grid.add(label,3,1);
  375.         grid.setVgap(10);
  376.         grid.setHgap(10);
  377.  
  378.         one.setOnAction(e-> textField.setText(textField.getText()+"1"));
  379.         two.setOnAction(e-> textField.setText(textField.getText()+"2"));
  380.         three.setOnAction(e-> textField.setText(textField.getText()+"3"));
  381.         four.setOnAction(e-> textField.setText(textField.getText()+"4"));
  382.         five.setOnAction(e-> textField.setText(textField.getText()+"5"));
  383.         six.setOnAction(e-> textField.setText(textField.getText()+"6"));
  384.         seven.setOnAction(e-> textField.setText(textField.getText()+"7"));
  385.         eight.setOnAction(e-> textField.setText(textField.getText()+"8"));
  386.         nine.setOnAction(e-> textField.setText(textField.getText()+"9"));
  387.         zero.setOnAction(e-> textField.setText(textField.getText()+"0"));
  388.  
  389.         Scene scene = new Scene(grid);
  390.         window.setScene(scene);
  391.         enterButton.setOnAction(e -> window.close());
  392.         window.showAndWait();
  393.  
  394.         String value = textField.getText();
  395.         if(value == null || value.isEmpty()) { value = "0"; }
  396.         return Double.valueOf(value);
  397.     }
  398.  
  399.     public static void displayExit(){
  400.         Stage window1 = new Stage();
  401.  
  402.         window1.setTitle("Thank you!");
  403.         window1.initModality(Modality.APPLICATION_MODAL);
  404.         window1.setMinWidth(300);
  405.         window1.setMinHeight(200);
  406.  
  407.         Label label = new Label();
  408.         label.setText("Thank you for using our services.\n Please do not forget your card!");
  409.         label.setFont(new Font("Verdana",15));
  410.         label.setTextFill(Color.web("#0000ff"));
  411.         Button closeButton = new Button("Close");
  412.         closeButton.setOnAction(e -> window1.close());
  413.  
  414.         VBox layout = new VBox(10);
  415.         layout.getChildren().addAll(label,closeButton);
  416.         layout.setAlignment(Pos.CENTER);
  417.  
  418.         Scene scene = new Scene(layout);
  419.         window1.setScene(scene);
  420.         window1.showAndWait();
  421.     }
  422.  
  423.     public static void displayTransactions(){
  424.         Queue lastTransactions = new Queue();
  425.  
  426.         Button closeButton = new Button("Close");
  427.         Label label0 = new Label(lastTransactions.queue[0]);
  428.         Label label1 = new Label(lastTransactions.queue[1]);
  429.         Label label2 = new Label(lastTransactions.queue[2]);
  430.         Label label3 = new Label(lastTransactions.queue[3]);
  431.         Label label4 = new Label(lastTransactions.queue[4]);
  432.  
  433.         label0.setVisible(true);
  434.         label1.setVisible(true);
  435.         label2.setVisible(true);
  436.         label3.setVisible(true);
  437.         label4.setVisible(true);
  438.  
  439.         Stage stage = new Stage();
  440.  
  441.         stage.setTitle("Last Five Transaction");
  442.         stage.initModality(Modality.APPLICATION_MODAL);
  443.         stage.setMinWidth(320);
  444.         stage.setMinHeight(250);
  445.  
  446.         GridPane grid = new GridPane();
  447.  
  448.         grid.add(label0,0,0);
  449.         grid.add(label1,0,1);
  450.         grid.add(label2,0,2);
  451.         grid.add(label3,0,3);
  452.         grid.add(label4,0,4);
  453.         grid.add(closeButton,1,5);
  454.         grid.setHgap(15);
  455.         grid.setVgap(15);
  456.  
  457.         closeButton.setOnAction(e -> stage.close());
  458.  
  459.  
  460.  
  461.         Scene scene = new Scene(grid);
  462.         stage.setScene(scene);
  463.         stage.showAndWait();
  464.     }
  465.  
  466.     public static void displayNegativeBalanceChecker(){
  467.         Stage window1 = new Stage();
  468.  
  469.         window1.setTitle("Error!");
  470.         window1.initModality(Modality.APPLICATION_MODAL);
  471.         window1.setMinWidth(300);
  472.         window1.setMinHeight(200);
  473.  
  474.         Label label = new Label();
  475.         label.setText("Insufficient amount!\nCurrent balance: "+ String.valueOf(Transactions.getBalance()));
  476.         label.setFont(new Font("Verdana",15));
  477.         label.setTextFill(Color.web("#b22222"));
  478.         Button closeButton = new Button("Close");
  479.         closeButton.setOnAction(e -> window1.close());
  480.  
  481.         VBox layout = new VBox(10);
  482.         layout.getChildren().addAll(label,closeButton);
  483.         layout.setAlignment(Pos.CENTER);
  484.  
  485.         Scene scene = new Scene(layout);
  486.         window1.setScene(scene);
  487.         window1.showAndWait();
  488.     }
  489. }
  490.  
  491. ==========================================================================================================================================
  492.  
  493. Validation
  494. ==========================================================================================================================================
  495. package sample;
  496.  
  497. import javafx.scene.control.Alert;
  498.  
  499. public class Validation {
  500.     public static boolean isFlag() {
  501.         return flag;
  502.     }
  503.  
  504.     public static void setFlag(boolean flag) {
  505.         Validation.flag = flag;
  506.     }
  507.  
  508.     private static boolean flag = false;
  509.     private static boolean disableFlag = false;
  510.  
  511.     static double value;
  512.  
  513.     public static double checkValidity(){
  514.         do{
  515.             value = AlertBox.displayNumbers();
  516.             if(value<Transactions.getBalance()||value==0)
  517.                 setFlag(true);
  518.             else
  519.                 AlertBox.displayNegativeBalanceChecker();
  520.         }while(!isFlag());
  521.         setFlag(false);
  522.         return value;
  523.     }
  524.  
  525. }
  526.  
  527. ==========================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement