id
stringlengths
36
36
text
stringlengths
1
1.25M
c09b0b03-14fc-4519-9689-6a050947a4a1
public ColorEnum getColor() { return color; }
22828c45-f68b-44a5-8f4e-7333f77b4822
public boolean PieceCanMove(Piece piece, Move move, Board board) { boolean r = false; //If we're dealing with a knight if (piece.getPiece() == PieceEnum.KNIGHT) { //If we're trying to capture if (board.getBoard()[move.getB().getRow()][move.getB().getCollumn()] != null) { //We can only capture enemy pieces if (board.getBoard()[move.getB().getRow()][move.getB().getCollumn()].getColor() != piece.getColor()) { //If this is one of the eight positions the knight can move to if(validateKnightMove(move)) { r = true; } else { System.out.println("Sorry, you cannot move your knight there"); } } } else { //If this is one of the eight positions the knight can move to if(validateKnightMove(move)) { r = true; } else { System.out.println("Sorry, you cannot move your knight there"); } } } else //We have a pawn { //If we're not trying to capture if (move.getA().getCollumn() == move.getB().getCollumn()) { if (piece.getColor() == ColorEnum.WHITE) { //White pawns can only move up if (move.getA().getRow() == move.getB().getRow() + 1 && board.getBoard()[move.getB().getRow()][move.getB().getCollumn()] == null) { r = true; } else { System.out.println("Sorry, you cannot move your pawn there"); } } else //The piece is black { //Black pawns can only move down if (move.getA().getRow() == move.getB().getRow() - 1 && board.getBoard()[move.getB().getRow()][move.getB().getCollumn()] == null) { r = true; } else { System.out.println("Sorry, you cannot move your pawn there"); } } } else //We're trying to capture { //Are we really trying to capture or is the destination empty? if (board.getBoard()[move.getB().getRow()][move.getB().getCollumn()] != null) { //We can only capture enemy pieces if (board.getBoard()[move.getB().getRow()][move.getB().getCollumn()].getColor() != piece.getColor()) { //We can only capture diagonally if (move.getA().getCollumn() == move.getB().getCollumn() + 1 || move.getA().getCollumn() == move.getB().getCollumn() - 1) { if (piece.getColor() == ColorEnum.WHITE) { //White pawns can only move up if (move.getA().getRow() == move.getB().getRow() + 1) { r = true; } else { System.out.println("Sorry, you cannot move your pawn there"); } } else //The piece is black { //Black pawns can only move down if (move.getA().getRow() == move.getB().getRow() - 1) { r = true; } else { System.out.println("Sorry, you cannot move your pawn there"); } } } else { System.out.println("Sorry, you cannot move your pawn there"); } } else { System.out.println("Sorry, you cannot move your pawn there"); } } else { System.out.println("Sorry, you cannot move your pawn there"); } } } return r; }
f6a9807c-ebeb-447e-a6e0-42cc03c01ca0
public boolean validateKnightMove(Move move) { return (move.getB().getCollumn() == move.getA().getCollumn() - 2 && (move.getB().getRow() == move.getA().getRow() + 1 || move.getB().getRow() == move.getA().getRow() - 1)) || (move.getB().getCollumn() == move.getA().getCollumn() + 2 && (move.getB().getRow() == move.getA().getRow() + 1 || move.getB().getRow() == move.getA().getRow() - 1)) || (move.getB().getCollumn() == move.getA().getCollumn() - 1 && (move.getB().getRow() == move.getA().getRow() + 2 || move.getB().getRow() == move.getA().getRow() - 2)) || (move.getB().getCollumn() == move.getA().getCollumn() + 1 && (move.getB().getRow() == move.getA().getRow() + 2 || move.getB().getRow() == move.getA().getRow() - 2)); }
9fe33671-255f-4d73-8a50-b171e911d3a4
ColorEnum(String colorStr) { this.colorStr = colorStr; }
1bb818a9-b814-4405-a0d4-acd9ac9f1d86
public String toString() { return colorStr; }
5823441e-8c59-4a55-9483-332a93dc0f64
public static void main(String[] args) throws ClassNotFoundException { Game game1 = new Game(); Move currMove; Move enemyMove; System.out.println("Welcome to Knight's Watch!!!"); System.out.println("Waiting for another player to connect..."); String host = args.length > 0 ? args[0] : DEFAULT_HOST; int port = args.length > 1 ? Integer.parseInt(args[1]) : DEFAULT_PORT; try { // connect to the server Socket socket = new Socket(host, port); // create a scanner & printstream out of the socket's I/O streams ObjectInputStream socketIn = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream socketOut = new ObjectOutputStream(socket.getOutputStream()); socketOut.flush(); String playerNum = (String) socketIn.readObject(); System.out.println("You are player number : " + playerNum); // prepare STDIN for reading String oppositePlayerNum = (playerNum.equals("1"))?"2":"1"; System.out.println("You are player" + playerNum + ", your color is: " + ((playerNum.equals("1"))?"white":"black")); game1.setCurrPlayer((playerNum.equals("1"))?ColorEnum.WHITE:ColorEnum.BLACK); System.out.println(game1.getBoard().toString()); if (playerNum.equals("2")) { System.out.println("Waiting for player" + oppositePlayerNum + " to make his move..."); enemyMove = (Move) socketIn.readObject(); // print to STDOUT System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); System.out.println("Enemy has made this move: From " + enemyMove.getA().toString() + " to " + enemyMove.getB().toString()); game1.getBoard().movePiece(enemyMove); System.out.println(game1.getBoard().toString()); } while(game1.getStatus() == GameStatus.CONTINUE) { //Get the move currMove = game1.makeMoveForCurrentPlayer(); game1.getBoard().movePiece(currMove); currMove.setResultingGameStatus(game1.getStatus()); // send the move socketOut.writeObject(currMove); socketOut.flush(); System.out.println(game1.getBoard().toString()); if (game1.getStatus() == GameStatus.CONTINUE) { // receive the enemy move System.out.println("Waiting for player" + oppositePlayerNum + " to make his move..."); enemyMove = (Move) socketIn.readObject(); System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); System.out.println("Enemy has made this move: From " + enemyMove.getA().toString() + " to " + enemyMove.getB().toString()); game1.getBoard().movePiece(enemyMove); System.out.println(game1.getBoard().toString()); } else { break; } } System.out.println("The Game is finished!"); switch (game1.getStatus()) { case WHITEWINS: System.out.println("White has won!"); break; case BLACKWINS: System.out.println("Black has won!"); break; case DRAW: System.out.println("it was a draw!"); break; } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
70854a8e-fd68-4a93-8c30-eb09756f686a
PieceEnum(String pieceStr) { this.pieceStr = pieceStr; }
564b762b-ba6e-46f8-9fdb-b47e926d7cbc
public String toString() { return pieceStr; }
222ce3e3-581a-4516-a9f3-42270cee2e52
public int getRow() { return row; }
dc52b4db-132a-46f7-8191-12527f1fc272
public void setRow(int row) { this.row = row; }
29905cab-d72e-4745-9280-817f8b7f4c65
public int getCollumn() { return collumn; }
c769a660-15e8-4bad-b0b3-5c2af8741392
public void setCollumn(int collumn) { this.collumn = collumn; }
f1e4b5a5-9ea5-48cf-a843-8afe022efc72
public BoardPosition(int row, int collumn) { this.row = row; this.collumn = collumn; }
218019d5-b468-4620-841d-39b2d9f744fc
public boolean isOutOfBounds() { return row < 0 || row > 6 || collumn < 0 || collumn > 6; }
e66cccc1-d6fa-461e-b7d7-39c3928c1108
public String toString() { ArrayList<String> colNames = new ArrayList<String>(); colNames.add("a"); colNames.add("b"); colNames.add("c"); colNames.add("d"); colNames.add("e"); colNames.add("f"); String r = ""; r += 6 - row + colNames.get(collumn); return r; }
87471279-f353-4291-9cbf-b856390a22e2
public Piece(ColorEnum color, PieceEnum piece) { this.color = color; this.piece = piece; }
b72fc693-bc67-452c-9f25-79f7648f409b
public String toString() { String output; output = color.toString() + piece.toString(); return output; }
2ddc88b6-33e8-4ff4-9fea-75b339bcc9d1
public ColorEnum getColor() { return color; }
7c78acf6-1036-40d0-a44a-65a4bac13a27
public PieceEnum getPiece() { return piece; }
c6d13ec4-80ef-4a7f-9d41-553790845ad5
public void setCurrPlayer(ColorEnum color) { if (color == ColorEnum.WHITE) { this.currPlayer = player1; } else { this.currPlayer = player2; } }
9601cfb9-acf2-4aa2-a4af-cf7b34673b38
public Player getPlayer1() { return player1; }
486653fe-d6d6-4d1c-b954-8aa1ee6f6e92
public void setPlayer1(Player player1) { this.player1 = player1; }
71a3a9b3-5b93-4f72-93de-badc4fe400b6
public Player getPlayer2() { return player2; }
9ed1ee4d-30f2-47de-8424-1a90315488e8
public void setPlayer2(Player player2) { this.player2 = player2; }
82f32663-e3ed-4ae1-91cc-7d9a56ae0731
public Game() { board = new Board(); player1 = new Player(ColorEnum.WHITE, board); player2 = new Player(ColorEnum.BLACK, board); }
1056661f-b78e-40b1-8710-568b4cf41e99
public Board getBoard() { return board; }
b4db8bdc-7e29-4e89-864a-cfffa136efd3
public Move makeMoveForCurrentPlayer() { return currPlayer.makeMove(); }
72f2dfd1-01e8-461b-b56e-d8e49b91d050
public GameStatus getStatus() { GameStatus r = GameStatus.CONTINUE; //Have any pawns reached the enemy wall? for (int i = 0; i < 6; i++) { //Check the White pawns if (board.getBoard()[0][i] != null) { if (board.getBoard()[0][i].getPiece() == PieceEnum.PAWN) { r = GameStatus.WHITEWINS; break; } } //And the Black pawns if (board.getBoard()[5][i] != null) { if (board.getBoard()[5][i].getPiece() == PieceEnum.PAWN) { r = GameStatus.BLACKWINS; break; } } } //Do both players still have pawns? int numDeadBlackPawns = 0; int numDeadWhitePawns = 0; for (int i = 0; i < board.getGraveyard().size(); i++) { if (board.getGraveyard().get(i).getColor() == ColorEnum.BLACK && board.getGraveyard().get(i).getPiece() == PieceEnum.PAWN) { numDeadBlackPawns++; } else { if (board.getGraveyard().get(i).getColor() == ColorEnum.WHITE && board.getGraveyard().get(i).getPiece() == PieceEnum.PAWN) { numDeadWhitePawns++; } } } if (numDeadWhitePawns == 6 && numDeadBlackPawns == 6) { r = GameStatus.DRAW; } int numDeadBlackPieces = 0; int numDeadWhitePieces = 0; //Do both players have at least one piece? for (int i = 0; i < board.getGraveyard().size(); i++) { if (board.getGraveyard().get(i).getColor() == ColorEnum.BLACK) { numDeadBlackPieces++; } else { if (board.getGraveyard().get(i).getColor() == ColorEnum.WHITE) { numDeadWhitePieces++; } } } if (numDeadWhitePieces == 8) { r = GameStatus.BLACKWINS; } else { if (numDeadBlackPieces == 8) { r = GameStatus.WHITEWINS; } } return r; }
d4f3d807-d9e1-431d-a2f6-9faf61309fe7
public ChatWindow() { super(); initGUI(); }
6069acda-d3c1-4463-a1c8-84fa22789b38
public ChatWindow(Socket s, String l) { socket = s; login = l; this.setLocationRelativeTo(null); this.setVisible(true); this.setTitle("Java Instant Chat - Server"); initGUI(); }
67bd0dff-7e43-4429-81d6-bcf9ff1f07f6
private void initGUI() { try { out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); sdf = new SimpleDateFormat("HH:mm"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jPanel1 = new JPanel(); getContentPane().add(jPanel1, BorderLayout.NORTH); jPanel1.setPreferredSize(new java.awt.Dimension(484, 474)); { jTextPane1 = new JTextPane(); jPanel1.add(jTextPane1); jTextPane1.setPreferredSize(new java.awt.Dimension(483, 473)); jTextPane1.setEditable(false); } } { jPanel2 = new JPanel(); GridBagLayout jPanel2Layout = new GridBagLayout(); jPanel2Layout.columnWidths = new int[] {48, 250, 48, 7}; jPanel2Layout.rowHeights = new int[] {7}; jPanel2Layout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1}; jPanel2Layout.rowWeights = new double[] {0.1}; getContentPane().add(jPanel2, BorderLayout.SOUTH); jPanel2.setLayout(jPanel2Layout); jPanel2.setPreferredSize(new java.awt.Dimension(484, 54)); { jTextField1 = new JTextField(); jPanel2.add(jTextField1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } { jButton1 = new JButton(); jPanel2.add(jButton1, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jButton1.setText("Send"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } } pack(); setSize(500, 600); //Infinite loop to listen for new messages coming in through the socket. while (true){ message = in.readLine(); loginSender = in.readLine(); cal = Calendar.getInstance(); jTextPane1.setText(jTextPane1.getText()+"("+sdf.format(cal.getTime())+") "+loginSender+" says: "+message+"\n"); System.out.println("caca"); } } catch (Exception e) { e.printStackTrace(); } }
7ca39d8e-9a4f-4415-bab8-9db290b255fb
public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); }
f8043dea-eafe-4223-b29a-00c306df519c
private void jButton1ActionPerformed(ActionEvent evt) { if (jTextField1.getText().isEmpty()) { JOptionPane.showMessageDialog(this, "Please type in a message."); } else { out.println(jTextField1.getText()); out.flush(); out.println(login); out.flush(); cal = Calendar.getInstance(); jTextPane1.setText(jTextPane1.getText()+"("+sdf.format(cal.getTime())+") "+login+" says: "+jTextField1.getText()+"\n"); jTextField1.setText(""); } }
7047df59-bb86-4614-8b08-9fa1b5995907
public Accept(ServerSocket ss){ socketserver = ss; }
a9305a4d-ae99-4414-938b-ac4c9fba0676
public void run() { try { while(true){ socket = socketserver.accept(); t1 = new Thread(new Auth(socket)); t1.start(); } } catch (IOException e) { System.err.println("Erreur serveur"); } }
4878b4c4-73c8-496f-84da-136f94b107d2
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Start inst = new Start(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setTitle("Java Instant Chat Server"); } }); }
4a04f84d-f8af-451a-b3ef-d9cd6eb6f7f8
public void run() { Start inst = new Start(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setTitle("Java Instant Chat Server"); }
cb9cf549-5831-44b6-b6ed-d49ff2e2326f
public Start() { super(); initGUI(); }
468affc8-fcfb-48f9-b149-438deb047403
private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jPanel2 = new JPanel(); getContentPane().add(jPanel2, BorderLayout.SOUTH); jPanel2.setPreferredSize(new java.awt.Dimension(384, 66)); { jButton1 = new JButton(); jPanel2.add(jButton1); jButton1.setText("Start Server"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } } { jPanel1 = new JPanel(); getContentPane().add(jPanel1, BorderLayout.CENTER); jPanel1.setPreferredSize(new java.awt.Dimension(384, 130)); { jLabel1 = new JLabel(); jPanel1.add(jLabel1); jLabel1.setText("Welcome to the server for JavaInstantChat"); } } { jPanel3 = new JPanel(); getContentPane().add(jPanel3, BorderLayout.NORTH); jPanel3.setPreferredSize(new java.awt.Dimension(384, 42)); } pack(); setSize(400, 300); } catch (Exception e) { e.printStackTrace(); } }
e74d8e0d-196b-423f-959f-78cd320dc67a
public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); }
37d53b44-115a-4fad-a4fa-aafa0677492b
private void jButton1ActionPerformed(ActionEvent evt) { try { ss = new ServerSocket(2009); System.out.println("Server started on "+ss.getLocalPort()+". Waiting for clients to connect."); t = new Thread(new Accept(ss)); t.start(); this.dispose(); } catch (IOException e) { System.err.println("The port "+ss.getLocalPort()+" is already in use."); } }
ee94a7a7-d77a-4dd3-ac64-ea6db5e65554
public Statement Connect() { try { Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost:5432/JavaInstantChat"; String user = "postgres"; String passwd = "Supinf0"; Connection conn = DriverManager.getConnection(url, user, passwd); state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); return state; } catch (Exception e) { System.out.println("Error connecting to the SQL Server."); } return state; }
99f6c6a7-5e53-454f-b4fb-d94ad4e9c4e1
public Auth(Socket s){ socket = s; }
b5cd6bd3-b8b8-46ed-8956-bd648641b780
public void run() { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); login = in.readLine(); password = in.readLine(); DbConnect DBC = new DbConnect(); state = DBC.Connect(); ResultSet result = state.executeQuery("SELECT login FROM users WHERE login = '"+login+"' AND password= '"+password+"'"); if (!result.next()){ IDcheck = 0; out.print(IDcheck); out.flush(); } else { IDcheck = 1; out.println(IDcheck); out.flush(); String s = "Server"; ChatWindow cw = new ChatWindow(socket,s); } } catch (IOException e) { System.out.println("Error connecting to the Server."); } catch (SQLException e) { System.out.println("Error connecting to the SQL Server."); } }
a7fc9f87-f87b-4f17-8a1b-0514f02d3d14
public ChatWindow(Socket s, String l) { socket = s; login = l; }
e89926c5-2c0c-4658-9191-2061582cb562
private void initGUI() { try { out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); sdf = new SimpleDateFormat("HH:mm"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jPanel1 = new JPanel(); getContentPane().add(jPanel1, BorderLayout.NORTH); jPanel1.setPreferredSize(new java.awt.Dimension(484, 474)); { jTextPane1 = new JTextPane(); jPanel1.add(jTextPane1); jTextPane1.setPreferredSize(new java.awt.Dimension(483, 473)); jTextPane1.setEditable(false); } } { jPanel2 = new JPanel(); GridBagLayout jPanel2Layout = new GridBagLayout(); jPanel2Layout.columnWidths = new int[] {48, 250, 48, 7}; jPanel2Layout.rowHeights = new int[] {7}; jPanel2Layout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1}; jPanel2Layout.rowWeights = new double[] {0.1}; getContentPane().add(jPanel2, BorderLayout.SOUTH); jPanel2.setLayout(jPanel2Layout); jPanel2.setPreferredSize(new java.awt.Dimension(484, 54)); { jTextField1 = new JTextField(); jPanel2.add(jTextField1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } { jButton1 = new JButton(); jPanel2.add(jButton1, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jButton1.setText("Send"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } } pack(); setSize(500, 600); //Infinite loop to listen for new messages coming in through the socket. while (true){ message = in.readLine(); loginSender = in.readLine(); cal = Calendar.getInstance(); jTextPane1.setText(jTextPane1.getText()+"("+sdf.format(cal.getTime())+") "+loginSender+" says: "+message+"\n"); } } catch (Exception e) { e.printStackTrace(); } }
2538bf98-95ad-41db-8670-b683d7768c68
public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); }
cbe7ee3b-06ae-4f87-a093-954e1f8b71f1
private void jButton1ActionPerformed(ActionEvent evt) { if (jTextField1.getText().isEmpty()) { JOptionPane.showMessageDialog(this, "Please type in a message."); } else { out.println(jTextField1.getText()); out.flush(); out.println(login); out.flush(); cal = Calendar.getInstance(); jTextPane1.setText(jTextPane1.getText()+"("+sdf.format(cal.getTime())+") "+login+" says: "+jTextField1.getText()+"\n"); jTextField1.setText(""); } }
06d8269d-623e-461c-86e7-dcd6843baa0d
public void run() { this.setLocationRelativeTo(null); this.setVisible(true); this.setTitle("Java Instant Chat - Client"); initGUI(); }
e22efbe8-203f-46ce-86b8-fd7781a07e5a
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Welcome inst = new Welcome(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setTitle("Java Instant Chat Client"); } }); }
15b2c3f3-83bd-4519-ae63-38e9d2381dce
public void run() { Welcome inst = new Welcome(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setTitle("Java Instant Chat Client"); }
561f739b-b131-4796-b6c0-890cf0de0791
public Welcome() { super(); initGUI(); }
f2e61584-7d4a-4303-8b8a-0257b7325d66
private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jPanel1 = new JPanel(); getContentPane().add(jPanel1, BorderLayout.NORTH); jPanel1.setPreferredSize(new java.awt.Dimension(384, 42)); { jLabel1 = new JLabel(); jPanel1.add(jLabel1); jLabel1.setText("Welcome to JavaInstantChat"); } } { jPanel2 = new JPanel(); GridBagLayout jPanel2Layout = new GridBagLayout(); getContentPane().add(jPanel2, BorderLayout.SOUTH); jPanel2.setPreferredSize(new java.awt.Dimension(384, 212)); jPanel2Layout.rowWeights = new double[] {0.1, 0.1, 0.1}; jPanel2Layout.rowHeights = new int[] {7, 7, 7}; jPanel2Layout.columnWeights = new double[] {0.0, 0.0, 0.1}; jPanel2Layout.columnWidths = new int[] {181, 151, 20}; jPanel2.setLayout(jPanel2Layout); { jLabel2 = new JLabel(); jPanel2.add(jLabel2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel2.setText("Login:"); } { jLabel3 = new JLabel(); jPanel2.add(jLabel3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel3.setText("Password:"); } { jTextField1 = new JTextField(); jPanel2.add(jTextField1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } { jPasswordField1 = new JPasswordField(); jPanel2.add(jPasswordField1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } { jButton1 = new JButton(); jPanel2.add(jButton1, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jButton1.setText("Connect"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } { jButton2 = new JButton(); jPanel2.add(jButton2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jButton2.setText("Exit"); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton2ActionPerformed(evt); } }); } } pack(); setSize(400, 300); } catch (Exception e) { e.printStackTrace(); } }
9f2428b1-7874-43d6-8848-e81149583222
public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); }
a66e664a-9044-4a50-9e10-1b4db235f741
public void actionPerformed(ActionEvent evt) { jButton2ActionPerformed(evt); }
81009ce9-2331-4384-90c6-fcc60bf7b7c5
private void jButton1ActionPerformed(ActionEvent evt) { try { socket = new Socket("127.0.0.1",2009); login = jTextField1.getText(); password = new String(jPasswordField1.getPassword()); LoginCheck lc = new LoginCheck(socket); IDcheck = lc.Check(login, password); if (!IDcheck){ JOptionPane.showMessageDialog(this, "Bad login and/or password."); } else{ t2 = new Thread(new ChatWindow(socket,login)); t2.start(); this.dispose(); } } catch (UnknownHostException e) { System.err.println("Can't connect to address "+socket.getLocalAddress()); } catch (IOException e) { System.err.println("No server listening on port "+socket.getLocalPort()); } }
9829d202-4b10-48ca-acb5-321585768379
private void jButton2ActionPerformed(ActionEvent evt) { this.dispose(); }
f959176b-1714-470b-bd9f-058d2c7438eb
public LoginCheck(Socket s){ socket = s; }
2c2d7d2a-0da3-4f71-b8ab-2be887055b9b
public boolean Check(String login, String password) { try { out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.println(login); out.flush(); out.println(password); out.flush(); j = Integer.parseInt(in.readLine()); switch(j){ case 0 : return false; case 1 : return true; } } catch (IOException e) { System.out.println("Error connecting to the Server."); } return false; }
43da4de0-d990-4bf3-9690-87fe9bc9ca81
public static CellStyle style(Workbook workbook) { Font font = font(workbook); CellStyle style = workbook.createCellStyle(); style.setFont(font); return style; }
d014533a-f929-4d89-ad44-6353f5357e4a
public static Font font(Workbook workbook) { Font font = workbook.createFont(); font.setColor(HSSFFont.COLOR_RED); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 15); return font; }
3f8d0f82-6ef2-46c8-81cc-28da11b1d5bf
public boolean isInterupt() { return isInterupt; }
7916831f-0f29-4b6b-a3c4-f91890e10752
public void setInterupt(boolean isInterupt) { this.isInterupt = isInterupt; }
0993b8ff-5f0e-4c7c-92a2-908a393100a0
public Highlight(int rowIndex, int cellIndex) { this.rowIndex = rowIndex; this.cellIndex = cellIndex; }
1bfe9cba-9002-44d8-b1c7-450522ae9f46
public int getRowIndex() { return rowIndex; }
ddbd4cad-e615-459b-a2ea-c70e9706b3c1
public int getCellIndex() { return cellIndex; }
0929e64a-bf71-4a4c-9820-97b64643b56a
public ExcelServiceImpl(String path) { this.filePath = path; }
3793fde2-d6bc-4b9d-9767-ae8d95963dfe
public ExcelServiceImpl(String path, int sheetNum) { this.filePath = path; this.sheetNum = sheetNum; }
af7027b3-3bd6-417a-8577-d40f10b7b9fc
@Override public List<Cell> getDataAtColumn(int column) throws Exception { Workbook workbook = _createWorkbook(); Sheet sheet = workbook.getSheetAt(sheetNum); return ExcelUtils.getCell(sheet, column); }
ac45d32f-9937-4b87-acac-2d1029b4ebfb
@Override public void highlight(List<Highlight> highlights) throws Exception { Workbook workbook = _createWorkbook(); for (Highlight highlight : highlights) { _setHighlight(workbook, highlight); } FileOutputStream out = new FileOutputStream(new File(filePath)); workbook.write(out); out.close(); }
bb344b67-3677-4067-add7-306880455310
private Workbook _createWorkbook() throws Exception { InputStream is = new FileInputStream(new File(filePath)); if (filePath.toLowerCase().endsWith("xlsx")) { return new XSSFWorkbook(is); } else if (filePath.toLowerCase().endsWith("xls")) { return new HSSFWorkbook(is); } is.close(); throw new Exception("Only support xls or xlsx"); }
77ba8c37-c54e-4b32-96c2-4e773b8e23ab
private void _setHighlight(Workbook workbook, Highlight highlight) throws Exception { Sheet sheet = workbook.getSheetAt(sheetNum); int rowIndex = highlight.getRowIndex(); int cellIndex = highlight.getCellIndex(); Cell cell = sheet.getRow(rowIndex).getCell(cellIndex); cell.setCellStyle(MarkOther.style(workbook)); }
25a6ecc9-4a51-43ad-9192-bbc624b3f2e0
@Override public ExcelService path(String path) { this.filePath = path; return this; }
b4a81cce-fac8-4470-9519-d36669b33d13
public DuplicateStrategyCheckServiceImpl(ExcelService excelService) { this.excelService = excelService; }
55571c0c-1beb-4f33-9bb3-e2948288bd27
public void checkDuplicate() throws Exception { List<Highlight> highlights = _getHighlights(); excelService.path(Contants.SYMBOLS_XLSX).highlight(highlights); }
4abf309e-54c6-4521-a1d9-b30572d6464f
private List<Highlight> _getHighlights() throws Exception { List<Integer> listYesNo = ExcelUtils.getCellValueAsInteger(excelService.getDataAtColumn(Contants.CHECK_DUPLICATE_COLUMN)); List<Highlight> highlights = new ArrayList<Highlight>(); for (int i = 0; i < listYesNo.size(); i++) { if (i == 1) { highlights.add(new Highlight(i, 0)); } } return highlights; }
29406825-d0e3-44c1-a89f-c3a2671b7984
List<Cell> getDataAtColumn(int column) throws Exception;
3d05b613-4325-46fa-9a98-44252df9d6f5
void highlight(List<Highlight> highlights) throws Exception;
6cdcd777-d899-411f-8159-347a4bb1354b
ExcelService path(String path);
5a42a345-4a9f-4c4a-8d49-1dc1edbbeb75
public MorningAfternoonStrategyCompareService(ExcelService excelService) { this.excelService = excelService; }
b6da6747-108e-447d-b7d5-66ec7345f947
public void compareMorningAfternoon() throws Exception { List<Highlight> highlights = _getHighlightList(); excelService.highlight(highlights); }
6a6b3bfc-efc5-4a9c-bd18-2a96774adc8d
private List<Highlight> _getHighlightList() throws Exception { List<String> firstColumn = ExcelUtils.getCellValueAsString(excelService.getDataAtColumn(0)); List<String> secondColumn = ExcelUtils.getCellValueAsString(excelService.getDataAtColumn(1)); List<Highlight> highlights = new ArrayList<Highlight>(); for (int i = 0; i < firstColumn.size(); i++) { String value = firstColumn.get(i); if (!secondColumn.contains(value)) { highlights.add(new Highlight(i, 0)); } } return highlights; }
f92d8c27-4bd2-4414-9303-8a0218fb97e9
void checkDuplicate() throws Exception;
b6ac228b-c0fd-4b82-bd1c-a216c944d32c
public static List<Cell> getCell(Sheet sheet, int column) { List<Cell> cells = new ArrayList<Cell>(); Iterator<Row> rowIter = sheet.rowIterator(); while (rowIter.hasNext()) { Row row = (Row) rowIter.next(); cells.add(row.getCell(column)); } return cells; }
56c0f653-5f20-4e14-b1ac-036c29d71ec6
public static List<String> getCellValueAsString(List<Cell> cells) { List<String> values = new ArrayList<String>(); for (Cell cell : cells) { if (cell == null) { break; } values.add(cell.getStringCellValue().toUpperCase()); } return values; }
bea65f94-ba47-4d6c-8aa4-e25f53d1c623
public static List<Integer> getCellValueAsInteger(List<Cell> cells) { List<Integer> values = new ArrayList<Integer>(); for (Cell cell : cells) { if (cell == null) { continue; } try { values.add((int) cell.getNumericCellValue()); } catch (Exception e) { continue; } } return values; }
c4dbbb91-69f8-4b53-b163-516ffad4b13b
public StrategyFilterView(String title) { super(title); _addListener(); _buildUI(); setSize(700, 150); setLocation(400, 300); }
340d310b-b84b-4fce-9649-c1dca670f30a
@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == this.morningAfternoonCompareBtn) { try { status.setText(" - Processing "); final ThreadInterceptor ti = new ThreadInterceptor(); Thread th = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }); th.start(); ExcelService excelService = new ExcelServiceImpl(Contants.SANGCHIEU_XLSX); MorningAfternoonStrategyCompareService service = new MorningAfternoonStrategyCompareService(excelService); service.compareMorningAfternoon(); ti.setInterupt(true); status.setText(status.getText() + " DONE !!!"); Thread.sleep(200); status.setText(" - Opening ."); Thread.sleep(200); status.setText(status.getText() + "."); Thread.sleep(200); status.setText(status.getText() + ".."); Thread.sleep(400); Desktop.getDesktop().open(new File(Contants.SANGCHIEU_XLSX)); status.setText(" * Caramen +1"); } catch (Exception ex) { status.setText(ex.getMessage()); } } else if (e.getSource() == this.checkDuplicateBLightBtn) { try { status.setText(" - Processing "); final ThreadInterceptor ti = new ThreadInterceptor(); Thread th = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }); th.start(); ExcelService excelService = new ExcelServiceImpl(Contants.CHECK_DUPLICATE_XLSX, 0); DuplicateStrategyCheckService service = new DuplicateStrategyCheckServiceImpl(excelService); service.checkDuplicate(); ti.setInterupt(true); status.setText(status.getText() + " DONE !!!"); Thread.sleep(200); status.setText(" - Opening ."); Thread.sleep(200); status.setText(status.getText() + "."); Thread.sleep(200); status.setText(status.getText() + ".."); Thread.sleep(400); Desktop.getDesktop().open(new File(Contants.SYMBOLS_XLSX)); status.setText("Caramen +1"); } catch (Exception ex) { status.setText(ex.getMessage()); } } else if (e.getSource() == this.checkDuplicateVTradeBtn) { try { status.setText(" - Processing "); final ThreadInterceptor ti = new ThreadInterceptor(); Thread th = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }); th.start(); ExcelService excelService = new ExcelServiceImpl(Contants.CHECK_DUPLICATE_XLSX, 1); DuplicateStrategyCheckService service = new DuplicateStrategyCheckServiceImpl(excelService); service.checkDuplicate(); ti.setInterupt(true); status.setText(status.getText() + " DONE !!!"); Thread.sleep(200); status.setText(" - Opening ."); Thread.sleep(200); status.setText(status.getText() + "."); Thread.sleep(200); status.setText(status.getText() + ".."); Thread.sleep(400); Desktop.getDesktop().open(new File(Contants.SYMBOLS_XLSX)); status.setText("Caramen +1"); } catch (Exception ex) { status.setText(ex.getMessage()); } } }
905cc851-7eb7-4735-a28b-18414574284a
@Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } }
8bd94ba1-051d-4d18-80e0-9ca47d86d60c
@Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } }
82b36421-b7df-484f-8dd6-5144c655ecd0
@Override public void run() { for (int i = 0; i < 30; i++) { if (ti.isInterupt()) { break; } status.setText(status.getText() + "->"); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } }
a6edb85d-8a75-4f4d-a62a-0baaf88a83c6
private void _buildUI() { this.status.setForeground(Color.DARK_GRAY); Font font = new Font("", Font.BOLD, 14); this.status.setFont(font); this.morningAfternoonCompareBtn.setBackground(Color.DARK_GRAY); this.morningAfternoonCompareBtn.setForeground(Color.ORANGE); this.checkDuplicateBLightBtn.setBackground(Color.DARK_GRAY); this.checkDuplicateBLightBtn.setForeground(Color.ORANGE); this.checkDuplicateVTradeBtn.setBackground(Color.DARK_GRAY); this.checkDuplicateVTradeBtn.setForeground(Color.ORANGE); Panel p1 = new Panel(new GridLayout(1, 3)); p1.add(this.morningAfternoonCompareBtn); p1.add(this.checkDuplicateBLightBtn); p1.add(this.checkDuplicateVTradeBtn); setLayout(new BorderLayout()); add(this.status, "North"); add(p1, "Center"); add(new Label(), "South"); add(new Label(), "East"); add(new Label(), "West"); setBackground(Color.ORANGE); }
4e3451dc-7044-421f-a93a-e620b14a37f5
private void _addListener() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { StrategyFilterView.this.dispose(); } }); this.morningAfternoonCompareBtn.addActionListener(this); this.checkDuplicateBLightBtn.addActionListener(this); this.checkDuplicateVTradeBtn.addActionListener(this); }
b6456869-bc24-4846-942a-8f6481914dfd
public void windowClosing(WindowEvent e) { StrategyFilterView.this.dispose(); }
064b9fe4-2446-4047-8a0a-59cb27cf66f8
public static void main(String[] args) { StrategyFilterView view = new StrategyFilterView("© minh.nguyen vnds"); view.setVisible(true); }
fcaa5d29-07f2-4fab-ae89-e7894e621c7a
@Before public void setUp() { service = new ExcelServiceImpl(path); }
0b90cad9-f8d9-4ccf-8994-73b18fa9e911
@Test public void testGetDuplicateData() throws Exception { service = new ExcelServiceImpl(path, 0); List<Cell> cells = service.getDataAtColumn(5); List<Integer> values = ExcelUtils.getCellValueAsInteger(cells); System.out.println(values); System.out.println(values.size()); }
1d090db5-3d91-4f5d-97a6-4fe89564b56d
@Test public void testGetColumnData() throws Exception { List<Cell> cells = service.getDataAtColumn(0); for (Cell cell : cells) { System.out.println(cell.getStringCellValue()); } }
b4559ea5-bde9-44f9-bd86-899d1677aa04
@Test public void testHighlight() throws Exception { Highlight[] highlightArr = new Highlight[] { new Highlight(1, 0) }; service.highlight(Arrays.asList(highlightArr)); }
e5179c03-116a-46d8-bb98-b0b3056a9a38
@Before public void setUp() { excelService = new ExcelServiceImpl(filePath); service = new MorningAfternoonStrategyCompareService(excelService); }