import javax.swing.*; import java.awt.*; import java.awt.event.*;

Similar documents
Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Chapter 13 Lab Advanced GUI Applications

Building Graphical User Interfaces. GUI Principles

Building Graphical User Interfaces. Overview

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

JLayeredPane. Depth Constants in JLayeredPane

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Final Examination Semester 2 / Year 2010

Window Interfaces Using Swing Objects

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

SampleApp.java. Page 1

A Simple Text Editor Application

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Today. cisc3120-fall2012-parsons-lectiii.3 2

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

H212 Introduction to Software Systems Honors

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Final Examination Semester 2 / Year 2012

Window Interfaces Using Swing Objects

Final Examination Semester 2 / Year 2011

Attempt FOUR questions Marking Scheme Time: 120 mins

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

State Application Using MVC

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Using Several Components

Inheritance (cont) Abstract Classes

CS 3331 Advanced Object-Oriented Programming. Final Exam

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

CSIS 10A Assignment 7 SOLUTIONS

Client-side GUI. A simple Swing-gui for searching for proudcts

Implementing Graphical User Interfaces

protected void printserial() { System.out.println("> NO." + this.serialno); this.serialno++; }

Introduction to the JAVA UI classes Advanced HCI IAT351

Layouts and Components Exam

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Graphical User Interfaces 2

COMP 102: Test 2 Model Solutions

Lösningsförslag till exempeldugga

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

JAVA NOTES GRAPHICAL USER INTERFACES

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Programmierpraktikum

1 Looping Constructs (4 minutes, 2 points)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

University of Cape Town Department of Computer Science Computer Science CSC1017F

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Event Driven Programming

MIT AITI Swing Event Model Lecture 17

encompass a group of features for building Graphical User Interfaces (GUI).

We are on the GUI fast track path

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

RAIK 183H Examination 2 Solution. November 10, 2014

Part 3: Graphical User Interface (GUI) & Java Applets

CMP 326 Midterm Fall 2015

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Window Interfaces Using Swing. Chapter 12

COMP16121 Sample Code Lecture 1

Part I: Learn Common Graphics Components

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS Exam 3 - Spring 2010

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

More Swing. CS180 Recitation 12/(04,05)/08

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

RobotPlanning.java Page 1

MainWindow.java. Page 1

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

DM503 Programming B. Peter Schneider-Kamp.

/* Write a Program implementing GUI based Calculator using Swing */

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

DM503 Programming B. Peter Schneider-Kamp.

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

1 (6) Lösningsförslag Objektorienterade applikationer Provdatum Program DAI 2 Läsår 2016/2017, lp 3. Uppgift 1 (1+1+1 p) a)

FirstSwingFrame.java Page 1 of 1

Graphical User Interfaces 2

Abstract, Interface, GUIs. Ch. 11 & 16

Points Missed on Page page 1 of 8

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

// Class fully inheritance and Person immutable GUI nun and Gridbag Layout import java.awt.*; import java.awt.event.*; import javax.swing.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1005ICT Object Oriented Programming Lecture Notes

Java. GUI building with the AWT

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

CS Exam 1 Review Suggestions

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Transcription:

