In: Computer Science
A javafx gui that's meant to work like a shopping cart, which has 3 books how did this happen 12.99, will benelli, 14.99, and where do I go, 10.99, you can remove from cart, clear cart, or checkout, and it should have a receipt of what you purchased. I am using eclipse, thanks.
Main.java
package sample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
// Button btn = new Button();
// btn.setText("Say 'Hello World'");
// btn.setOnAction(new EventHandler<ActionEvent>() {
//
// @Override
// public void handle(ActionEvent event) {
// System.out.println("Hello World!");
// }
// });
primaryStage.setTitle("Shopping cart App");
FlowPane rootNode = new FlowPane();
FlowPane list_items = new FlowPane();
FlowPane list_items1 = new FlowPane();
FlowPane check_pane = new FlowPane();
rootNode.setVgap(10);
list_items.setVgap(10);
list_items.setHgap(85);
list_items1.setVgap(10);
list_items1.setHgap(10);
check_pane.setHgap(10);
rootNode.setPadding(new Insets(10, 10, 10, 10));
Label items_pick = new Label("Items to pick:");
Label your_cart = new Label("Your shopping cart:");
Label result = new Label("");
ListView<Product> listView = new ListView<>();
Product Apple = new Product("Apple",5);
Product Pumpkin = new Product("Pumpkin",15);
Product Orange = new Product("Orange",10);
Product Banana = new Product("Banana",12);
Product Pear = new Product("Pear",25);
listView.getItems().addAll(Apple,Pumpkin,Orange,Banana,Pear);
listView.setPrefHeight(120);
listView.setPrefWidth(100);
Button add = new Button("Add");
Button remove = new Button("Remove");
Button checkout = new Button("Checkout");
ListView<Product> cartView = new ListView<>();
cartView.setPrefHeight(120);
cartView.setPrefWidth(100); // sporno ako dobavish poveche ot 5
add.setOnAction((ActionEvent event) -> {
cartView.getItems().add(listView.getSelectionModel().getSelectedItem());
});
remove.setOnAction((ActionEvent event) -> {
final int selectedIdx = cartView.getSelectionModel().getSelectedIndex();
cartView.getItems().remove(selectedIdx);
});
checkout.setOnAction((ActionEvent event) -> {
ObservableList<Product> res = cartView.getItems();
double result_price = 0;
for (Product tabPane : res){
result_price += tabPane.getValue();
}
result.setText("Total price: " + result_price);
});
list_items.getChildren().addAll(items_pick,your_cart);
list_items1.getChildren().addAll(listView,add,cartView,remove);
check_pane.getChildren().addAll(checkout,result);
//list_items.setAlignment(Pos.CENTER);
rootNode.getChildren().addAll(list_items,list_items1,check_pane);
Scene myScene = new Scene(rootNode,400,300
primaryStage.setScene(myScene);
primaryStage.show();
}
/**
command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Product.java
package sample;
public class Product {
String name;
double price;
Product(String name, double price){
this.name = name;
this.price = price;
}
public String toString(){
return this.name + " - " + this.price;
}
double getValue(){
return this.price;
}
}
Hope This will help You .