package MenuCena;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.text.*;
public class MenuCena extends JFrame implements ItemListener, ActionListener
{
String platos[] =
{ //-- Platos del Menú (6) --
"Entrantes" , //Primer plato.
"Carnes" , //Segundo plato.
"Pescados" , //Tercer plato.
"Postres" , //Postres.
"Bebidas" , //Bebidas.
"Café / Copa" //Café o Copa.
};
String variedades[][] =
{
{ //-- Entrantes (4) --
"Entremeses variados" , //1º- Entrante
"Marisco" , //2º- Entrante
"Ensalada" , //3º- Entrante
"Gazpacho" //4º- Entrante
},
{ //-- Carnes (7) --
"Filete de Ternera" , //1º- Carne
//"Chuletón de ternera", //2º- Carne
"Entrecot de ternera" , //3º- Carne
"Pato a la naranja" , //4º- Carne
"Filete de cerdo" , //5º- Carne
"OsoBuco" , //6º- Carne
"Filete de pollo a la plancha" //7º- Carne
},
{ //-- Pescados (3) --
"Merluza en salsa" , //1º- Pescado
"Bacalao al pil-pil", //2º- Pescado
"Besugo al horno" //3º- Pescado
},
{ //-- Postres (4) --
"Tarta al whiski" , //1º- Postre
"Flan" , //2º- Postre
"Helado" , //3º- Postre
"Postre de la casa" //4º- Postre
},
{ //-- Bebidas (6) --
"Vino blanco (botella)" , //1º- Bebida
"Vino tinto (botella)" , //2º- Bebida
"Agua pequeña (0,5 l.)" , //3º- Bebida
"Agua grande (1,5 l.)" , //4º- Bebida
"Cerveza (33 cl.)" , //5º- Bebida
"Refrescos (33 cl.)" //6º- Bebida
},
{ //-- Café / Copa (5) --
"Café sólo" , //1º- Café / Copa
"Café cortado" , //2º- Café / Copa
"Café con leche" , //3º- Café / Copa
"Licor de pacharán" , //4º- Café / Copa
"Copa de whisky" //5º- Café / Copa
}
};
String precio[][] =
{
{ //-- Precios de Entrantes (4) --
"10" , //1º- Precio de Entrante (Entremeses variados).
"10,50" , //2º- Precio de Entrante (Marisco).
"6,30" , //3º- Precio de Entrante (Ensalada).
"4,50" //4º- Precio de Entrante (Gazpacho).
},
{ //-- Precios de Carnes (7) --
"5" , //1º- Precio de Carne (Filete de Ternera).
"7,90" , //2º- Precio de Carne (Chuletón de ternera).
"8,50" , //3º- Precio de Carne (Entrecot de ternera).
"5,50" , //4º- Precio de Carne (Pato a la naranja).
"3,50" , //5º- Precio de Carne (Filete de cerdo).
"4,50" , //6º- Precio de Carne (OsoBuco).
"3,50" //7º- Precio de Carne (Filete de pollo a la plancha).
},
{ //-- Precios de Pescados (3) --
"6,90" , //1º- Precio de Pescado (Merluza en salsa).
"7,50" , //2º- Precio de Pescado (Bacalao al pil-pil).
"12" //3º- Precio de Pescado (Besugo al horno).
},
{ //-- Precios de Postres (4) --
"3" , //1º- Precio de Postre (Tarta al whiski).
"2,50" , //2º- Precio de Postre (Flan).
"2,50" , //3º- Precio de Postre (Helado).
"4" //4º- Precio de Postre (Postre de la casa).
},
{ //-- Precios de Bebidas (6) --
"7" , //1º- Precio de Bebida (Vino blanco).
"8" , //2º- Precio de Bebida (Vino tinto).
"1,50" , //3º- Precio de Bebida (Agua pequeña).
"2,50" , //4º- Precio de Bebida (Agua grande).
"4" , //5º- Precio de Bebida (Cerveza).
"2,50" //6º- Precio de Bebida (Refrescos).
},
{ //-- Precios de Café / Copa (5) --
"1" , //1º- Precio de Café / Copa (Café sólo).
"1,10" , //2º- Precio de Café / Copa (Café cortado).
"1,20" , //3º- Precio de Café / Copa (Café con leche).
"2,20" , //4º- Precio de Café / Copa (Copa de pacharán).
"3,50" //5º- Precio de Café / Copa (Copa de whisky).
}
};
int iNumTotalPlatos = platos.length; //Número total de platos del menú.
int NumVariedadesPlatoActual[]; //Número total de variedades del plato actual.
String sMonedaPrecio = "€"; //Moneda a mostrar en los precios.
Color cRojo = Color.red; //Asignamos el color rojo a la variable de color cRojo.
Color cVerde = Color.green; //Asignamos el color verde a la variable de color cVerde.
Color cAzul = Color.blue; //Asignamos el color azúl a la variable de color cAzul.
Color cBlanco = Color.white; //Asignamos el color blanco a la variable de color cBlanco.
Color cNegro = Color.black; //Asignamos el color negro a la variable de color cNegro.
Color cCian = Color.cyan; //Asignamos el color cian a la variable de color cCian.
ButtonGroup gb[]= new ButtonGroup[iNumTotalPlatos];
JButton b1,b2,b3,b4;
JPanel p,p1,p2,p3,p4,p5,p2i[] = new JPanel[iNumTotalPlatos]; //6 paneles y uno de tipo array.
JTextArea ta;
JScrollPane jspTextArea; //Hemos cambiado el nombre de la clase tajsp por jspTextArea.
JScrollPane jsp;
JRadioButton rb[][];//= new JRadioButton[platos.length][variedades[5].length];
JCheckBox cb[] = new JCheckBox[iNumTotalPlatos];
boolean agotado[][];
BorderLayout bl; //Creamos una capa Borde para poner los botones de debajo.
Font fNegro,fRojo;
//
public MenuCena()
{
int i,j;
//Declaramos las variables tipo int para los margenes de la clase Insets para los 4 botones.
int iMargenArribaInsetB1,iMargenArribaInsetB2,iMargenArribaInsetB3,iMargenArribaInsetB4;
int iMargenIzquierdoInsetB1,iMargenIzquierdoInsetB2,iMargenIzquierdoInsetB3,iMargenIzquierdoInsetB4;
int iMargenAbajoInsetB1,iMargenAbajoInsetB2,iMargenAbajoInsetB3,iMargenAbajoInsetB4;
int iMargenDerechoInsetB1,iMargenDerechoInsetB2,iMargenDerechoInsetB3,iMargenDerechoInsetB4;
//Establecemos los valores de las variables tipo int para los margenes de la clase Insets para los 4 botones.
iMargenArribaInsetB1 = 50;
iMargenArribaInsetB2 = 50;
iMargenArribaInsetB3 = 50;
iMargenArribaInsetB4 = 50;
iMargenIzquierdoInsetB1 = 50;
iMargenIzquierdoInsetB2 = 50;
iMargenIzquierdoInsetB3 = 50;
iMargenIzquierdoInsetB4 = 50;
iMargenAbajoInsetB1 = 50;
iMargenAbajoInsetB2 = 50;
iMargenAbajoInsetB3 = 50;
iMargenAbajoInsetB4 = 50;
iMargenDerechoInsetB1 = 50;
iMargenDerechoInsetB2 = 50;
iMargenDerechoInsetB3 = 50;
iMargenDerechoInsetB4 = 50;
//Creamos las 4 instancias de la clase Insets para los 4 botones(estableciendo los márgenes correspondientes con las variables).
Insets insB1= new Insets (
iMargenArribaInsetB1,
iMargenIzquierdoInsetB1,
iMargenAbajoInsetB1,
iMargenDerechoInsetB1
);
Insets insB2= new Insets (
iMargenArribaInsetB2,
iMargenIzquierdoInsetB2,
iMargenAbajoInsetB2,
iMargenDerechoInsetB2
);
Insets insB3= new Insets (
iMargenArribaInsetB3,
iMargenIzquierdoInsetB3,
iMargenAbajoInsetB3,
iMargenDerechoInsetB3
);
Insets insB4= new Insets (
iMargenArribaInsetB4,
iMargenIzquierdoInsetB4,
iMargenAbajoInsetB4,
iMargenDerechoInsetB4
);
bl = new BorderLayout();
//Declaramos las variables tipo int para los números de filas y de columnas de los paneles p, p1, p2 y p3.
int iNumColumnasP,iNumColumnasP1,iNumColumnasP2,iNumColumnasP3;
int iNumFilasP,iNumFilasP1,iNumFilasP2,iNumFilasP3;
//Establecemos los valores de las variables tipo int para los números de filas y de columnas de los paneles p, p1, p2 y p3.
iNumColumnasP = 1;
iNumColumnasP1 = 1;
iNumColumnasP2 = iNumTotalPlatos;
iNumColumnasP3 = 1;
// iNumColumnasP4 = 1;
iNumFilasP = 4;
iNumFilasP1 = 1;
iNumFilasP2 = 1;
iNumFilasP3 = 1;
// iNumFilasP4 = 1;
p = new JPanel(new GridLayout(iNumFilasP,iNumColumnasP));
p1 = new JPanel(new GridLayout(iNumFilasP1,iNumColumnasP1));
p2 = new JPanel(new GridLayout(iNumFilasP2,iNumColumnasP2));
p3 = new JPanel(new GridLayout(iNumFilasP3,iNumColumnasP3));
p4 = new JPanel(bl); //new GridLayout(1,1));
for(i=0;i<iNumTotalPlatos;i++)
{
p2i[i] = new JPanel (
new GridLayout (
iNumTotalPlatos,
1
)
);
p2i[i].setBorder (
BorderFactory.createTitledBorder (
BorderFactory.createEtchedBorder(),
platos[i].toString()
)
);
}
// panel 1
for(i=0; i<cb.length; i++)
{
cb[i]= new JCheckBox(platos[i]);
cb[i].addItemListener(this);
cb[i].setBackground(cVerde);
p1.add(cb[i]);
}
// panel 2
rb= new JRadioButton[iNumTotalPlatos][];
agotado = new boolean[iNumTotalPlatos][];
for(i=0; i<iNumTotalPlatos; i++)
{ rb[i] = new JRadioButton[iNumTotalPlatos];
agotado[i] = new boolean[iNumTotalPlatos];
}
for(i=0; i<iNumTotalPlatos; i++)
{
gb[i] = new ButtonGroup();
int iNumVariedadesPlatoActual = variedades[i].length;
for (j=0; j<iNumVariedadesPlatoActual; j++)
{
rb[i][j] = new JRadioButton(variedades[i][j],false);
agotado[i][j]= false;
rb[i][j].addItemListener(this);
rb[i][j].setEnabled(false);
gb[i].add(rb[i][j]);
p2i[i].add(rb[i][j]);
rb[i][j].setBackground(cCian);
//Establecemos el mensaje de ayuda que aparece en el radiobutton correspondiente.
rb[i][j].setToolTipText (
variedades[i][j] +
" ...... " +
precio[i][j] +
sMonedaPrecio +
" (IVA no incluido). "
);
}
rb[i][0].setSelected(true);
p2.add(p2i[i]);
}
// Panel 3 �rea de texto.
Color cColorFondoPanelTextArea = cVerde; //Asignamos el color de fondo a la variable para poner el color de fondo del panel del �rea de texto.
String sTituloBorderFactoryJTextArea = "MENÚ ELEGIDO"; //Añadimos una variable donde pondremos el texto que aparecerá en el tÃtulo del borde del Ã�rea de texto.
ta = new JTextArea("-- CAFETERIA CANDY -- \n - Elija un menú.",15,40);
ta.setBackground(cColorFondoPanelTextArea);
//Establecemos el borde del Ã�rea de texto y le añadimos un tÃtulo.
ta.setBorder (
BorderFactory.createTitledBorder (
BorderFactory.createBevelBorder (
BevelBorder.LOWERED
),
sTituloBorderFactoryJTextArea
)
); //createEtchedBorder(),"MENU ELEGIDO"));
ta.setEditable(false);
//Añadimos el panel con las barras de desplazamiento al �rea de texto.
jspTextArea = new JScrollPane(ta);
// try
// {
//Ponemos la propieddad de la forma en que se muestran las barras de desplazamiento del �rea de Texto.
jspTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jspTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// }
// catch(IllegalArgumentException iae)
// {
// ta.append("Argumento ilegal");
// }
//Creamos unas variables de tipo String para poner el texto de los botones del panel p4.
String sTextoB1,sTextoB2,sTextoB3,sTextoB4;
sTextoB1 = "SELECCIONAR";
sTextoB2 = "DE ESTO NO QUEDA";
sTextoB3 = "REPONER";
sTextoB4 = "SALIR";
b1 = new JButton(sTextoB1);
b2 = new JButton(sTextoB2);
b3 = new JButton(sTextoB3);
b4 = new JButton(sTextoB4);
b1.setBackground(cAzul);
b2.setBackground(cBlanco);
b3.setBackground(cRojo);
b1.setForeground(cNegro);
b2.setBackground(cBlanco);
b3.setBackground(cRojo);
b1.setMargin(insB1);
b2.setMargin(insB2);
b3.setMargin(insB3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener
(
new ActionListener()
{
public void actionPerformed( ActionEvent aeExit)
{
System.exit(0);
}
}
);
p4.add(b1,BorderLayout.CENTER);
p4.add(b2,BorderLayout.EAST);
p4.add(b3,BorderLayout.WEST);
p4.add(b4,BorderLayout.SOUTH);
p3.add(jspTextArea);// area de texto con deslizadores.
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
this.getContentPane().add(p);
}
public void itemStateChanged(ItemEvent ie)
{
Object obj;
obj=ie.getSource();
if (obj instanceof JCheckBox)
{
for(int i=0;i<iNumTotalPlatos;i++)
{
if((JCheckBox)ie.getItemSelectable()==cb[i])
if(((JCheckBox)obj).isSelected())
activarBotonera(i,variedades[i].length, true);
else
activarBotonera(i,variedades[i].length,false);
}
}
}
private void activarBotonera(int k,int n,boolean sw)
{
for(int i=0; i<n;i++)
{
if(sw && !agotado[k][i])
rb[k][i].setEnabled(true);
else
rb[k][i].setEnabled(false);
}
}
public void actionPerformed(ActionEvent ae)
{
//Declaramos las variables
double tot,x;
int i,j;
int iLongitudPrecioConceptoActualEnCadena,iLongitudPrecioTotalEnCadena,iLongitudIVATotalEnCadena,iLongitudFacturaEnCadena;
NumberFormat formato;
//Creamos una nueva instancia de la clase formato
formato = NumberFormat.getNumberInstance();
//Establecemos las propiedades de la instancia de NumberFormat formato
formato.setMaximumFractionDigits(2);
formato.setMinimumFractionDigits(2);
//formato.setMinimumIntegerDigits(3);
if(ae.getSource() instanceof JButton)
{
if(((JButton)ae.getSource())==b1) // SELECCIONAR
{ // borrar pantalla previa
ta.setText("");
//inicializar variables
tot = 0;
for(i=0;i<iNumTotalPlatos;i++)
{
if(cb[i].isSelected())
for(j=0; j<variedades[i].length;j++)
if(!agotado[i][j] && rb[i][j].isSelected())
{
x = Double.parseDouble(precio[i][j].replace(',', '.'));
iLongitudPrecioConceptoActualEnCadena = formato.format(x).toString().trim().length();
ta.append("Precio de concepto " + (i+1) + ": \t");
if (iLongitudPrecioConceptoActualEnCadena <= 4)
ta.append(" ");
else
if (iLongitudPrecioConceptoActualEnCadena > 5)
ta.append(".");
ta.append(formato.format(x));
ta.append(sMonedaPrecio + " | " + rb[i][j].getText() + " (" + cb[i].getText() + ")." + "\n");
tot += x;
}
}
double iva = tot*.10;
// iLongitudPrecioTotalEnCadena
// iLongitudIVATotalEnCadena
// iLongitudFacturaEnCadena
for (i = 0; i <= 54; i++)
{
ta.append("-");
}
ta.append("|");
ta.append("\n");
ta.append("PRECIO TOTAL");
for (i = 0; i <= 10; i++)
{
ta.append(" ");
}
ta.append("= \t");
iLongitudPrecioTotalEnCadena = formato.format(tot).toString().trim().length();
if (iLongitudPrecioTotalEnCadena <= 4)
ta.append(" ");
else
if (iLongitudPrecioTotalEnCadena > 5)
ta.append(".");
ta.append(formato.format(tot));
ta.append(sMonedaPrecio + " | " + "\n");
ta.append(" IVA (10%)");
for (i = 0; i <= 20; i++)
{
ta.append(" ");
}
ta.append("= \t");
//ta.setForeground(cRojo);
iLongitudIVATotalEnCadena = formato.format(iva).toString().trim().length();
if (iLongitudIVATotalEnCadena <= 4)
ta.append(" ");
else
if (iLongitudIVATotalEnCadena > 5)
ta.append(".");
ta.append(formato.format(iva));
ta.append(sMonedaPrecio + " | " + "\n");
for (i = 0; i <= 54; i++)
{
ta.append("-");
}
ta.append("|");
ta.append("\n");
ta.append("TOTAL FACTURA");
for (i = 0; i <= 5; i++)
{
ta.append(" ");
}
ta.append("= \t");
iLongitudFacturaEnCadena = formato.format(tot+iva).toString().trim().length();
if (iLongitudFacturaEnCadena <= 4)
ta.append(" ");
else
if (iLongitudFacturaEnCadena > 5)
ta.append(".");
ta.append(formato.format(tot+iva));
ta.append(sMonedaPrecio + " | ");
repaint();
ta.setForeground(cAzul);
}
else if(((JButton)ae.getSource()) == b2) // AGOTADO
{
for(i=0;i<iNumTotalPlatos;i++)
if(cb[i].isSelected())
for(j=0; j<variedades[i].length;j++)
if( rb[i][j].isSelected())
{ agotado[i][j] =true;
rb[i][j].setEnabled(false);
}
}
else if(((JButton)ae.getSource()) == b3) // REPONER
{
for(i = 0; i < iNumTotalPlatos; i++)
if( cb[i].isSelected() )
for(j=0; j<variedades[i].length;j++)
{
// reponer todos.
// if(!rb[i][j].isEnabled() && agotado[i][j]) // no se pueden seleccionar si desactivados
// {
agotado[i][j] =false;
rb[i][j].setEnabled(true);
// }
}
}
}
}
public static void main(String[]args)
{
MenuCena mn= new MenuCena();
mn.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(1);
}
}
);
mn.setSize(1000,770);
mn.setResizable(false);
mn.setVisible(true);
}
}
En este caso seria la segunda versión nombrada como MenuCena2 ó versión 2.0
Se han añadido nuevas variedades y los precios han sido modificados.
El programa esta hecho todo en Java.