Ya se encuentra disponible la plataforma Git en producción. Se recomienda migrar sus proyectos ASAP. GitBETA será cerrado a partir del segundo semestre de 2020.

Para guías de cómo migrar sus proyectos, véase la documentación de Gitlab sobre Importar/Exportar proyectos

Commit 3b95aa83 authored by TheMikox16's avatar TheMikox16

Finalizado laboratorio 4

parent f4880577
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package labcode;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
* @author Sean Stward Campos Siles Carne: B91569
*/
public class Client {
private DataOutputStream output;
private DataInputStream input;
private Socket client;
private final String HOST = "127.0.0.1";
private final int PORT = 1334;
public void runClient() {
try {
connectToServer();
getStream();
processConnection();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
closeConnection();
}
}
private void connectToServer() throws IOException {
System.out.println("Intentando conectar\n");
client = new Socket(HOST, PORT);
System.out.println("Conectado a: " + client.getInetAddress().getHostName());
}
private void getStream() throws IOException {
output = new DataOutputStream(client.getOutputStream());
output.flush();
input = new DataInputStream(client.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
int option, quantity, temp;
boolean confirm, repeat, mainMenu;
repeat = mainMenu = true;
option = 0;
while (mainMenu) {
message = input.readUTF();
repeat = true;
while (repeat) {
option = Integer.parseInt(JOptionPane.showInputDialog(null,
message));
if (option > 0 && option < 6) {
repeat = false;
output.writeInt(option);
} else if (option == 0) {
repeat = false;
mainMenu = false;
output.writeInt(option);
} else {
JOptionPane.showMessageDialog(null, "Opción invalida, "
+ "intente de nuevo");
}
}
if (mainMenu) {
message = input.readUTF();
quantity = Integer.parseInt(JOptionPane.showInputDialog(null,
message));
output.writeInt(quantity);
for (int i = 0; i < quantity; i++) {
message = input.readUTF();
temp = Integer.parseInt(JOptionPane.showInputDialog(null,
message));
output.writeInt(temp);
message = input.readUTF();
temp = Integer.parseInt(JOptionPane.showInputDialog(null,
message));
output.writeInt(temp);
if (input.readBoolean()) {
--i;
JOptionPane.showMessageDialog(null, "Error al asignar asiento, "
+ "intente de nuevo", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
message = input.readUTF();
repeat = true;
while (repeat) {
option = Integer.parseInt(JOptionPane.showInputDialog(null,
message));
switch (option) {
case 1:
output.writeBoolean(true);
repeat = false;
break;
case 2:
if (JOptionPane.showConfirmDialog(null, "¿Desea cancelar la compra?") == 0) {
output.writeBoolean(false);
repeat = false;
}
break;
default:
JOptionPane.showMessageDialog(null, "Opción invalida, "
+ "intente de nuevo");
break;
}
}
}
}
}
private void closeConnection() {
System.out.println("\nCerrando Conexión");
try {
output.close();
input.close();
client.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Client().runClient();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package labcode;
import basecode.Cinema;
import basecode.Movie;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
* @author Sean Stward Campos Siles Carne: B91569
*/
public class Connection extends Thread {
private Socket connection;
private DataOutputStream output;
private DataInputStream input;
private Cinema cinema;
private static final int PRICE = 2500;
private final ProcessBuy buy = ProcessBuy.getInstance();
public Connection(Socket connection, Cinema cinema) {
this.connection = connection;
this.cinema = cinema;
}
private void getStreams() throws IOException {
output = new DataOutputStream(connection.getOutputStream());
output.flush();
input = new DataInputStream(connection.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
boolean repeat, confirmation, mainMenu;
int option, quantity, line, row, column, total;
Movie movie;
total = 0;
mainMenu = true;
while (mainMenu) {
message = "Bienvenido a Cinemark, seleccione su pelicula: \n\n"
+ cinema.getMoviesString() + "\n\n Digite 0 para salir";
output.writeUTF(message);
option = input.readInt();
switch (option) {
case 0:
mainMenu = false;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
buy.waitMovie(option - 1);
buy.setOcuppied(option - 1);
movie = cinema.getMovie(option - 1);
message = "Decripción: " + movie.getDescription()
+ "\n\nSeleccione la cantidad de asientos";
output.writeUTF(message);
quantity = input.readInt();
for (int i = 0; i < quantity; i++) {
repeat = true;
while (repeat) {
message = movie.getSeatsString() + "\n\nIntroduzca la fila";
output.writeUTF(message);
row = input.readInt();
message = movie.getSeatsString() + "\n\nIntroduzca la columna";
output.writeUTF(message);
column = input.readInt();
if (movie.setSeat(row, column)) {
repeat = false;
}
output.writeBoolean(repeat);
}
}
total = PRICE * quantity;
message = movie.getSeatsString() + "\n\nMonto a pagar: "
+ total + "\n\nDIgite 1 para pagar, 2 para cancelar";
output.writeUTF(message);
confirmation = input.readBoolean();
if (confirmation) {
movie.saveSeatsSelection();
} else {
movie.undoSeatsSelection();
}
buy.setDesocuppied(option - 1);
buy.notifyMovie();
break;
default:
JOptionPane.showMessageDialog(null, "Opción invalida, "
+ "intente de nuevo");
break;
}
}
}
private void closeConnection() {
System.out.println("\nTerminando Conexión");
try {
output.close();
input.close();
connection.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
try {
getStreams();
processConnection();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally {
closeConnection();
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package labcode;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
* @author Sean Stward Campos Siles Carne: B91569
*/
public class ProcessBuy {
private static ProcessBuy buy = new ProcessBuy();
private ArrayList<Boolean> entries = new ArrayList<>();
private ProcessBuy() {
for(int i = 0; i < 5; i++){
entries.add(false);
}
}
public void setEntries(ArrayList<Boolean> entries){
this.entries = entries;
}
public static ProcessBuy getInstance(){
return buy;
}
public synchronized void waitMovie(int i){
if(entries.get(i)){
JOptionPane.showMessageDialog(null, "Espere a que el otro cliete "
+ "termine su compra");
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(ProcessBuy.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public synchronized void notifyMovie(){
notify();
}
public void setOcuppied(int i) {
entries.set(i, true);
}
public void setDesocuppied(int i) {
entries.set(i, false);
}
public boolean getEntry(int i){
return entries.get(i);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package labcode;
import basecode.Cinema;
import basecode.CinemaException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
* @author Sean Stward Campos Siles Carne: B91569
*/
public class Server {
private ServerSocket server;
private Socket connection;
private final int PORT = 1334;
private DataOutputStream output;
private DataInputStream input;
private Cinema cinema;
private final ProcessBuy buy = ProcessBuy.getInstance();
public void runServer() {
try {
cinema = new Cinema();
server = new ServerSocket(PORT);
while(true){
waitForConnection();
new Connection(connection, cinema).start();
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (CinemaException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}finally {
closeConnection();
}
}
private void waitForConnection() throws IOException {
System.out.println("Server esperando por conexión (cliente)...\n");
connection = server.accept();
System.out.println("\nConexión recibida de: " + connection.getInetAddress().getHostName());
}
private void closeConnection(){
System.out.println("\nTerminando Conexión");
try{
server.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
public static void main (String[] args){
new Server().runServer();
}
public Cinema getCinema() {
return cinema;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment