JFrame and JOptionPane
hi guy,
Thank you for taking your time to solve my problem. I doing a project, what my project does is that when a customer come to the shop to rent the movie. If the movie found and the customer want to rent the movie so it have to key in the customer detail using GUI(using JFrame) and it ask customer how many day do they want to rent (using JOptionPane)and then it display the movie, cost and customer after the display the movie status change from "available" to "rented out".
Here is my problem:
1) When customer want to rent the movie it load GUI to enter the customer detail then follow by JOptionPane to ask the customer how many day to rent. But it load both the GUI and JOptionPane together on the screen. How do I load the GUI and enter the customer detail, when the user click "OK" and close then it load the JOptionPane.
here is the driver file(DvdUI.java):
import javax.swing.JOptionPane;
import javax.swing.JFrame;
class DvdUI
{
public static void main(String args[])
{
String movieTitle, output="", name, address, phone;
final String title = "Odyssey Film Rental Pte Ltd";
Movie movie;
Customer customer;
int pressed;
DvdAdmin admin = new DvdAdmin();
JFrame GUI = new GuiTesting();
do
{
do
{
movieTitle = JOptionPane.showInputDialog(null,"Enter the movie title: ", title
, JOptionPane.PLAIN_MESSAGE);
movie = admin.findMovieTitle(movieTitle);
}while(movie==null);
output = "" + admin.showMovieDetails(movieTitle);
pressed = JOptionPane.showConfirmDialog(null, output +"\n\nDo you want to rental this movie? ",
title,JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}while(pressed == JOptionPane.NO_OPTION);
if (pressed == JOptionPane.YES_OPTION)
{
movie.setStatus("Rented out");
//JFrame GUI = new GuiTesting();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
//GUI.setFocusable(true);
String input = JOptionPane.showInputDialog(null, "Please enter how many day you wish to rent: ");
}
}
}
here is the class file (GuiTesting.java):
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GuiTesting extends JFrame
{
private static final int width = 500;
private static final int height = 175;
private JLabel nameLabel, addressLabel, phoneLabel;
private JTextField nameTextField, addressTextField, phoneTextField;
private JButton okButton, clearButton;
private OkButtonHandler obHandler;
//private ClearButtonHandler cbHandler;
public GuiTesting()
{
setTitle("Customer Detail");
setSize(width,height);
Container pane = getContentPane();
GridLayout Layout = new GridLayout(4,3);
pane.setLayout(Layout);
nameLabel = new JLabel("Name: ");
addressLabel = new JLabel("Address: ");
phoneLabel = new JLabel("Phone: ");
nameTextField = new JTextField(30);
addressTextField = new JTextField(50);
phoneTextField = new JTextField(8);
okButton = new JButton("OK");
obHandler = new OkButtonHandler();
okButton.addActionListener(obHandler);
clearButton = new JButton("CLEAR");
//add components to the pane
pane.add(nameLabel);
pane.add(nameTextField);
pane.add(addressLabel);
pane.add(addressTextField);
pane.add(phoneLabel);
pane.add(phoneTextField);
pane.add(okButton);
pane.add(clearButton);
}//end constructor
private class OkButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name;
DvdAdmin admin = new DvdAdmin();
name = nameTextField.getText();
admin.printCustomerName(name);
//String address;
//address = addressTextField.getText();
//String phone;
//phone = phoneTextField.getText();
}//method
}//class AcceptButtonHander
}//class GuiTesting
Sorry to take your time.......Thank you
Message was edited by:
Kleave
[4416 byte] By [
Kleavea] at [2007-11-26 15:58:08]

