Patrick help me

This commit is contained in:
sageTheDM 2025-01-13 17:53:59 +01:00
parent f2c2984db1
commit 012c9bcf61
14 changed files with 3445 additions and 84 deletions

View file

@ -10,18 +10,16 @@ import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.PropertyValueFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
public class CartController { public class CartController {
@FXML private static final String CART_FILE = "cart.txt";
private Button cartBtn;
@FXML @FXML
private Button loginBtn; private Button cartBtn, loginBtn, btnCheckOut;
@FXML
private Button btnCheckOut;
@FXML @FXML
private TableView<Item> cartContent; private TableView<Item> cartContent;
@ -39,15 +37,11 @@ public class CartController {
private TableColumn<Item, Double> priceColumn; private TableColumn<Item, Double> priceColumn;
@FXML @FXML
private Label cartTitle; private Label cartTitle, mainTitle;
@FXML
private Label mainTitle;
// Observable list to hold cart items // Observable list to hold cart items
private final ObservableList<Item> cartItems = FXCollections.observableArrayList(); private final ObservableList<Item> cartItems = FXCollections.observableArrayList();
// Initialize method to set up the TableView
@FXML @FXML
public void initialize() { public void initialize() {
// Set up columns to match Item properties // Set up columns to match Item properties
@ -56,16 +50,27 @@ public class CartController {
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price")); priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
// Populate the cart table with sample data for demonstration // Load cart items from file
cartItems.addAll( loadCartItemsFromFile();
new Item("Book A", "A fascinating fantasy novel.", 1, 12.99),
new Item("Book B", "A thrilling sequel.", 2, 10.50)); // Populate TableView with cart items
cartContent.setItems(cartItems); cartContent.setItems(cartItems);
} }
private void loadCartItemsFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(CART_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
// Assuming each line contains only the book name for simplicity
cartItems.add(new Item(line, "Book Description", 1, 9.99));
}
} catch (IOException e) {
System.err.println("Error loading cart items: " + e.getMessage());
}
}
@FXML @FXML
private void handleCartButton(ActionEvent event) { private void handleCartButton(ActionEvent event) {
System.out.println("Cart button clicked!");
try { try {
App.setRoot("cart"); App.setRoot("cart");
} catch (IOException e) { } catch (IOException e) {
@ -75,7 +80,6 @@ public class CartController {
@FXML @FXML
private void handleLoginButton(ActionEvent event) { private void handleLoginButton(ActionEvent event) {
System.out.println("Login button clicked!");
try { try {
App.setRoot("login"); App.setRoot("login");
} catch (IOException e) { } catch (IOException e) {
@ -92,7 +96,7 @@ public class CartController {
System.out.println("Proceeding to checkout..."); System.out.println("Proceeding to checkout...");
try { try {
App.setRoot("checkout"); App.setRoot("checkOut");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -110,7 +110,6 @@ public class CheckOutController {
System.out.println("Form submitted successfully."); System.out.println("Form submitted successfully.");
System.out.printf("Billing Info: %s, %s, %s, %s, %s\n", fullName, address, city, zip, country); System.out.printf("Billing Info: %s, %s, %s, %s, %s\n", fullName, address, city, zip, country);
System.out.printf("Credit Card Info: %s, %s, %s, CVV: %s\n", cardNumber, cardHolderName, expiryDate, cvv); System.out.printf("Credit Card Info: %s, %s, %s, CVV: %s\n", cardNumber, cardHolderName, expiryDate, cvv);
// Process the payment here
} }
} }
} }

View file

@ -1,23 +1,30 @@
package com.example; package com.example;
import java.util.Objects;
public class Item { public class Item {
private String name; private String name;
private String description; private String description;
private int quantity; private int quantity;
private double price; private double price;
// Constructor
public Item(String name, String description, int quantity, double price) { public Item(String name, String description, int quantity, double price) {
this.name = name; setName(name);
this.description = description; setDescription(description);
this.quantity = quantity; setQuantity(quantity);
this.price = price; setPrice(price);
} }
// Getters and Setters
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Item name cannot be null or empty");
}
this.name = name; this.name = name;
} }
@ -26,6 +33,9 @@ public class Item {
} }
public void setDescription(String description) { public void setDescription(String description) {
if (description == null || description.trim().isEmpty()) {
throw new IllegalArgumentException("Description cannot be null or empty");
}
this.description = description; this.description = description;
} }
@ -34,6 +44,9 @@ public class Item {
} }
public void setQuantity(int quantity) { public void setQuantity(int quantity) {
if (quantity < 0) {
throw new IllegalArgumentException("Quantity cannot be negative");
}
this.quantity = quantity; this.quantity = quantity;
} }
@ -42,6 +55,33 @@ public class Item {
} }
public void setPrice(double price) { public void setPrice(double price) {
if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative");
}
this.price = price; this.price = price;
} }
// Override equals and hashCode for object comparison
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Item item = (Item) o;
return Objects.equals(name, item.name) &&
Objects.equals(description, item.description);
}
@Override
public int hashCode() {
return Objects.hash(name, description);
}
// Override toString for better debugging and UI representation
@Override
public String toString() {
return String.format("Item{name='%s', description='%s', quantity=%d, price=%.2f}",
name, description, quantity, price);
}
} }

View file

@ -1,7 +1,8 @@
package com.example; package com.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@ -9,6 +10,8 @@ import javafx.scene.control.TextArea;
public class ShopController { public class ShopController {
private static final String CART_FILE = "cart.txt";
@FXML @FXML
private Button cartBtn; private Button cartBtn;
@ -16,59 +19,20 @@ public class ShopController {
private Button loginBtn; private Button loginBtn;
@FXML @FXML
private Button cardBtn1; private Button cardBtn1, cardBtn2, cardBtn3, cardBtn4;
@FXML @FXML
private Button cardBtn2; private Button cardBtn5, cardBtn6, cardBtn7, cardBtn8;
@FXML
private Button cardBtn3;
@FXML
private Button cardBtn4;
@FXML
private Button cardBtn5;
@FXML
private Button cardBtn6;
@FXML
private Button cardBtn7;
@FXML
private Button cardBtn8;
@FXML @FXML
private Label mainTitle; private Label mainTitle;
@FXML @FXML
private TextArea cardText1; private TextArea cardText1, cardText2, cardText3, cardText4;
@FXML @FXML
private TextArea cardText2; private TextArea cardText5, cardText6, cardText7, cardText8;
@FXML
private TextArea cardText3;
@FXML
private TextArea cardText4;
@FXML
private TextArea cardText5;
@FXML
private TextArea cardText6;
@FXML
private TextArea cardText7;
@FXML
private TextArea cardText8;
@FXML @FXML
public void initialize() { public void initialize() {
// Initialization logic can go here.
System.out.println("ShopController initialized."); System.out.println("ShopController initialized.");
} }
@ -82,51 +46,59 @@ public class ShopController {
App.setRoot("login"); App.setRoot("login");
} }
// Book Addition Handlers
@FXML @FXML
private void handleAddToCartButton1() { private void handleAddToCartButton1() {
System.out.println("Added 'Lord of the Rings' to cart!"); addToCart("Lord of the Rings");
// Logic for adding the first book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton2() { private void handleAddToCartButton2() {
System.out.println("Added 'The Wheel of Time' to cart!"); addToCart("The Wheel of Time");
// Logic for adding the second book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton3() { private void handleAddToCartButton3() {
System.out.println("Added 'The Chronicles of Narnia' to cart!"); addToCart("The Chronicles of Narnia");
// Logic for adding the third book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton4() { private void handleAddToCartButton4() {
System.out.println("Added 'The Earthsea Cycle' to cart!"); addToCart("The Earthsea Cycle");
// Logic for adding the fourth book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton5() { private void handleAddToCartButton5() {
System.out.println("Added 'The First Law Trilogy' to cart!"); addToCart("The First Law Trilogy");
// Logic for adding the fifth book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton6() { private void handleAddToCartButton6() {
System.out.println("Added 'The Kingkiller Chronicle' to cart!"); addToCart("The Kingkiller Chronicle");
// Logic for adding the sixth book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton7() { private void handleAddToCartButton7() {
System.out.println("Added 'The Mistborn Series' to cart!"); addToCart("The Mistborn Series");
// Logic for adding the seventh book to the cart.
} }
@FXML @FXML
private void handleAddToCartButton8() { private void handleAddToCartButton8() {
System.out.println("Added 'The Stormlight Archive' to cart!"); addToCart("The Stormlight Archive");
// Logic for adding the eighth book to the cart. }
/**
* Adds the specified item to the cart by appending it to the cart file.
*
* @param bookTitle the title of the book to add to the cart
*/
private void addToCart(String bookTitle) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CART_FILE, true))) {
writer.write(bookTitle);
writer.newLine();
System.out.println("Added to cart: " + bookTitle);
} catch (IOException e) {
System.err.println("Failed to add item to cart: " + e.getMessage());
}
} }
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff