Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This java file right here I needed modified package dateNight; import cst8132.restaurant.Menu; import cst8132.restaurant.MenuItem; import cst8132.restaurant.Restaurant; import javax.swing.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; public

This java file right here I needed modified

package dateNight; import cst8132.restaurant.Menu; import cst8132.restaurant.MenuItem; import cst8132.restaurant.Restaurant;

import javax.swing.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Random;

public class DoubleDate extends JFrame {

protected ArrayList guests; protected String[] movies; protected String movieTitle; protected int movieTime; protected Restaurant restaurant; protected Menu menu; protected Bill bill;

private JPanel inputPanel; private JPanel guestList; private JLabel addGuestPrompt; private JLabel guestListHeader; private JTextField newGuestName; private JButton addGuest; private JButton letsGo;

private Random random;

public static void main(String[] args) {

DoubleDate date = new DoubleDate("Andrew", "Nina", "Connor", "Lisa"); date.pickAMovie(); date.getShowing();

if (date.movieTime == 10) { date.bill.setHappyHour(); }

date.placeOrder("Andrew", "drinks"); date.placeOrder("Nina", "drinks"); date.placeOrder("Connor", "drinks"); date.placeOrder("Lisa", "drinks"); date.placeOrder("Andrew", "appetizers"); date.placeOrder("Andrew", "entrees"); date.placeOrder("Nina", "entrees"); date.placeOrder("Connor", "entrees"); date.placeOrder("Lisa", "entrees"); date.placeOrder("Connor", "desserts");

System.out.println(date); }

public DoubleDate(String yourName, String... guests) { //initialize array list capacity and add your name this.guests = new ArrayList(4); this.guests.add(0, yourName);

//check to see if any other guests are coming with you if (guests != null) { this.guests.addAll(Arrays.asList(guests)); }

//initialize restaurant restaurant = Restaurant.getInstance("The Waverly");

//initialize a menu menu = restaurant.getMenu();

//populate the menu addMenuItems();

bill = new Bill();

movies = new String[4]; movies[0] = "Snatch"; movies[1] = "The Shawshank Redemption"; movies[2] = "Baby Driver"; movies[3] = "Snowpiercer"; }

public String pickAMovie() { double rando = Math.random();

if (rando < .25) { movieTitle = movies[0]; } else if (rando < .5 && rando >= .25) { movieTitle = movies[1]; } else if (rando < .75) { movieTitle = movies[2]; } else if (rando < 1) { movieTitle = movies[3]; }

return movieTitle; }

// Flips a coin to decide what show time the group is going to see.

public int getShowing() { double coinFlip = Math.random();

if (coinFlip <= .5) { movieTime = 6; } else { movieTime = 10; }

return movieTime;

}

// method populates the restaurant menu.

public void addMenuItems() {

menu.addMenuItem("Drinks", "Coke", 6); menu.addMenuItem("Drinks", "Beer", 8); menu.addMenuItem("Drinks", "Wine", 10); menu.addMenuItem("Drinks", "Sparkling Water", 4);

menu.addMenuItem("Appetizers", "Spinach Dip", 14); menu.addMenuItem("Appetizers", "Wings", 16); menu.addMenuItem("Appetizers", "Nachos", 16); menu.addMenuItem("Appetizers", "Cheese Sticks", 12);

menu.addMenuItem("Entrees", "Burger", 16); menu.addMenuItem("Entrees", "Club Sandwich", 16); menu.addMenuItem("Entrees", "Chicken Salad", 14); menu.addMenuItem("Entrees", "Striploin Steak", 28);

menu.addMenuItem("Desserts", "Tiramisu", 14); menu.addMenuItem("Desserts", "Chocolate brownie", 12); menu.addMenuItem("Desserts", "Gelato", 14); menu.addMenuItem("Desserts", "Churros", 14);

}

//THe placeOrder method is used to generate a random order and add it to the bill.

public boolean placeOrder(String guest, String itemtype) { MenuItem item = menu.getRandomMenuItem(itemtype); return bill.addOrderItem(guest, item); }

@Override public String toString() {

if (movieTime == 6) { return "Movies: " + " " + movies[0] + " " + movies[1] + " " + movies[2] + " " + movies[3] + " " + " " + "You have" + " chosen " + this.movieTitle + "! " + "We will be meeting at " + this.restaurant.getName() + " after the movie." + " " + "We will be missing happy hour, but we will still be happy! " + "MENU: " + menu.toString() + " " + bill.toString(); } else { return "Movies: " + " " + movies[0] + " " + movies[1] + " " + movies[2] + " " + movies[3] + " " + " " + "You have" + " chosen " + this.movieTitle + "! " + "We will be meeting at " + this.restaurant.getName() + " before the movie. " + "It's happy hour! $2 off drinks, and 1/2 price appetizers!! " + "MENU: " + menu.toString() + " " + bill.toString(); } }

}