I need to be walked through with why the stocks are being recognized "half way." They will print out in the console but won't be recognized by certain code. Every line of code seems to look right and that's where I'm confused. import javax.swing.; import java.awt.; import java.awt.event.; This class builds the GUI window for the Inventory Program and contains the main method to get the program started. public class GUI implements ActionListener { private static GUI window; // Link to the GUI object, once created Call this to get the whole thing started. Creates GUI and Inventory objects. @param args not used. public static void main(string[] args) { window = new GUI(new Inventory()); Prints the given string to the GUI's text area. @param message the text to print. public static void println(string msg) { if (window!= null) { window.display(msg + "\n"); private Inventory inventory; // Link to the Inventory object private JFrame frame; // The window itself private JTextArea output; // The window's text area

private JButton stock; // Button to stock a product private JButton sell; // Button to sell a product private JButton print; // Button to print data on all products private JButton price; // Button to check the price of a product private JButton total; // Button to compute the total value of products private JButton purge; // Button to remove all products with quantities of 0 private JMenuItem cleardisplay; // Menu option to clear the text area private JMenuItem quit; // Menu option to end the program private JMenuItem add; // Menu option to add a new product private JMenuItem remove; // Menu option to delete a product Constructor! @param inventory an Inventory object public GUI(Inventory inventory) { this.inventory = inventory; inventory.addproduct("shoes", 20.25); inventory.addproduct("socks", 5.50); inventory.addproduct("pants", 89.99); inventory.addproduct("shirt", 49.00); makeframe(); Displays the given string in the text area. @param message the text to display. public void display(string msg) { output.append(msg); Clears all text from the text area. public void clear() { output.settext(""); Handles all events (button and menu item clicks).

@param e the event that occurred. @Override public void actionperformed(actionevent e) { Object event = e.getsource(); if (event.equals(remove)) { // Remove option clicked String name = JOptionPane.showInputDialog(frame, "Enter old product name: "); if (name!= null) { double price = getdouble("enter individual product price: "); inventory.removeproduct(name, price); else if (event.equals(total)) { // Compute Total Value button clicked inventory.totalvalue(); else if (event.equals(stock)) { // Stock button clicked String name = JOptionPane.showInputDialog(frame, "Enter product name: "); if (name!= null) { int number = getint("how many items are you adding?"); inventory.stockproduct(name, number); else if (event.equals(sell)) { // Sell button clicked String name = JOptionPane.showInputDialog(frame, "Enter product name: "); if (name!= null) { int number = getint("how many items are you selling?"); inventory.sellproduct(name, number); else if (event.equals(add)) { // Add option clicked String name = JOptionPane.showInputDialog(frame, "Enter new product name: "); if (name!= null) { double cost = getdouble("enter individual product price: ");

inventory.addproduct(name, cost); else if (event.equals(print)) { // Print button clicked inventory.printproducts(); else if (event.equals(price)) { // Check Price button clicked String name = JOptionPane.showInputDialog(frame, "Enter product name: "); if (name!= null) { double cost = inventory.checkprice(name); if (cost == -1) { JOptionPane.showMessageDialog(frame, name + " is not a product."); else { JOptionPane.showMessageDialog(frame, name + ": $" + cost); else if (event.equals(cleardisplay)) { // Clear option clicked "); clear(); else if (event.equals(purge)) { // Purge button clicked String answer = JOptionPane.showInputDialog(frame, "Would you like to check for products with quantities of 0? (yes or no): if (answer.equalsignorecase("yes")) { inventory.purge(); JOptionPane.showMessageDialog(frame, "All products with quantities of 0 removed."); else { if (answer.equalsignorecase("no")) { JOptionPane.showMessageDialog(frame, "Ok! Have a nice day!"); else if (event.equals(quit)) { // Exit option clicked JOptionPane.showMessageDialog(frame, "Thank you for using this Valuable program!"); System.exit(0);

else { // Unknown event occurred JOptionPane.showMessageDialog(frame, "Error: " + e.getactioncommand() + " not recognized."); Gets an integer from the user. Will keep prompting for an integer until one is entered. @param prompt the prompt to display. @return the integer entered by the user. private int getint(string prompt) { String input = JOptionPane.showInputDialog(frame, prompt); try { return Integer.parseInt(input); catch (NumberFormatException e) { JOptionPane.showMessageDialog(frame, "Error: " + input + " is not an integer."); return getint(prompt); Gets a double form the user. Will keep prompting for a double until one is entered. @param prompt the prompt to display. @return the double entered by the user. private double getdouble(string prompt) { String input = JOptionPane.showInputDialog(frame, prompt); try { return Double.parseDouble(input); catch (NumberFormatException e) { JOptionPane.showMessageDialog(frame, "Error: " + input + " is not a number.");

return getdouble(prompt); Builds the GUI window with all buttons and menu items! private void makeframe() { frame = new JFrame("Keepin' Track o' Stuff"); frame.setdefaultcloseoperation(jframe.exit_on_close); Container pane = frame.getcontentpane(); pane.setlayout(new BorderLayout()); output = new JTextArea(); pane.add(new JScrollPane(output), BorderLayout.CENTER); JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); add = new JMenuItem("Add new product"); add.addactionlistener(this); file.add(add); remove = new JMenuItem("Remove product"); remove.addactionlistener(this); file.add(remove); cleardisplay = new JMenuItem("Clear display"); cleardisplay.addactionlistener(this); file.add(cleardisplay); quit = new JMenuItem("Exit"); quit.addactionlistener(this); file.add(quit); menubar.add(file); frame.setjmenubar(menubar); JPanel left = new JPanel(); left.setlayout(new GridLayout(5, 1)); stock = new JButton("Stock"); stock.addactionlistener(this); left.add(stock); sell = new JButton("Sell"); sell.addactionlistener(this); left.add(sell); print = new JButton("Print"); print.addactionlistener(this); left.add(print); price = new JButton("Check Price");

price.addactionlistener(this); left.add(price); total = new JButton("Compute Total Value"); total.addactionlistener(this); left.add(total); pane.add(left, BorderLayout.WEST); purge = new JButton("Purge"); purge.addactionlistener(this); left.add(purge); frame.pack(); frame.setsize(600, 300); frame.setlocationrelativeto(null); frame.setvisible(true);

An object of this class is a type of product that includes: 1) The product name 2) The quantity of the product available 3) The individual product price public class Product { private String name; // Product name private int quantity; // How many items are available private double price; // Price of each item // CONSTRUCTORS Provide the name and price. @param name the name of the product. @param price the price of an individual item. public Product(String name, double price) { this.name = name; this.price = price; quantity = 0; Provide name, price and quantity. @param name the name of the product. @param price the price of an individual item. @param quantity the number of products in stock. public Product(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; // ACCESSOR METHODS

Returns the product's name @return name public String getname() { return name; Is there a duplicate of the product being added? @Override public boolean equals(object other) { if (other instanceof Product) { return true; else { if (!(other instanceof Product)) { return false; Product p = (Product) other; if (this.getname().equals(p.getname()) && this.getprice() == p.getprice()) { return true; else { return false; Returns the number of items available. @return quantity public int getquantity() { return quantity; Returns the price of an item. @return price public double getprice() { return price;

// MUTATOR METHODS Sets a new price for the product. @param newprice the price. public void setprice(double newprice) { price = newprice; Increases the quantity by the given number. @param number number of items added. public void stock(int number) { quantity += number; Decreases the quantity by the given number. @param number number of items sold. public void sell(int number) { quantity -= number; Clears the inventory. public void clear() { quantity = 0; Converts the information into a readable format. @Override public String tostring() {

String s = name + " $" + price + " " + quantity; return s;

An object of this class maintains an inventory of Products. import java.util.arraylist; public class Inventory { private ArrayList<Product> storage; Default constructor. public Inventory() { storage = new ArrayList<Product>(); Stock an existing product. Does nothing if given product is not in inventory. @param name the name of the product. @param moreitems number of items to add. public void stockproduct(string name, int moreitems) { Product p = findproduct(name); if (p!= null) { p.stock(moreitems); Sell an existing product. Does nothing if given product is not in inventory. @param name the name of the product. @param itemsold the number of items to sell. public void sellproduct(string name, int itemssold) { Product p = findproduct(name); if (p!= null) {

p.sell(itemssold); Prints info on all the products in inventory. public void printproducts() { for (int i = 0; i < storage.size(); i++) { System.out.println(storage.get(i)); Add a new product type to inventory. Does nothing if inventory is full. @param name name of the new product. @param price the price of an individual item. public void addproduct(string name, double price) { int n = storage.indexof(name); if (n < 0) { Product p = new Product(name, price); storage.add(p); public void removeproduct(string name, double price) { int n = storage.indexof(name); if (n >= 0) { storage.remove(storage.indexof(name)); @return total value of all of the products in the inventory public double totalvalue() { double total = 0; for (int i = 0; i < storage.size(); i++) { Product allitems = (storage.get(i)); if (allitems!= null) {

total += allitems.getprice(); System.out.println("$" + total); return total; Returns the price of an existing product. @param name the name of the product. @return the price of the product or -1 if product not in inventory. public double checkprice(string name) { Product p = findproduct(name); if (p!= null) { return p.getprice(); else { return -1; Removes all products with a quantity of 0 public void Purge() { for (int i = 0; i < storage.size(); i++) { if (storage.get(i).getquantity() == 0) { storage.remove(storage.get(i)); Returns the Product object with the given name. @param name name of the product to find. @return the Product object or null if product not in inventory. public Product findproduct(string name) { for (int i = 0; i < storage.size(); i++) { if (storage.contains(name)) { return storage.get(i);

System.out.println("We are out of stock on that product."); return null;