Question

Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,

Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields de

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Two buttons are created on the scene 1 of primary stage.
  • click the first button -"Create three retail items" to create 3 RetailItem objects and perform readData on each of them
  • The second button -"Read Retail items" is used to access every data member's value of each RetailItem object.
  • The description values of each item are iteratively read and added as 'menu items' of Menu in stage 2's scene.
  • Click on each Description Menu item , the corresponding price and Units On Hold values are dispalyed on label of the stage 2.

CODE:


package javafxapp;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author Anjana pc
*/

class RetailItem
{
String description;
int unitsOnHand;
double price;
  
public RetailItem()
{
  
}

public RetailItem(String description, int unitsOnHand, double price) {
this.description = description;
this.unitsOnHand = unitsOnHand;
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getUnitsOnHand() {
return unitsOnHand;
}

public void setUnitsOnHand(int unitsOnHand) {
this.unitsOnHand = unitsOnHand;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
  
public void readData(Scanner sc)
{
  
  
String line = sc.nextLine();
System.out.println(line);
String[] arr=line.split(" ");
this.setDescription(arr[0]);
this.setUnitsOnHand(Integer.parseInt(arr[1]));
this.setPrice(Double.parseDouble(arr[2]));

  
  
}
  
}


public class JavaFxApp extends Application {
  
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Retail Sample App");
  
primaryStage.setHeight(500);
primaryStage.setWidth(500);
RetailItem[] ri=new RetailItem[3];
ri[0]=new RetailItem();
ri[1]=new RetailItem();
ri[2]=new RetailItem();
  
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));

Text scenetitle = new Text("Retail");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);

Button btn1 = new Button("Create\n 3 Retail Items");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn1);
grid.add(hbBtn, 0, 1);

Button btn2 = new Button("Read\n Retail Items");
HBox hbBtn2 = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn2);
grid.add(hbBtn2, 1, 1);

final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
  

btn1.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent e) {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Creating 3 Retail Items");
  
try{
  
File f=new File("RetailItemDatabase.txt");
Scanner sc=new Scanner(f);
sc.nextLine();
  
ri[0].readData(sc);
ri[1].readData(sc);
ri[2].readData(sc);
sc.close();
actiontarget.setText("3 Retail Items Created");

}
catch(FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
  
}
});
  
btn2.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent e) {
actiontarget.setFill(Color.FIREBRICK);
  
String res="";
MenuItem mi[]=new MenuItem[3];
for(int i=0;i<3;i++)
{
res+=ri[i].getDescription()+"\t";
res+=ri[i].getUnitsOnHand()+"\t";
res+=ri[i].getPrice()+"\n";
mi[i] = new MenuItem(i+" "+ri[i].getDescription());
}
actiontarget.setText("3 Retail Items read:\n"+res);




  
// create a menu
Menu m = new Menu("Menu");
  
// create menuitems
  
  
// add menu items to menu
m.getItems().add(mi[0]);
m.getItems().add(mi[1]);
m.getItems().add(mi[2]);
  
// label to display events
Label l = new Label("\t\t\t\t"
+ "no menu item selected");
  
// create events for menu items
// action event
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
String[] s=((MenuItem)e.getSource()).getText().split(" ");
int index=Integer.parseInt(s[0]);
l.setText("\n\n\n\t\t\t\t Description - " + s[1] +
"\n\t\t\t\tUnits On Hand - "+ ri[index].getUnitsOnHand()+
"\n\t\t\t\tPrice - "+ri[index].getPrice());
}
};
  
// add event
mi[0].setOnAction(event);
mi[1].setOnAction(event);
mi[2].setOnAction(event);
  
// create a menubar
MenuBar mb = new MenuBar();
  
// add menu to menubar
mb.getMenus().add(m);
  
// create a VBox
VBox vb = new VBox(mb, l);
Stage s=new Stage();
s.setHeight(500);
s.setWidth(500);
Scene sc = new Scene(vb, 500, 300);
  
// set the scene
s.setScene(sc);
  
s.show();
  
}
});
  
  
  
  
  
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
  
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
  
}

OUTPUT:

ALL THE BEST :D

Add a comment
Know the answer?
Add Answer to:
Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing...

    Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing this assignment, students will practice Object Oriented design and programming by creating a Java program that will implement Object Oriented basic concepts. RetailItem Class: Part 1: Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields: • description. The description field references a String object that holds a brief description of the...

  • Write a class named RetailItem that holds data about an item in a retail store. The...

    Write a class named RetailItem that holds data about an item in a retail store. The class should store the following data in attributes: item description, unit sold, units in inventory, and price. Once you have written the class, write a program that creates 5 RetailItem objects and stores the following data in them: Description Units Sold Units in Inventory Price Item #1 Jacket 20 12 59.95 Item #2 Designer Jeans 150 40 34.95 Item #3 Shirt 230 20 24.95...

  • USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds...

    USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...

  • Week 6: Lab Overview TABLE OF CONTENTS Lab Overview Scenario/Summary Write a windows console application that...

    Week 6: Lab Overview TABLE OF CONTENTS Lab Overview Scenario/Summary Write a windows console application that holds data about an item in a retail store. Your class should be named RetailItem and should hold data about an item in a retail store. The class will have the following member variables. Description - string holding the description of the item, unitsOnHand - int that holds the number of units in inventory Price - double that holds the price of the item...

  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

  • Need this program in C#. Thank you 3/19 Lab11 PartC Dur Scenario/Summary Write a windows console...

    Need this program in C#. Thank you 3/19 Lab11 PartC Dur Scenario/Summary Write a windows console application that holds data about an item in a retail store. Your class should be named Retailltem and should hold data about an item in a retail store. The class will have the following member variables. Description- string holding the description of the item, unitsOnHand - int that holds the number of units in inventory Price - double that holds the price of the...

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

  • Java Program Write a class named Car that has the following fields: yearModel- The yearModel field...

    Java Program Write a class named Car that has the following fields: yearModel- The yearModel field is an int that holds the car’s year model. make- The make field is a String object that holds the make of the car, such as “Ford”, “Chevrolet”, etc. speed- This speed field is an int that holds the car’s current speed. In addition, the class should have the following methods: Constructor- The constructor should accept the car’s year model and make as arguments....

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT