When i run this program a box appears asking me to input the name of the restaur
ID: 3581043 • Letter: W
Question
When i run this program a box appears asking me to input the name of the restaurant and I dont know what it is it keeps giving me an error please help
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JOptionPane;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
try{
File inputFile = new File(JOptionPane.showInputDialog("Enter the Restaurant "));
new Restaurant(inputFile);
}
catch(FileNotFoundException e) {
JOptionPane.showMessageDialog(null,"No Menu found!", "Please inut again", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error! Termination"
, " Error", JOptionPane.ERROR_MESSAGE);
}
}
}
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
/**
* @author
* This class takes the input file, creates menu items and creates GUI and corresponding methods
*/
public class Restaurant extends JFrame {
private BigDecimal totalCost;
private JPanel panelReceipt;
private JPanel panelCenter;
private JTextField orderPriceText;
private ArrayList <Item> itemsOrdered;
private JTextPane paneOrderedItems;
private String itemInfo;
private ArrayList<Item> menuItems;
private Box menuBox;
public Restaurant(File menu) throws FileNotFoundException {
//initializing cost, item info, ordered items
totalCost = new BigDecimal(0);
itemInfo = "";
itemsOrdered = new ArrayList<Item>();
//reads the menu from input file and stores it in menuItems
readInputFile(menu);
//initializes the mainWindow of the GUI
JPanel mainWindow = (JPanel) getContentPane();
// initializes and populates the menu items on to the GUI using method populateMenu
menuBox = Box.createVerticalBox();
populateMenu(menuBox);
// makes menu scrollable
JScrollPane scroller = formatMenu(menuBox);
//// initializes and creates the receipt on the GUI
panelReceipt = new JPanel();
formatReceipt();
//formats menu and receipt side by side
JSplitPane paneTwoWindow = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroller, panelReceipt);
paneTwoWindow.setDividerLocation(600);
mainWindow.setLayout(new BorderLayout());
mainWindow.add(paneTwoWindow, BorderLayout.CENTER);
setSize(1200,1500);
setTitle("Welcome!");
setBackground(Color.WHITE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void formatReceipt() {
JLabel mainText = new JLabel("Customer Order:");
panelReceipt.setLayout(new BorderLayout());
// divides panel into upper center and lower
JPanel panelLower = new JPanel();
panelLower.setLayout(new BorderLayout());
panelReceipt.add(panelLower,BorderLayout.SOUTH);
panelReceipt.add(mainText, BorderLayout.NORTH);
panelCenter = new JPanel();
panelCenter.setLayout(new GridLayout(0,1));
paneOrderedItems = new JTextPane();
//adds the order items text to center
panelCenter.add(paneOrderedItems);
paneOrderedItems.setEditable(false);
//makes the center scrollable
JScrollPane scrollingPanelCenter = new JScrollPane(panelCenter);
panelReceipt.add(scrollingPanelCenter, BorderLayout.CENTER);
//total cost text formatting
orderPriceText = new JTextField(40);
orderPriceText.setText("Total Cost = $0.00");
orderPriceText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
orderPriceText.setFont(new Font("Courier" , Font.BOLD , 15));
orderPriceText.setEditable(false);
//adding and formatting buttons
JButton placeOrder = new JButton("Place Order");
JButton clearOrder = new JButton("Clear Order");
panelCenter.setBackground(Color.LIGHT_GRAY);
placeOrder.setFont(new Font ("Courier", Font.BOLD,25));
clearOrder.setFont(new Font ("Courier", Font.BOLD,25));
// resetting if clear order
clearOrder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
reset();
}
});
eventListen(placeOrder);
// adding to receipt GUI
panelLower.add(orderPriceText, BorderLayout.NORTH);
panelLower.add(placeOrder, BorderLayout.CENTER);
panelLower.add(clearOrder, BorderLayout.SOUTH);
panelLower.setBackground(Color.LIGHT_GRAY);
panelReceipt.setBackground(Color.WHITE);
}
/** This method formats the menu GUI and makes the menu box scrollable
* @param box
* @return JScrollPane
*/
private JScrollPane formatMenu(Box box) {
JScrollPane scroller = new JScrollPane(box);
Border foramttedBorder = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(foramttedBorder, "Items",TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Lucida", Font.BOLD, 20) , Color.BLACK);
box.setBorder(border);
return scroller;
}
/**
* populates menu on the GUI
* @param box
*/
private void populateMenu(Box box) {
ArrayList<Item> itemButtons = menuItems;
// for each item in the input file
for (final Item itemButton: itemButtons) {
//creates button
final JButton menuItemButton = new JButton();
menuItemButton.setLayout(new BorderLayout());
//sets name, description and price
JLabel itemName = new JLabel(itemButton.getName());
JTextArea itemDescription = new JTextArea(itemButton.getItemDescription());
itemDescription.setLineWrap(true);
itemDescription.setEditable(false);
JLabel itemPrice = new JLabel("$"+itemButton.getCost().toString());
menuItemButton.setToolTipText(itemButton.getName());
//sets positioning on the button
menuItemButton.add(BorderLayout.WEST , itemName);
menuItemButton.add(BorderLayout.EAST, itemPrice);
menuItemButton.add(BorderLayout.SOUTH, itemDescription);
// refresh pane
menuItemButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
refresh(itemButton);
}
});
//add this button to main menu box GUI
box.add(menuItemButton);
menuItemButton.setMaximumSize(new Dimension(Integer.MAX_VALUE , menuItemButton.getMinimumSize().height));
}
}
/**
* Resets the GUI after order is placed
* @param placeOrder
*/
private void eventListen(JButton placeOrder) {
placeOrder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!orderPriceText.getText().equals("Total Cost = $0.00")) {
JOptionPane.showMessageDialog(getContentPane(), "Order has been sent to kitchen", "Order Done", JOptionPane.INFORMATION_MESSAGE);
reset();
}
else {
JOptionPane.showMessageDialog(null,"No items ordered", "Place order", JOptionPane.ERROR_MESSAGE);
}
}
});
}
/**
* reads input file and creates the menu items
* @param menu
* @throws FileNotFoundException
*/
public void readInputFile(File menu) throws FileNotFoundException {
Scanner readMenu = new Scanner(menu);
menuItems = new ArrayList<Item>();
//while EOF
while (readMenu.hasNextLine()) {
//reads the info
String item = readMenu.nextLine();
String itemDescription = readMenu.nextLine();
BigDecimal itemPrice = new BigDecimal(readMenu.nextLine());
//creates the item and adds to the ArrayList
Item createItem = new Item(item, itemDescription, itemPrice);
menuItems.add(createItem);
}
readMenu.close();
}
/**
* resets the GUI and relevant variables to defaults
*/
private void reset() {
orderPriceText.setText("Total Cost = $0.00");
totalCost = new BigDecimal(0);
itemsOrdered.clear();
itemInfo = "";
paneOrderedItems.setText(null);
}
/**
* refresh the items ordered with the clicked menu item details
* @param itemButton
*/
private void refresh(final Item itemButton) {
//gets info of the button
String item = itemButton.getName();
BigDecimal itemPrice = itemButton.getCost();
itemInfo += " " + item + " : $" + itemPrice + " ";
//updates the textpane
paneOrderedItems.setText(itemInfo);
paneOrderedItems.setFont(new Font("Courier" , Font.PLAIN , 15));
//adds to the ordered items list
itemsOrdered.add(itemButton);
//updates total cost
totalCost = totalCost.add(itemPrice) ;
orderPriceText.setText("Total cost = $" + totalCost);
}
}
import java.math.BigDecimal;
/**
* @author
* This Item class stores the details of a menu item. Name, description and Cost.
* It also has a to itemToString method
*/
public class Item {
private String itemName;
private String itemDescription;
private BigDecimal itemPrice;
public Item(String itemName, String itemDescription, BigDecimal itemPrice) {
this.itemDescription = itemDescription;
this.itemName = itemName;
this.itemPrice = itemPrice;
}
public String getName(){
return itemName;
}
public BigDecimal getCost(){
return itemPrice;
}
public String getItemDescription() {
return itemDescription;
}
public String itemToString(){
String info = itemName + " "+itemDescription + " " + itemPrice;
return info;
}
}
Explanation / Answer
in your program its taking input as a file name, so you need to give the Restaurant particulars file name
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.