144 lines
4.1 KiB
Java
144 lines
4.1 KiB
Java
package Shop;
|
|
import Shop.Products;
|
|
import java.util.ArrayList;
|
|
|
|
public class Cart {
|
|
private static ArrayList<Products> cartContents;
|
|
|
|
public Cart() {
|
|
cartContents = new ArrayList<Products>();
|
|
}
|
|
|
|
|
|
//Adding stuff to Cart (adding stuff to cart means removing from stock)
|
|
public static boolean AddCart(Products prod, int amount){
|
|
if (prod.getStock() > 0) { // checking if product is in stock
|
|
cartContents.add(prod); // adding to cart
|
|
prod.setStock(prod.getStock() - amount); // removing from stock
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int NumberOfArticels (Products prod){
|
|
int nums = 0;
|
|
for(Products p: cartContents){
|
|
if(p.equals(prod)){
|
|
nums++;
|
|
}
|
|
}
|
|
return nums;
|
|
}
|
|
|
|
//removing stuff from cart (means adding back to stock)
|
|
public boolean RemoveCart(Products prod, int amount){
|
|
if ( NumberOfArticels(prod) > 0) { // checking if product is in stock
|
|
if(cartContents.remove(prod)){ // removing from cart
|
|
prod.setStock(prod.getStock() + amount); // adding back to stock
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int AmountAllCart (){
|
|
return cartContents.size();
|
|
}
|
|
|
|
public double GrossAmountProd (){
|
|
double gross = 0.0;
|
|
for(Products prod: cartContents) {
|
|
gross += prod.getGrossPrice();
|
|
}
|
|
return gross;
|
|
}
|
|
|
|
public double ContentsMass() {
|
|
double weights = 0.0;
|
|
for(Products prod: cartContents) {
|
|
weights += prod.getWeight();
|
|
}
|
|
return weights;
|
|
}
|
|
|
|
public double ContentsNetWorth() {
|
|
double netWorth = 0.0;
|
|
for(Products prod: cartContents) {
|
|
netWorth += prod.getNetWorth();
|
|
}
|
|
return netWorth;
|
|
}
|
|
|
|
|
|
public double deliveryCost () {
|
|
|
|
double weight = ContentsMass();
|
|
double delivery = 0;
|
|
|
|
//Bedingungen für die Versandkosten:
|
|
if ( weight <= 0.1) {
|
|
delivery = 3.95;
|
|
}
|
|
else if (weight > 0.1 && weight <= 1.0) {
|
|
delivery = 4.95;
|
|
}
|
|
else if (weight> 1.0 && weight <= 5.0 ) {
|
|
delivery = 5.95;
|
|
}
|
|
else if (weight > 5.0) {
|
|
delivery = 19.95;
|
|
}
|
|
|
|
|
|
if (GrossAmountProd() >= 500) { //Brutto == Warenwert
|
|
delivery = 0;}
|
|
|
|
return delivery;
|
|
}
|
|
|
|
|
|
public double taxDelivery(){
|
|
double delivery = deliveryCost ();
|
|
|
|
double tax7perc = 0; // grosstax is 7% which means: 1/3rd of 19,95
|
|
double tax19perc = 0; // grosstax is 19% which means: 2/3rd of 19,95
|
|
|
|
for (Products prod : cartContents) {
|
|
if (prod.getNetWorth() == 7) {
|
|
tax7perc += prod.getGrossPrice();
|
|
}
|
|
else if (prod.getNetWorth() == 19) {
|
|
tax19perc += prod.getGrossPrice();
|
|
}
|
|
}
|
|
//Prozentuale Anteile am gesamten Bruttopreis:
|
|
double gross = GrossAmountProd ();
|
|
|
|
|
|
if (gross == 0) { //Um Teilung durch 0 zu verhindern (wenn Warenkorb leer)
|
|
return 0;
|
|
}
|
|
|
|
//Anteile am Versand:
|
|
double delivery7 = delivery * (tax7perc / gross);
|
|
double delivery19 = delivery * (tax19perc / gross);
|
|
|
|
//MwSt vom Versand:
|
|
double taxDelivery7 = delivery7- (delivery7/ 1.07);
|
|
double taxDelivery19 = delivery19 - (delivery19 / 1.19);
|
|
|
|
return taxDelivery7 + taxDelivery19;
|
|
}
|
|
|
|
|
|
|
|
public double wholeAmount () {
|
|
|
|
double amount = GrossAmountProd () + deliveryCost () + taxDelivery();
|
|
|
|
|
|
return amount;
|
|
}
|
|
|
|
|
|
} |