Skip to content

Commit

Permalink
Merge pull request #30 from ahmedsiam0/cart-discount-payment
Browse files Browse the repository at this point in the history
Add cart, discountViewer and payment
  • Loading branch information
ahmedsiam0 authored Dec 20, 2023
2 parents 049132b + db323cb commit aa3b83c
Show file tree
Hide file tree
Showing 15 changed files with 803 additions and 45 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/asu/librarysystem/CardPayment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.asu.librarysystem;

public class CardPayment implements PaymentMethod {
private String cardNumber;
private String cvv;


CardPayment(String cardNumber, String cvv) {
this.cardNumber = cardNumber;
this.cvv = cvv;
}

@Override
public Boolean pay() {
if (cardNumber.length() != 16)
return false;
if (cvv.length() != 3)
return false;
return true;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/asu/librarysystem/CashPayment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.asu.librarysystem;

public class CashPayment implements PaymentMethod {
private String paymentAddress;

CashPayment(String paymentAddress) {
this.paymentAddress = paymentAddress;
}

@Override
public Boolean pay() {
if (paymentAddress.length() < 1) {
return false;
}
return true;
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/asu/librarysystem/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void updateQuantity(int orderId, int newQuantity) {
}
}


public void setAssignBefore(boolean assignBefore) {
if (!assignBefore)
this.assignBefore = true;
Expand All @@ -63,4 +62,7 @@ public ArrayList<Book> arrayOFOrderBooks(){
}
return orderBooksArrayList;
}
public ArrayList<Order> getOrders() {
return orders;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/asu/librarysystem/Discount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.asu.librarysystem;

public class Discount {
private String code;
private double discount;

public Discount(String code, double discount) {
this.code = code;
this.discount = discount;
}
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public double getDiscount() {
return discount;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/asu/librarysystem/DiscountHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.asu.librarysystem;

import java.util.ArrayList;

public class DiscountHandler {
private ArrayList<Discount> discounts;

public DiscountHandler() {
discounts = new ArrayList<Discount>();
}

public void addDiscount(String code, Double discount) {
int index = findDiscount(code);
if (index != -1)
discounts.remove(index);
discounts.add(new Discount(code, discount));
}

public void deleteDiscount(String code) {
int index = findDiscount(code);
if (index != -1)
discounts.remove(index);
}

public double getDiscount(String code) {
int index = findDiscount(code);
if (index == -1)
return -1.0;
return discounts.get(index).getDiscount();
}

public ArrayList<Discount> getDiscounts() {
return (new ArrayList<Discount>(discounts));
}

private int findDiscount(String code) {
for (int i = 0; i < discounts.size(); i++) {
if (discounts.get(i).getCode().equals(code)) {
return i;
}
}
return -1;
}
}
92 changes: 92 additions & 0 deletions src/main/java/com/asu/librarysystem/DiscountsViewController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.asu.librarysystem;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.ArrayList;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class DiscountsViewController implements Initializable {
@FXML
private TableView<Discount> mainTable;

@FXML
private TableColumn<Discount, String> codeColumn;

@FXML
private TableColumn<Discount, Double> discountColumn;

@FXML
private GridPane root;

@FXML
private TextField addDiscount;

private Stage stage;

private ObservableList<Discount> tableData = FXCollections.observableArrayList();

private static String discountInitial = "Lucky";
private static int discountCounter = 0;

public void delete(ActionEvent event) {
ObservableList<Discount> selected = mainTable.getSelectionModel().getSelectedItems();
for (var discount : selected) {
Library.getDiscountHandler().deleteDiscount(discount.getCode());
}
mainTable.getItems().removeAll(selected);
}
public void add(ActionEvent event) {
try {
String discountCode = discountInitial + discountCounter;
double discount = Double.parseDouble(addDiscount.getText());

if (discount <= 0.0 || discount > 100.0)
throw new Exception();

tableData.add(new Discount(discountCode, discount));
Library.getDiscountHandler().addDiscount(discountCode, discount);

discountCounter++;
} catch (Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setContentText("Only a number more than 0 and less than or equal 100 is allowed");
alert.show();
}
}
public void back(ActionEvent event) {
stage = (Stage)root.getScene().getWindow();
stage.close();
}

@Override
public void initialize(URL location, ResourceBundle resources) {
codeColumn.setCellValueFactory(new PropertyValueFactory<Discount, String>("code"));
discountColumn.setCellValueFactory(new PropertyValueFactory<Discount, Double>("discount"));



ArrayList<Discount> discounts = Library.getDiscountHandler().getDiscounts();

for (var discount : discounts) {
tableData.add(discount);
}



mainTable.setItems(tableData);
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/asu/librarysystem/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class Library {
private static Account activeAccount;
private static Admin admin = new Admin("Ahmad" , "1234" ,"01030243591") ;
private static ReviewHandler reviewHandler = new ReviewHandler();
private static DiscountHandler discountHandler = new DiscountHandler();
private static Stack<Book> previousBooks = new Stack<Book>();
private static ShoppingCart shoppingCart = new ShoppingCart();

private static String lastViewer;

Expand Down Expand Up @@ -314,12 +316,17 @@ public static ReviewHandler getReviewHandler() {
return reviewHandler;
}

public static DiscountHandler getDiscountHandler() {
return discountHandler;
}

public static Account getActiveAccount() {
return activeAccount;
}


public static ShoppingCart getShoppingCart() {
return shoppingCart;
}

public static ArrayList<Book> copyElementOfArrayList() {
return books;
Expand Down Expand Up @@ -529,4 +536,4 @@ public static ArrayList<Book> searchBookByAuthorInArray(String word) {
// }
// }

}
}
100 changes: 58 additions & 42 deletions src/main/java/com/asu/librarysystem/Order.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
package com.asu.librarysystem;

import static com.asu.librarysystem.Library.searchBookById;

public class Order {

static private int idCounter = 0;
final private int id;
private int bookId;
private int quantity;

public Order(int bookId, int quantity) {
id = ++idCounter;
this.bookId = bookId;
this.quantity = quantity;
}


public int getId() {
return id;
}

public int getBookId() {
return bookId;
}

public int getQuantity() {
return quantity;
}

public int getPrice() {
return searchBookById(bookId).getPrice() * quantity;
}

public void setBookId(int bookId) {
this.bookId = bookId;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
package com.asu.librarysystem;

public class Order {

static private int idCounter = 0;
final private int id;
private int bookId;
private int quantity;
private String discountCode;

public Order(int bookId, int quantity) {
this.id = ++idCounter;
this.bookId = bookId;
this.quantity = quantity;
this.discountCode = "";
}

public int getId() {
return id;
}

public String getBook() {
return Library.searchBookById(bookId).getTitle();
}

public int getBookId() {
return bookId;
}

public int getQuantity() {
return quantity;
}

public double getPrice() {
double price = Library.searchBookById(bookId).getPrice() * quantity;
if (discountCode != "") {
price -= Library.getDiscountHandler().getDiscount(discountCode) * price / 100.0;
}
return price;
}

public void setBook(int bookId) {
this.bookId = bookId;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public void setDiscountCode(String discountCode) {
if (Library.getDiscountHandler().getDiscount(discountCode) != -1.0)
this.discountCode = discountCode;
}

public String getDiscountCode() {
return discountCode;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/asu/librarysystem/PaymentMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.asu.librarysystem;

public interface PaymentMethod {
Boolean pay();
}
Loading

0 comments on commit aa3b83c

Please sign in to comment.