-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ahmedsiam0/cart-discount-payment
Add cart, discountViewer and payment
- Loading branch information
Showing
15 changed files
with
803 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
92
src/main/java/com/asu/librarysystem/DiscountsViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.asu.librarysystem; | ||
|
||
public interface PaymentMethod { | ||
Boolean pay(); | ||
} |
Oops, something went wrong.