Skip to content

Commit

Permalink
added a submit button and socketUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvir10029 committed Apr 9, 2021
1 parent b7aa98a commit cae3a88
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 1 deletion.
84 changes: 84 additions & 0 deletions Expedia/src/application/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.Date;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
Expand All @@ -30,6 +32,8 @@ public class Main extends Application
{
double total=0;
TextArea clock;
int numOfItems=0;

@Override
public void start(Stage primaryStage)
{
Expand Down Expand Up @@ -99,6 +103,85 @@ public void start(Stage primaryStage)
RadioButton RB2 = new RadioButton("Debit\t");
RB1.setToggleGroup(Payment);
RB2.setToggleGroup(Payment);


Button submitButton = new Button("SUBMIT");
submitButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override public void handle(ActionEvent e)
{
Platform.runLater(new Runnable()
{
public void run()
{
String rs=null;
socketUtils su = new socketUtils();

if (su.socketConnect() == true) //this always seems to be false for whatever reason
{
String strDouble = String.format("%.2f", total);
String msg = "Transaction>kiosk#001" + "," + numOfItems + "," + strDouble;
su.sendMessage(msg);
String ackOrNack = su.recvMessage();


msg = "quit";
su.sendMessage(msg);
rs = su.recvMessage();


//
// close the socket connection
//
su.closeSocket();

//
// write to transaction log
//
msg = "CLIENT : Transaction>kiosk#001" + "," + numOfItems + "," + strDouble;
fileIO trans = new fileIO();
trans.wrTransactionData(msg);


// initialize variables back to zero
total=0.0;
numOfItems=0;

ta.setText("");
ta2.setText("");

if (ackOrNack.startsWith("ACK") == true)
{
ta2.setText("Success! Message was received and processed by the Socket Server!");
}
else
{
ta2.setText("RECV : " + ackOrNack);
ta2.appendText(rs);
}
}
else
{
//
// write to transaction log
//
String strDouble = String.format("%.2f", total);
String msg = "CLIENT NETWORK ERROR : Transaction>kiosk#001" + "," + numOfItems + "," + strDouble;

fileIO trans = new fileIO();
trans.wrTransactionData(msg);


Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("--- Network Communications Error ---");
alert.setHeaderText("Unable to talk to Socket Server!");

alert.showAndWait();
}
}
});
}
});

//Image Buttons (Just going to be lying around for now)
Text reccLabel = new Text(" Recommendations: ");
Expand Down Expand Up @@ -227,6 +310,7 @@ public void start(Stage primaryStage)
gridPane.add(payGrid, 3, 3, 1, 1);
payGrid.add(RB1, 0, 1);
payGrid.add(RB2, 0, 2);
payGrid.add(submitButton, 0, 3);

payGrid.setVgap(10);
contact.setVgap(5);
Expand Down
28 changes: 28 additions & 0 deletions Expedia/src/application/fileIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


public class fileIO
{
Expand All @@ -30,5 +33,30 @@ public void writeToFile (String strSTA, String strEND)

outg.close();
}

public void wrTransactionData(String dataStr)
{
FileWriter fwg = null;
try
{
// open the file in append write mode
fwg = new FileWriter("transactionLog.txt", true);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

BufferedWriter bwg = new BufferedWriter(fwg);
PrintWriter outg = new PrintWriter(bwg);

String timeStamp = new SimpleDateFormat("MM-dd-yyyy HH.mm.ss").format(new Date());

outg.println(timeStamp + " : " + dataStr);

outg.close();
}

}

2 changes: 1 addition & 1 deletion Expedia/src/application/sockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void run()
// write to transaction log
//
fileIO transLog = new fileIO();
/////////////transLog.wrTransactionData("SERVER : " + clientString);
transLog.wrTransactionData("SERVER : " + clientString);


// update the status text area to show progress of program
Expand Down
122 changes: 122 additions & 0 deletions Expedia/src/application/socketUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package application;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

public class socketUtils
{
Socket clientSocket=null;
DataOutputStream outToServer=null;
BufferedReader inFromServer=null;

public boolean socketConnect()
{
String ipAddress, portString;
int portNumber;
boolean rc=false;

try
{
File file = new File("config.txt");
if (file.exists())
{
BufferedReader br = new BufferedReader(new FileReader("config.txt"));


ipAddress = br.readLine();
portString = br.readLine();
portNumber = Integer.parseInt(portString);
br.close();
}
else
{
ipAddress = "localhost"; //127.0.0.1 and "localhost" aren't working
portNumber = 3333;
}



clientSocket = new Socket(ipAddress, portNumber);

outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));

rc= true;
}
catch (ConnectException ex)
{
ex.printStackTrace();
}
catch (UnknownHostException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}

return rc;
}

public boolean sendMessage(String msg)
{
boolean rc=false;

try
{
outToServer.writeBytes(msg + "\r\n");
rc = true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return rc;
}

public String recvMessage()
{
String msg=null;

try
{
msg = inFromServer.readLine();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return msg;
}

public boolean closeSocket()
{
boolean rc=false;

try
{
clientSocket.close();
rc=true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return rc;
}
}
23 changes: 23 additions & 0 deletions Expedia/transactionLog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
04-08-2021 23.41.41 : SERVER : Transaction>kiosk#001,2,1.44
04-08-2021 23.41.42 : SERVER : quit
04-08-2021 23.54.25 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.25 : SERVER : quit
04-08-2021 23.54.26 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.27 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.28 : SERVER : quit
04-08-2021 23.54.28 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.28 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.29 : SERVER : quit
04-08-2021 23.54.29 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.29 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.30 : SERVER : quit
04-08-2021 23.54.30 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.30 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.31 : SERVER : quit
04-08-2021 23.54.31 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.31 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.32 : SERVER : quit
04-08-2021 23.54.32 : CLIENT : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.32 : SERVER : Transaction>kiosk#001,0,0.00
04-08-2021 23.54.33 : SERVER : quit
04-08-2021 23.54.33 : CLIENT : Transaction>kiosk#001,0,0.00

0 comments on commit cae3a88

Please sign in to comment.