Can someone please modify this with this information

Do NOT use NetBeans or another Swing/GUI IDE for this. Please use a code editor to write the code manually.

Import classes from javax.swing and java.awt packages as needed. Only import the classes you use in your application.

Modify the DoubleDate class to extend the JFrame class.

Declare the following instance variables:

JPanel inputPanel

JPanel guestList

JLabel addGuestPrompt

JLabel guestListHeader

JTextField newGuestName

JButton addGuest

JButton letsGo

Modify the DoubleDate constructor to configure the GUI components.

Remove the parameters.

Remove the addition of names to the guests ArrayList. Names will now be added through the GUI.

Call the JFrame constructor by calling the super constructor and pass the title Double Date as a String argument.

Set the layout of the DoubleDate by calling the setLayout method, which is inherited from the JFrame class.

Set the layout to be a new GridLayout with 2 rows and 2 columns.

Instantiate addGuestPrompt with the text Enter a guest name:. Using the add method which DoubleDate inherits from JFrame, add addGuestPrompt to the JFrame layout.

Instantiate guestListHeader with the text Guest List. Using the add method which DoubleDate inherits from JFrame, add guestListHeader to the JFrame layout.

Instantiate inputPanel as a new JPanel. Set the layout of this JPanel to a new FlowLayout using its setLayout method.

Instantiate the JTextField object as a new JTextField with a size of 20.

Add the JTextField object to the inputPanel, using the add method of the inputPanel.

Instantiate the addGuest button as a new JButton with the text Add Guest to List. Add it to the inputPanel.

Instantiate the letsGo button as a new JButton with the text Lets Go Out!. Add it to the inputPanel.

Set the visibility of the letsGo button to false. It should only become visible when there is at least one guest on the Guest List.

Add the inputPanel to the DoubleDate JFrame layout.

Instantiate the guestList as a new JPanel. Set its layout as a new FlowLayout, and add it to the DoubleDate JFrame layout.

Set the size and default close operation of the DoubleDate JFrame using the following lines of code:

setSize(500, 250);

The layout of the GUI is complete, however it will not appear or do anything yet.

Add new method to the DoubleDate class: public void goOnDate(DoubleDate date)

Copy everything from the existing main method into the goOnDate method, except for the declaration of the DoubleDate object (the declaration should stay in the main method).

Update the main method:

In the main method, there should be a declaration of a new DoubleDate object, and we will now make the DoubleDate GUI visible.

DoubleDate date = new DoubleDate();

date.setVisible(true);

Add the JFrame paint method:

public void paint( Graphics g ) {

super.paint( g );

Add Action Listeners

Each JButton needs an ActionListener to process button click events.

To learn both methods, one action listener will be implemented as a nested private class and the other action listener will be defined as an anonymous class.

private class AddGuestHandler implements ActionListenerpublic void actionPerformed(ActionEvent e)

Get the guest name using the getText() method of the newGuestName JTextField object.

Add the new guest name to the guests ArrayList.

Reset the value of the newGuestName JTextField to an empty String using the setText() method.

Add a new JLabel to the guestList JPanel containing the new guest name.

Set the visibility of the letsGo JButton to true, as we now have at least one guest.

By passing a condition to the setVisible(boolean b) method of the addGuest JButton, set the visibility to true if there are less than 4 guests or false if there are 4 or more guests.

Use the ArrayList size() method to determine the number of guests.

Do NOT declare a boolean variable.

Because we have added new components to the guestList JPanel, we need to validate it.

Because the components of our JFrame have changed, it must be repainted. However, we can repaint the guestList JPanel alone, as it is the only piece that has changed. This is more optimal than repainting the entire JFrame.

Add a new instance of the AddGuestHandler class to the addGuest JButton using the addActionListener(ActionListener l) method.

For the letsGo JButton, we will define an anonymous class inside of the addActionListener(ActionListener l) method.

In the DoubleDate constructor, after the letsGo JButton is instantiated, add the following lines of code:

letsGo.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent e ) {

goOnDate((DoubleDate) SwingUtilities.getRoot(

(Component) e.getSource()));

} );

In the actionPerformed method:

The first line gets the root JFrame Component of the Component which was the source of the ActionEvent. In this case, that root Component happens to be the DoubleDate object, so we can cast it as such.

We then pass the root DoubleDate JFrame to the goOnDate method, which takes a DoubleDate object as a parameter.

Then we set the visibility of the JFrame to false, so it disappears from the screen.

Finally, we dispose of the JFrame object, removing it from memory and terminating the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

What does serializability of transactions mean?

Answered: 1 week ago