# 5
Thank for your help.....it working fine.......I got one problem.........
after enter the customer detail and it print the receipt( Example of movie title is crank ). Then after the printing the receipt the movie status change to 'rented out' . When serving the next customer, when enter the customer detail in GUI and click 'ok' it reset my crank movie status to 'available', by right should be 'rented out'. How to I stop the status from be reset to 'available'. Can someone help me out. Thank.......
Here is the Movie class:
public class Movie
{
private String movieTitle, catalogue, category, status;
private double cost;
public Movie(String cata, String bMovieTitle, double bCost, String bCategory, String bStatus)
{
movieTitle = bMovieTitle;
catalogue = cata;
cost = bCost;
category = bCategory;
status = bStatus;
System.out.println(movieTitle + "\t" + status);
}
public String getMovieTitle()
{
return movieTitle;
}
public String getCategory()
{
return category;
}
public String getCatalogue()
{
return catalogue;
}
public double getCost()
{
return cost;
}
public String getStatus()
{
return status;
}
public void setStatus(String newStatus)
{
status = newStatus;
System.out.println(movieTitle + "\t" + status);
}
public String toString()
{
String msg;
msg = "DVD title: " + movieTitle +
"\nCatalogue No: " + catalogue +
"\nCategory: " + category +
"\nCost: $" + cost +
"\nStatus: " + status;
return msg;
}
}
Here is my DVDAdmin class:
import javax.swing.JOptionPane;
public class DvdAdmin
{
private Movie[] movie;
private Customer customer;
public DvdAdmin()
{
movie = new Movie[15];
movie[0] = new Movie("C0001", "The OH in Ohio", 10, "Comedy", "Available");
movie[1] = new Movie("C0002", "Bring It On", 10, "Comedy", "Available");
movie[2] = new Movie("A0001", "Shrek", 5, "Aimation", "Available");
movie[3] = new Movie("A0002", "Joseph: King of Dreams", 5, "Aimation", "Available");
movie[4] = new Movie("A0003", "Barney's Great Adventure", 5, "Aimation", "Available");
movie[5] = new Movie("A0004", "Ice Age 2", 5, "Aimation", "Available");
movie[6] = new Movie("A0005", "A Bug's Life", 8, "Aimation", "Available");
movie[7] = new Movie("A0006", "We're back", 5,"Aimation", "Available");
movie[8] = new Movie("A0007", "Flushed Away", 10, "Aimation", "Available");
movie[9] = new Movie("A0008", "Over the Hedge", 5, "Aimation", "Available");
movie[10] = new Movie("T0001", "Operation Undercover", 10, "Thriller", "Available");
movie[11] = new Movie("T0002", "Inside Man", 8, "Thriller", "Available");
movie[12] = new Movie("T0003", "Hannibal", 5, "Thriller", "Available");
movie[13] = new Movie("T0004", "Crank", 10, "Thriller", "Available");
movie[14] = new Movie("T0005", "The Guardian", 10, "Thriller", "Available");
}
public Movie findMovieTitle(String bMovie)
{
int sizeOfMovie = movie.length;
String msg;
for( int i = 0; i < sizeOfMovie; i++)
{
if (movie.getMovieTitle().equalsIgnoreCase(bMovie))
{
return movie;
}//if
}//for
JOptionPane.showMessageDialog(null, "Movie title: " + bMovie + " not found",
"Odyssey Film Rental Pte Ltd", JOptionPane.INFORMATION_MESSAGE );
return null;
}//Movie
public String showMovieDetails(String bMovie)
{
Movie movie;
movie = findMovieTitle(bMovie);
return movie.toString();
}
public void NewCustomer(String cName, String cAddress, String cPhone)
{
Customer cus = new Customer(cName, cAddress, cPhone);
//cus(cName, cAddress, cPhone);
}
public String showCustomerDetail()
{
return customer.toString();
}
}
here is my customer class:
class Customer
{
private String name, address, phone;
public Customer(String aName, String aAddress, String aPhone)
{
name = aName;
address= aAddress;
phone= aPhone;
System.out.println(name + "\t" + address + "\t" + phone);
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getPhone()
{
return phone;
}
public String toString()
{
String output;
output = "Customer name: " + name + "\n" +
"Customer address: " + address + "\n" +
"Customer Number: " + phone;
return output;
}
}