-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwnerBookScreen.java
More file actions
110 lines (90 loc) · 3.66 KB
/
OwnerBookScreen.java
File metadata and controls
110 lines (90 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package bookstoreapp;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import static bookstoreapp.BookStore.*;
/**
*
* @author a4rahim
*/
public class OwnerBooksScreen {
private final TableView<Book> table = new TableView<>();
public Group display(Stage primaryStage, BookstoreApp app) {
Group screen = new Group();
//Table Header
final Label label = new Label("Books Shelf");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
// Load books
ObservableList<Book> booksList = FXCollections.observableArrayList(getBooks());
table.setItems(booksList);
TableColumn<Book, String> BookName = new TableColumn<>("Book Name");
BookName.setMinWidth(200);
BookName.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Book, Double> BookPrice = new TableColumn<>("Book Price");
BookPrice.setMinWidth(150);
BookPrice.setCellValueFactory(new PropertyValueFactory<>("price"));
table.getColumns().addAll(BookName, BookPrice);
// Add book fields
TextField addBookName = new TextField();
addBookName.setPromptText("Book Name");
addBookName.setMaxWidth(200);
TextField addBookPrice = new TextField();
addBookPrice.setPromptText("Price");
addBookPrice.setMaxWidth(150);
Button addButton = new Button("Add Book");
addButton.setOnAction(e -> {
try {
String name = addBookName.getText().trim();
double price = Double.parseDouble(addBookPrice.getText().trim());
if (!name.isEmpty() && price > 0) {
Book newBook = new Book(name, price);
getBooks().add(newBook);
booksList.add(newBook);
saveBooks();
addBookName.clear();
addBookPrice.clear();
}
} catch (NumberFormatException ex) {
// Invalid price
}
});
Button deleteButton = new Button("Delete Selected");
deleteButton.setOnAction(e -> {
Book selected = table.getSelectionModel().getSelectedItem();
if (selected != null) {
getBooks().remove(selected);
booksList.remove(selected);
saveBooks();
}
});
Button backButton = new Button("Back");
backButton.setOnAction(e -> {
app.showOwnerStartScreen();
});
HBox addBox = new HBox(5);
addBox.setAlignment(javafx.geometry.Pos.CENTER);
addBox.getChildren().addAll(addBookName, addBookPrice, addButton, deleteButton);
HBox backBox = new HBox(5);
backBox.setPadding(new Insets(5));
backBox.getChildren().add(backButton);
final VBox vbox = new VBox();
final VBox vbox = new VBox(10);
vbox.setSpacing(10);
vbox.setPadding(new Insets(20, 0, 0, 50));
vbox.getChildren().addAll(backBox, label, table, addBox);
screen.getChildren().addAll(vbox);
return screen;
}
}