Salve ragazzi, mi è stato dato un compito per scuola, ovvero simulare la gestione di un teatro tramite la GUI, il teatro deve essere diviso in 4 settori, su cui sarà possibile prenotare un posto a sedere, quindi in dovrà essere creato un file sul quale sono memorizzate tutte le informazioni del posto a sedere prenotato, es prezzo, settore di appartenenza ecc..., inoltre i minori di 23 anni hanno uno sconto del 25%, e i posti a sedere in platea subiranno un rincaro del 20%.
Ho fatto tutta la parte grafica, ma ora sono davanti a un problema che proprio non riesco a superare
; non so come fare a gestire tutta la parte di algoritmo con gli ActionListener, passaggi di parametri ecc...
Classe Window:
codice:
import java.awt.Button;import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import javax.swing.*;
public class Window {
public static final int SECTOR_ONE_SEAT = 20;
public static final int SECTOR_TWO_SEAT = 15;
public static final int SECTOR_THREE_SEAT = 10;
public static final int SECTOR_FOUR_SEAT = 5;
public static final boolean STATE = false;
public static final int SECTOR_ONE = 0;
public static final int SECTOR_TWO = 1;
public static final int SECTOR_THREE = 2;
public static final int SECTOR_FOUR = 3;
private static final Seat[] Seat = null;
private static Window instance;
private Window() {}
public static Window getInstance() {
if (instance == null)
instance = new Window();
return instance;
}
///////////////////////////////////////////////////////
public Frame getWindow() {
Frame frame = new Frame("Gestione Teatro");
frame.setLayout(new GridLayout(5,1));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
///////////////////////////////////////////////////
Panel sectorOnePanel = sectorOne();
Panel sectorTwoPanel = sectorTwo();
Panel sectorThreePanel = sectorThree();
Panel sectorFourPanel = sectorFour();
Panel sectorFivePanel = sectorFive();
frame.add(sectorOnePanel);
frame.add(sectorTwoPanel);
frame.add(sectorThreePanel);
frame.add(sectorFourPanel);
frame.add(sectorFivePanel);
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setVisible(true);
return frame;
}
private Panel sectorOne() {
Panel panel = new Panel(new FlowLayout());
Seat[] seats = initializeSeats(SECTOR_ONE_SEAT, SECTOR_ONE,STATE);
for ( int i=0; i<SECTOR_ONE_SEAT;i++) {
final Button button = seats[i].getButton();
button.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int cnt=0;
cnt++;
Seat[] seats = initializeSeats(SECTOR_ONE_SEAT, SECTOR_ONE,STATE);
button.setBackground(Color.RED);
for(int j=0;j<cnt;j++)
{
seats[j].setState(true);
seats[j].setSector(SECTOR_ONE);
}
}
});
panel.add( button );
}
return panel;
}
private Panel sectorTwo() {
Panel panel = new Panel(new FlowLayout());
Seat[] seats = initializeSeats(SECTOR_TWO_SEAT, SECTOR_TWO,STATE);
for (final Seat seat : seats) {
final Button button = seat.getButton();
button.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int cnt=0;
button.setBackground(Color.RED);
cnt++;
seat.setState(true);
seat.setSector(SECTOR_TWO);
}
});
panel.add( button );
}
return panel;
}
private Panel sectorThree() {
Panel panel = new Panel(new FlowLayout());
Seat[] seats = initializeSeats(SECTOR_THREE_SEAT, SECTOR_THREE,STATE);
for (final Seat seat : seats) {
final Button button = seat.getButton();
button.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int cnt=0;
button.setBackground(Color.RED);
cnt++;
seat.setState(true);
seat.setSector(SECTOR_THREE);
}
});
panel.add( button );
}
return panel;
}
private Panel sectorFour() {
Panel panel = new Panel(new FlowLayout());
Seat[] seats = initializeSeats(SECTOR_FOUR_SEAT, SECTOR_FOUR,STATE);
int z=0;
for (final Seat seat : seats) {
final Button button = seat.getButton();
button.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int cnt=0;
button.setBackground(Color.RED);
cnt++;
seat.setState(true);
seat.setSector(SECTOR_FOUR);
}
});
panel.add( button );
}
return panel;
}
private Panel sectorFive() {
Panel panel = new Panel(new FlowLayout());
JRadioButton radio1 = new JRadioButton("Meno di 23 anni");
JRadioButton radio2 = new JRadioButton("Più di 23 anni");
Label age2 = new Label("Età");
panel.add(age2);
panel.add(radio1);
panel.add(radio2);
radio1.addActionListener(new Minore_23());
return panel;
}
private Seat[] initializeSeats(int numberOfSeats, int sector, boolean state) {
Seat[] seats = new Seat[numberOfSeats];
for(int column = 0; column < numberOfSeats; column++) {
Button button = new Button("Seat " + (column + 1)); // Poichè l'indice parte da 0
//seats[column] = new Seat();
seats[column] = new Seat(button, sector, state, column);
}
return seats;
}
}
classe Teatro:
codice:
import java.awt.*;import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
class Teatro {
private static Window window;
private static Frame frame;
private static Frame frame2;
public static void main(String[] args) {
window = Window.getInstance();
frame = window.getWindow();
}
}
class Minore_23 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
InputStreamReader In = new InputStreamReader(System.in);
BufferedReader Tastiera = new BufferedReader(In);
}
}
classe Posto:
codice:
import java.awt.Button;
public class Seat {
private Button button;
private int sector;
private boolean state;
private int column;
public Seat(Button button, int sector, boolean state, int column) {
super();
this.button = button;
this.sector = sector;
this.state = state;
//this.column = column;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public Button getButton() {
return button;
}
public void setButton(Button button) {
this.button = button;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
public int getSector() {
return sector;
}
public void setSector(int sector) {
this.sector = sector;
}
}