多重预订系统 [英] Multiple Bookings System
问题描述
PS:我没有使用数据库,我不喜欢;由于任务的要求。
这是我的代码,请运行它,如果可以的话。
code> // CM1203 Java的计算机基础知识第二分配。
// Walter Carvalho - C1001984;
//加的夫大学
//加载库
import java.awt。*;
import java.awt.event。*;
import javax.swing。*;
import java.util。*;
import java.text。*;
public class cinemaSystem extends JFrame implements ActionListener {
//全局变量
boolean lselected,rselected,mselected;
double chargeDue;
int a,b,c;
static Ticket oapticket,childticket,adultticket;
JFrame frame = new JFrame(); //创建JFrame
JLabel标题,lchild,ladult,loap,ltotalprice,time;
JTextField儿童,成人,oap,totalprice;
JButton提交;
JRadioButton time1,time2,time3,time4,time5; // Radio Butons
JToggleButton l [] [],m [] [],r [] []; //命名网格JButtons
JPanel panel1,panel2,panel3,panel4,panel5,panel6;
ArrayList< String> seatsAvailable = new ArrayList< String>();
ArrayList< String> coupon = new ArrayList< String>();
//构造函数
public cinemaSystem(){
title = new JLabel(Cinema Booking System);
title.setFont(new Font(Helvetica,Font.BOLD,30));
title.setLocation(12,5);
title.setSize(600,60);
frame.setLayout(null); //设置网格布局
// panel1.setLayout(new GridLayout(seat,row));
l = new JToggleButton [4] [4]; //分配网格的大小
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBounds(20,95,220,140); (int y = 0; y <4; y ++){
for(int x = 0; x< 4; x ++){
l [x] [y] = new JToggleButton(L+ y + x); //创建新的JButton
l [x] [y] .addActionListener(this);
seatsAvailable.add(L+ y + x);
panel1.add(l [x] [y]); //将按钮添加到网格
}
}
m = new JToggleButton [4] [2]; //分配网格的大小
panel2 = new JPanel(new GridLayout(2,4));
panel2.setBounds(240,165,220,70); (int y = 0; y <2; y ++){
for(int x = 0; x <4; x ++){
m [x] [y] = new JToggleButton(M+ y + x); //创建新的JButton
m [x] [y] .addActionListener(this);
seatsAvailable.add(M+ y + x);
panel2.add(m [x] [y]); //将按钮添加到网格
}
}
r = new JToggleButton [4] [4]; //分配网格的大小
panel3 = new JPanel(new GridLayout(4,4));
panel3.setBounds(460,95,220,140); (int y = 0; y< 4; y ++){
for(int x = 0; x< 4; x ++){
r [x] [y] = new JToggleButton(R+ y + x); //创建新的JButton
r [x] [y] .addActionListener(this);
seatsAvailable.add(R+ y + x);
panel3.add(r [x] [y]); //将按钮添加到网格
}
}
panel4 = new JPanel(new FlowLayout());
panel4.setBounds(0,25,300,70);
lchild = new JLabel(Child);
child = new JTextField(0,2);
child.addActionListener(this);
ladult = new JLabel(Adult);
adult = new JTextField(0,2);
adult.addActionListener(this);
loap = new JLabel(OAP);
oap = new JTextField(0,2);
oap.addActionListener(this);
submit = new JButton(Submit);
submit.addActionListener(this);
oapticket =新票(4.70);
childticket =新票(3.50);
adultticket =新票(6.10);
child.addKeyListener(new MyKeyAdapter());
oap.addKeyListener(new MyKeyAdapter());
adult.addKeyListener(new MyKeyAdapter());
panel4.add(lchild);
panel4.add(child);
panel4.add(ladult);
panel4.add(成人);
panel4.add(loap);
panel4.add(oap);
panel4.add(submit);
panel5 = new JPanel(new FlowLayout());
panel5.setBounds(240,250,300,70);
ltotalprice = new JLabel(Charge Due(£):);
totalprice = new JTextField(£0.00,5);
totalprice.setEnabled(false);
panel5.add(ltotalprice);
panel5.add(totalprice);
panel6 = new JPanel(new FlowLayout());
panel6.setBounds(0,55,560,30);
time = new JLabel(请选择电影时间:);
time1 = new JRadioButton(13:00,true);
time2 = new JRadioButton(15:00,false);
time3 = new JRadioButton(17:00,false);
time4 = new JRadioButton(19:00,false);
time5 = new JRadioButton(21:00,false);
ButtonGroup group = new ButtonGroup();
group.add(time1);
group.add(time2);
group.add(time3);
group.add(time4);
group.add(time5);
panel6.add(time);
panel6.add(time1);
panel6.add(time2);
panel6.add(time3);
panel6.add(time4);
panel6.add(time5);
time1.addActionListener(this);
time2.addActionListener(this);
time3.addActionListener(this);
time4.addActionListener(this);
time5.addActionListener(this);
frame.add(title);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
frame.add(panel5);
frame.add(panel6);
frame.setPreferredSize(new Dimension(700,350));
frame.setTitle(Cinema Booking);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //为框架设置适当的大小
frame.setVisible(true); //使框架可见
}
//计算当前交易的到期费用。
public double calcChargeDue(){
DecimalFormat df = new DecimalFormat(#。##);
double chargeDue = 0.0;
chargeDue =(a * childticket.price)+(b * oapticket.price)+(c * adultticket.price);
totalprice.setText(£+ df.format(chargeDue));
return chargeDue;
}
//检查人数是否与所选席位数相匹配的方法。
public void check(){
int check = coupon.size();
int noTickets = a + b + c;
if(check!= noTickets){
submit.setEnabled(false);
}
else {
submit.setEnabled(true);
}
}
// Ticket对象
public class Ticket {
double price;
//构造函数
public Ticket(double cost){
price = cost;
}
public double getTicketPrice(){
return price;
}
}
public void actionPerformed(ActionEvent e)
{
a = Integer.parseInt(child.getText ());
b = Integer.parseInt(oap.getText());
c = Integer.parseInt(adult.getText()); (int x = 0; x <4; x ++){
(int y = 0; y< 4; y ++){
for lselected = l [x] [y] .isSelected();
rselected = r [x] [y] .isSelected();
if(e.getSource()== l [x] [y]){
if(lselected == true){
coupon.add(e.getActionCommand ));
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if(e.getSource()== r [x] [y]){
if(rselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if(e.getSource()== oap){
calcChargeDue();
check();
}
if(e.getSource()== adult){
calcChargeDue();
check();
}
if(e.getSource()== child){
calcChargeDue();
check(); (int y = 0; y <2; y ++){
为(int x)
$ b}
for = 0; x <4; x ++){
mselected = m [x] [y] .isSelected();
if(e.getSource()== m [x] [y]){
if(mselected == true){
coupon.add(e.getActionCommand ));
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
}
}
if(time1 == e.getSource()){
}
if(time2 == e.getSource()){
}
if(time3 == e.getSource()){
}
if(time4 == e.getSource()){
}
if(time5 == e .getSource()){
}
if(submit == e.getSource()){
for(int y = 0;对于(int x = 0; x <4; x ++){
lselected = l [x] [y] .isSelected(); y< 4; y ++){
rselected = r [x] [y] .isSelected();
if(lselected == true){
l [x] [y] .setEnabled(false);
}
if(rselected == true){
r [x] [y] .setEnabled(false); (int y = 0; y <2; y ++){
为(int x)
$ b}
for = 0; x <4; x ++){
mselected = m [x] [y] .isSelected();
if(mselected == true){
m [x] [y] .setEnabled(false);
}
}
}
Collections.sort(coupon);
System.out.println(这是你的票:);
System.out.println(coupon);
System.out.println(Child:+ child.getText());
System.out.println(Adult:+ adult.getText());
System.out.println(OAP:+ oap.getText());
System.out.println(Total Price:+ totalprice.getText());
System.out.println(谢谢,享受你的电影);
System.out.println();
coupon.clear();
child.setText(0);
adult.setText(0);
oap.setText(0);
}
}
//主类
public static void main(String [] args){
new cinemaSystem(); //使新的ButtonGrid有两个参数
}
}
相关: a href =http://stackoverflow.com/questions/10092353/java-disable-all-jtogglebuttons-after-submission-setenabledfalse> Java:在提交后禁用所有JToggleButton - setEnabled(false);
如果您已经有一个预定时间工作,您需要做的就是将所有数据结构用于存储信息那个预订时间和双倍的时间来支持几个独立的预订时间。
例如, ArrayList< String> SeatingAvailable = new ArrayList< String>();
将成为:
字典< Time,ArrayList<字符串> > seatsAvailable =
新词典< Time,ArrayList< String> >();
时间firstBooking =新时间(13,0,0);
时间secondBooking =新时间(15,0,0);
seatsAvailable.put(firstBooking,new ArrayList< String>());
seatsAvailable.put(secondBooking,new ArrayList< String>());
现在您可以跟踪两个完全独立的座位数组列表。
I am currently developing a booking system as a task for my degree. I need the seating plan to be "saved/stored" for each showing time (radio buttons), so that e.g. If I book 2 tickets at 13:00, I can also book 2 tickets on the same spot for 15:00. What is the best way of doing this?
PS: I'm not making use of a database and I would prefer not to; due to task's requirements.
Here's my code, please run it if you can.
// CM1203 Fundamentals of Computing with Java; Second Assignement.
// Walter Carvalho - C1001984; 2012.
// Cardiff University
// Load Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class cinemaSystem extends JFrame implements ActionListener {
// Global Variables
boolean lselected, rselected, mselected;
double chargeDue;
int a, b, c;
static Ticket oapticket, childticket, adultticket;
JFrame frame = new JFrame(); // Creates JFrame
JLabel title, lchild, ladult, loap, ltotalprice, time;
JTextField child, adult, oap, totalprice;
JButton submit;
JRadioButton time1, time2, time3, time4, time5; // Radio Butons
JToggleButton l[][], m[][], r[][]; // Names grid of JButtons
JPanel panel1, panel2, panel3, panel4, panel5, panel6;
ArrayList<String> seatsAvailable = new ArrayList<String>();
ArrayList<String> coupon = new ArrayList<String>();
// Constructor
public cinemaSystem(){
title = new JLabel("Cinema Booking System");
title.setFont(new Font("Helvetica", Font.BOLD, 30));
title.setLocation(12,5);
title.setSize(600, 60);
frame.setLayout(null); // Setting Grid Layout
// panel1.setLayout(new GridLayout(seat,row));
l = new JToggleButton[4][4]; // Allocating Size of Grid
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBounds(20, 95, 220, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
l[x][y] = new JToggleButton("L" + y + x); // Creates New JButton
l[x][y].addActionListener(this);
seatsAvailable.add("L" + y + x);
panel1.add(l[x][y]); //adds button to grid
}
}
m = new JToggleButton[4][2]; // Allocating Size of Grid
panel2 = new JPanel(new GridLayout(2,4));
panel2.setBounds(240, 165, 220, 70);
for(int y = 0; y <2 ; y++){
for(int x = 0; x < 4; x++){
m[x][y] = new JToggleButton("M" + y + x); // Creates New JButton
m[x][y].addActionListener(this);
seatsAvailable.add("M" + y + x);
panel2.add(m[x][y]); //adds button to grid
}
}
r = new JToggleButton[4][4]; // Allocating Size of Grid
panel3 = new JPanel(new GridLayout(4,4));
panel3.setBounds(460, 95, 220, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
r[x][y] = new JToggleButton("R" + y + x); // Creates New JButton
r[x][y].addActionListener(this);
seatsAvailable.add("R" + y + x);
panel3.add(r[x][y]); //adds button to grid
}
}
panel4 = new JPanel(new FlowLayout());
panel4.setBounds(0, 250, 300, 70);
lchild = new JLabel("Child");
child = new JTextField("0", 2);
child.addActionListener(this);
ladult = new JLabel("Adult");
adult = new JTextField("0", 2);
adult.addActionListener(this);
loap = new JLabel("OAP");
oap = new JTextField("0", 2);
oap.addActionListener(this);
submit = new JButton("Submit");
submit.addActionListener(this);
oapticket = new Ticket(4.70);
childticket = new Ticket(3.50);
adultticket = new Ticket(6.10);
child.addKeyListener(new MyKeyAdapter());
oap.addKeyListener(new MyKeyAdapter());
adult.addKeyListener(new MyKeyAdapter());
panel4.add(lchild);
panel4.add(child);
panel4.add(ladult);
panel4.add(adult);
panel4.add(loap);
panel4.add(oap);
panel4.add(submit);
panel5 = new JPanel(new FlowLayout());
panel5.setBounds(240, 250, 300, 70);
ltotalprice = new JLabel("Charge Due (£): ");
totalprice = new JTextField("£0.00", 5);
totalprice.setEnabled(false);
panel5.add(ltotalprice);
panel5.add(totalprice);
panel6 = new JPanel(new FlowLayout());
panel6.setBounds(0, 55, 560, 30);
time = new JLabel("Please select a film time: ");
time1 = new JRadioButton("13:00", true);
time2 = new JRadioButton("15:00", false);
time3 = new JRadioButton("17:00", false);
time4 = new JRadioButton("19:00", false);
time5 = new JRadioButton("21:00", false);
ButtonGroup group = new ButtonGroup();
group.add(time1);
group.add(time2);
group.add(time3);
group.add(time4);
group.add(time5);
panel6.add(time);
panel6.add(time1);
panel6.add(time2);
panel6.add(time3);
panel6.add(time4);
panel6.add(time5);
time1.addActionListener(this);
time2.addActionListener(this);
time3.addActionListener(this);
time4.addActionListener(this);
time5.addActionListener(this);
frame.add(title);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
frame.add(panel5);
frame.add(panel6);
frame.setPreferredSize(new Dimension(700, 350));
frame.setTitle("Cinema Booking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
// Calculates Charge Due for current transaction.
public double calcChargeDue(){
DecimalFormat df = new DecimalFormat("#.##");
double chargeDue = 0.0;
chargeDue = (a*childticket.price) + (b*oapticket.price) + (c*adultticket.price);
totalprice.setText("£"+df.format(chargeDue));
return chargeDue;
}
// Method to check if the number of people matches the number of seats selected.
public void check(){
int check = coupon.size();
int noTickets = a + b + c;
if (check != noTickets){
submit.setEnabled(false);
}
else {
submit.setEnabled(true);
}
}
// Ticket Object
public class Ticket {
double price;
// Constructor
public Ticket(double cost) {
price = cost;
}
public double getTicketPrice() {
return price;
}
}
public void actionPerformed(ActionEvent e)
{
a = Integer.parseInt(child.getText());
b = Integer.parseInt(oap.getText());
c = Integer.parseInt(adult.getText());
for(int y = 0; y < 4; y++){
for(int x = 0; x < 4; x++){
lselected = l[x][y].isSelected();
rselected = r[x][y].isSelected();
if (e.getSource() == l[x][y]) {
if(lselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if (e.getSource() == r[x][y]) {
if(rselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if (e.getSource() == oap){
calcChargeDue();
check();
}
if (e.getSource() == adult){
calcChargeDue();
check();
}
if (e.getSource() == child){
calcChargeDue();
check();
}
}
}
for(int y = 0; y < 2; y++){
for(int x = 0; x < 4; x++){
mselected = m[x][y].isSelected();
if (e.getSource() == m[x][y]) {
if(mselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
}
}
if(time1 == e.getSource()){
}
if(time2 == e.getSource()){
}
if(time3 == e.getSource()){
}
if(time4 == e.getSource()){
}
if(time5 == e.getSource()){
}
if(submit == e.getSource()) {
for(int y = 0; y < 4; y++){
for(int x = 0; x < 4; x++){
lselected = l[x][y].isSelected();
rselected = r[x][y].isSelected();
if (lselected == true) {
l[x][y].setEnabled(false);
}
if (rselected == true) {
r[x][y].setEnabled(false);
}
}
}
for(int y = 0; y < 2; y++){
for(int x = 0; x < 4; x++){
mselected = m[x][y].isSelected();
if (mselected == true) {
m[x][y].setEnabled(false);
}
}
}
Collections.sort(coupon);
System.out.println("Here's your ticket:");
System.out.println(coupon);
System.out.println("Child: " + child.getText());
System.out.println("Adult: " + adult.getText());
System.out.println("OAP: " + oap.getText());
System.out.println("Total Price: " + totalprice.getText());
System.out.println("Thank you. Enjoy your film.");
System.out.println(" ");
coupon.clear();
child.setText("0");
adult.setText("0");
oap.setText("0");
}
}
// Main Class
public static void main(String[] args) {
new cinemaSystem(); //makes new ButtonGrid with 2 parameters
}
}
Related: Java: Disable all JToggleButtons after Submission — setEnabled(false);
If you already have it working for one booking time, all you need to do is take all your data structures used for storing information about that booking time and double them up to support several independent booking times.
For instance, ArrayList<String> seatsAvailable = new ArrayList<String>();
will become:
Dictionary<Time, ArrayList<String> > seatsAvailable =
new Dictionary<Time, ArrayList<String> >();
Time firstBooking = new Time(13,0,0);
Time secondBooking = new Time(15,0,0);
seatsAvailable.put( firstBooking , new ArrayList<String>() );
seatsAvailable.put( secondBooking , new ArrayList<String>() );
Now you can keep track of two completely seperate ArrayLists of seatsAvailable.
这篇关于多重预订系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!