Ok, questi sono i due codici che ho sviluppato...
l'unica cosa che devo ancora fare è usare il menù per caricare i dati in entrata da file e salvare quelli in uscita...
mi spiego meglio:
se uso load, il programma dovrà caricarmi i dati dei posti occupati che ho salvato in precedenza con save/save as.
qualche idea?
:spy:
package cinema;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class Cinema {
public Cinema()
{
posti=new boolean[ROWS][COLUMNS];
for (int i=0; i<ROWS; i++)
for (int j=0;j<COLUMNS;j++)
posti[i][j]=false;
fila=new String[ROWS];
fila[0]="A";
fila[1]="B";
fila[2]="C";
fila[3]="D";
fila[4]="E";
fila[5]="F";
fila[6]="G";
fila[7]="H";
fila[8]="I";
fila[9]="J";
}
public String prenota(int i, int j)
{
if(posti[i-1][j-1]!=false)
{
return("Il posto è già prenotato!");
}
else
{
i-=1;
j-=1;
posti[i][j]=true;
return("Hai prenotato il posto "+(j+1)+" in fila "+fila[i]+".");
}
}
public String prenotaAbbonamento(int i, int j, String cliente, String indirizzo)
{
if(posti[i-1][j-1]!=false)
{
return("Il posto è già prenotato!");
}
else
{
i-=1;
j-=1;
posti[i][j]=true;
return("Hai prenotato il posto "+(j+1)+" in fila "+fila[i]+" per una stagione!");
}
}
public void load()
{
JFileChooser chooser=new JFileChooser();
FileReader in=null;
if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
File selectedFile=chooser.getSelectedFile();
try {
in=new FileReader(selectedFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public void saveas()
{
JFileChooser chooser=new JFileChooser();
PrintWriter out=null;
if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
{
File selectedFile=chooser.getSelectedFile();
try {
out=new PrintWriter(selectedFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
ArrayList<Posto> postiCinema=new ArrayList<Posto>();
private String fila[];
private boolean[][] posti;
private static final int ROWS=10;
private static final int COLUMNS=10;
}
package cinema;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CinemaField extends JFrame{
public CinemaField()
{
nuovoCinema=new Cinema();
resultArea=new JTextArea(AREA_ROWS, AREA_COLUMNS);
resultArea.setEditable(false);
setTitle("Cinema");
createTextFieldFila();
createTextFieldPosto();
createButtonPrenota();
createTextFieldNome();
createTextFieldIndirizzo();
createButtonPrenotaAbbonamento();
createPanel();
createPosti();
createSeparator();
createMenu();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createTextFieldFila()
{
filaLabel=new JLabel("Fila: ");
final int FIELD_WIDTH=2;
filaField= new JTextField(FIELD_WIDTH);
}
private void createTextFieldPosto()
{
postoLabel=new JLabel("Posto: ");
final int FIELD_WIDTH=2;
postoField= new JTextField(FIELD_WIDTH);
}
private void createTextFieldNome()
{
nomeLabel=new JLabel("Nome: ");
final int FIELD_WIDTH=18;
nomeField= new JTextField(FIELD_WIDTH);
}
private void createTextFieldIndirizzo()
{
indirizzoLabel=new JLabel("Indirizzo: ");
final int FIELD_WIDTH=18;
indirizzoField= new JTextField(FIELD_WIDTH);
}
private void createButtonPrenota()
{
prenotaButton= new JButton("Prenota");
class prenotaListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int fila= Integer.parseInt(filaField.getText());
int posto= Integer.parseInt(postoField.getText());
resultArea.setText("Stato prenotazione: "+nuovoCinema.prenota(fila, posto));
}
}
ActionListener listener = new prenotaListener();
prenotaButton.addActionListener(listener);
}
private void createButtonPrenotaAbbonamento()
{
prenotaAbbonamentoButton= new JButton("Prenota Abbonamento");
class prenotaListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int fila= Integer.parseInt(filaField.getText());
int posto= Integer.parseInt(postoField.getText());
String nome=nomeField.getText();
String indirizzo=indirizzoField.getText();
resultArea.setText("Stato prenotazione: "+nuovoCinema.prenotaAbbonamento(fila, posto, nome, indirizzo));
}
}
ActionListener listener = new prenotaListener();
prenotaAbbonamentoButton.addActionListener(listener);
}
private void createSeparator()
{
sep = new JSeparator();
panel.add(sep);
}
private void createPosti()
{
GridBagConstraints c = new GridBagConstraints();
int posx=0;
int posy=0;
for(int i=0;i<10;i++){
c.gridx = posx;
c.gridy = posy;
JLabel q = new JLabel("o");
q.setBackground(new Color(1,22,22));
q.setOpaque(true);
panel.add(q,c);
posx++;
if(posx==5){
posx=0;
posy++;
}
}
}
private void createMenu()
{
MenuBar = new JMenuBar();
fileMenu = new JMenu("File");
load=new JMenuItem("Load..");
fileMenu.add(load);
save=new JMenuItem("Save");
fileMenu.add(save);
saveas=new JMenuItem("Save as..");
fileMenu.add(saveas);
fileMenu.insertSeparator(3);
exit=new JMenuItem("Exit");
fileMenu.add(exit);
MenuBar.add(fileMenu);
setJMenuBar(MenuBar);
class LoadListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
nuovoCinema.load();
}
}
ActionListener loadlistener = new LoadListener();
load.addActionListener(loadlistener);
class SaveasListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
nuovoCinema.saveas();
}
}
ActionListener saveaslistener = new SaveasListener();
saveas.addActionListener(saveaslistener);
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(NORMAL);
}
}
ActionListener exitlistener = new ExitListener();
exit.addActionListener(exitlistener);
}
private void createPanel()
{
panel=new JPanel();
panel.add(filaLabel);
panel.add(filaField);
panel.add(postoLabel);
panel.add(postoField);
panel.add(prenotaButton);
JScrollPane scrollPane=new JScrollPane(resultArea);
panel.add(scrollPane);
add(panel);
panel.add(nomeLabel);
panel.add(nomeField);
panel.add(indirizzoLabel);
panel.add(indirizzoField);
panel.add(prenotaAbbonamentoButton);
}
private JLabel filaLabel;
private JLabel postoLabel;
private JLabel indirizzoLabel;
private JLabel nomeLabel;
private JTextField filaField;
private JTextField postoField;
private JTextField indirizzoField;
private JTextField nomeField;
private JButton prenotaButton;
private JButton prenotaAbbonamentoButton;
private JPanel panel;
private Cinema nuovoCinema;
private JTextArea resultArea;
private JSeparator sep;
private JMenuBar MenuBar;
private JMenu fileMenu;
private JMenuItem load;
private JMenuItem saveas;
private JMenuItem save;
private JMenuItem exit;
private static final int FRAME_WIDTH=220;
private static final int FRAME_HEIGHT=500;
private static final int AREA_ROWS=10;
private static final int AREA_COLUMNS=15;
}