Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
MampfMobil.java 19.56 KiB
package com.example.mampfmobil.ui;

import android.content.Context;
import android.util.Log;

import com.example.mampfmobil.R;
import com.example.mampfmobil.ui.Classes.Bestellung;
import com.example.mampfmobil.ui.Classes.BestellungsTeil;
import com.example.mampfmobil.ui.Classes.Customer;
import com.example.mampfmobil.ui.Classes.Deliverer;
import com.example.mampfmobil.ui.Classes.Item;
import com.example.mampfmobil.ui.Classes.ShopItem;
import com.example.mampfmobil.ui.Classes.Supplier;

import java.util.Vector;

public class MampfMobil {

    public static Vector<Customer> customers;
    public static Customer currentCustomer;
    public static Vector<Deliverer> deliverers;
    public static Deliverer currentDeliverer;
    public static Vector<Supplier> suppliers;
    public static Supplier currentSupplier;

    static Context context;



    public static boolean isInitialized = false;

    public MampfMobil(Context context){
        this.context = context;

        if(!isInitialized) {
            customers = new Vector<>();
            deliverers = new Vector<>();
            suppliers = new Vector<>();
            takefromPersistance();
            isInitialized = true;
        }

        /*if(!isInitialized) {
            customers = new Vector<>();
            deliverers = new Vector<>();
            suppliers = new Vector<>();

            //create Dummys!!!
            customers.add(new Customer("Fabio", "Heyming", "Weinbergstr 70, 55299 Nackenheim"));
            customers.add(new Customer("Leah", "Iilyav", "Bad Homburg"));
            customers.add(new Customer("Max", "Muster", "Musteradresse"));
            deliverers.add(new Deliverer("Mr", "Fahrer", "Fahrstr."));
            deliverers.add(new Deliverer("Mrs", "Fahrerin", "Fahrstr."));
            suppliers.add(new Supplier("DummyShop", "Dummystreet"));
            suppliers.add(new Supplier("FreeShop", "Freeadress"));
            suppliers.add(new Supplier("Rewe", "Rewestr"));
            suppliers.get(0).itemAdd("DummyApfel", 10, 0.5);
            suppliers.get(0).itemAdd("DummyBanane", 5, 0.8);
            suppliers.get(0).itemAdd("DummyBirne", 10, 0.5);
            suppliers.get(0).itemAdd("DummyAnanas", 5, 0.8);
            suppliers.get(1).itemAdd("DummyApfel", 10, 0.5);
            suppliers.get(1).itemAdd("DummyBanane", 5, 0.8);
            suppliers.get(2).itemAdd("FreeApfel", 5, 3.4);
            suppliers.get(2).itemAdd("FreeBanane", 5, 5.8);

            Bestellung temp = new Bestellung(suppliers.get(0),true,context.getString(R.string.ordered),customers.get(0));
            temp.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 5));
            temp.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 2));
            temp.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 2));
            temp.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 7));
            customers.get(0).addBestellung(temp);

            Bestellung temp4 = new Bestellung(suppliers.get(0),true,context.getString(R.string.readyForTransport),customers.get(0));
            temp4.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 8));
            temp4.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 2));
            temp4.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(2).item, 1));
            customers.get(0).addBestellung(temp4);

            Bestellung temp6 = new Bestellung(suppliers.get(0),true,context.getString(R.string.readyForTransportDelivererFound),customers.get(0));
            temp6.deliverer = deliverers.get(0);
            temp6.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 8));
            temp6.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 2));
            temp6.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(2).item, 1));
            customers.get(0).addBestellung(temp6);

            Bestellung temp5 = new Bestellung(suppliers.get(0),false,context.getString(R.string.readyForPickup),customers.get(0));
            temp5.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 8));
            temp5.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 2));
            temp5.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(2).item, 1));
            customers.get(0).addBestellung(temp5);

            Bestellung temp2 = new Bestellung(suppliers.get(0),true,context.getString(R.string.inTransportation),customers.get(0));
            temp2.deliverer = deliverers.get(0);
            temp2.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 5));
            temp2.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 10));
            temp2.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(2).item, 5));
            temp2.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(3).item, 10));
            customers.get(0).addBestellung(temp2);

            Bestellung temp7 = new Bestellung(suppliers.get(0),true,context.getString(R.string.delivered),customers.get(0));
            temp7.deliverer = deliverers.get(0);
            temp7.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(0).item, 5));
            temp7.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(1).item, 10));
            temp7.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(2).item, 5));
            temp7.addBestellungsteil(new BestellungsTeil(suppliers.get(0).shopItems.get(3).item, 10));
            customers.get(0).addBestellung(temp7);

            Bestellung temp3 = new Bestellung(suppliers.get(1),true,"ordered",customers.get(1));
            temp3.addBestellungsteil(new BestellungsTeil(suppliers.get(1).shopItems.get(1).item, 7));
            temp3.addBestellungsteil(new BestellungsTeil(suppliers.get(1).shopItems.get(0).item, 14));
            customers.get(1).addBestellung(temp3);


            customers.add(new Customer("", "", ""));
            deliverers.add(new Deliverer("", "", ""));
            suppliers.add(new Supplier("", ""));
            isInitialized = true;
            savePersistant();
        }*/
    }

    public static boolean findCustomer(String vorname, String nachname){
        for(Customer c:customers){
            if(c.vorname.equals(vorname) && c.nachname.equals(nachname)){
                MampfMobil.currentCustomer = c;
                return true;
            }
        }
        return false;
    }

    public static boolean createCustomer(String vorname, String nachname,String adresse){
        for(Customer c:customers){
            if(c.vorname.equals(vorname) && c.nachname.equals(nachname)){
                return false;
            }
        }
        Customer temp = new Customer(vorname,nachname,adresse);
        customers.add(temp);
        currentCustomer = temp;
        return true;
    }

    public static boolean findSupplier(String name){
        for(Supplier s:suppliers){
            if(s.name.equals(name)){
                MampfMobil.currentSupplier = s;
                return true;
            }
        }
        return false;
    }

    public static boolean createSupplier(String name,String address){
        for(Supplier s:suppliers){
            if(s.name.equals(name)){
                return false;
            }
        }
        Supplier temp = new Supplier(name,address);
        suppliers.add(temp);
        currentSupplier = temp;
        return true;
    }
    public static boolean findDeliverer(String vorname, String nachname){
        for(Deliverer d:deliverers){
            if(d.vorname.equals(vorname) && d.nachname.equals(nachname)){
                MampfMobil.currentDeliverer = d;
                return true;
            }
        }
        return false;
    }

    public static boolean createDeliverer(String vorname, String nachname,String address){
        for(Deliverer d:deliverers){
            if(d.vorname.equals(vorname) && d.nachname.equals(nachname)){
                return false;
            }
        }

        Deliverer temp = new Deliverer(vorname,nachname,address);
        deliverers.add(temp);
        currentDeliverer=temp;
        return true;
    }

    public static int getItemQuantity(Item item){
        for(Supplier s:suppliers){
            for(ShopItem sI: s.shopItems){
                if(sI.item == item){
                    return sI.quantity;
                }
            }
        }
        return 0;
    }



    public static void savePersistant(){

        //ID,VORNAME,NACHNAME,ADRESSE
        String csvDeliverer = "";
        for(Deliverer d:MampfMobil.deliverers){
            csvDeliverer = csvDeliverer + d.id +","+ d.vorname +","+ d.nachname +","+ d.adresse + "\n";
        }
        String fileName = "deliverer.csv";
        CSVFileHelper.saveCSVFile(context, fileName, csvDeliverer);

        //ID,NAME,ADRESSE
        String csvSupplier = "";
        for(Supplier s:MampfMobil.suppliers){
            csvSupplier = csvSupplier + s.id +","+ s.name +","+s.address + "\n";
        }
        String fileName2 = "supplier.csv";
        CSVFileHelper.saveCSVFile(context, fileName2, csvSupplier);


        //ID,VORNAME,NACHNAME,ADRESSE
        String csvCustomer = "";
        for(Customer c:MampfMobil.customers){
            csvCustomer = csvCustomer + c.id +","+ c.vorname +","+ c.nachname +","+ c.adresse + "\n";
        }
        String fileName3 = "customer.csv";
        CSVFileHelper.saveCSVFile(context, fileName3, csvCustomer);



        // ID,NAME,PREIS,SUPPLIER-ID
        String csvItems = "";
        for(Supplier s:MampfMobil.suppliers){
            for(ShopItem sI: s.shopItems){
                csvItems = csvItems + sI.item.id + "," + sI.item.name + "," + sI.item.price + "," + sI.item.supplier.id + "\n";
            }
        }
        String fileName4 = "items.csv";
        CSVFileHelper.saveCSVFile(context, fileName4, csvItems);




        // ID/ITEM-ID(immer gleich), AMOUNT
        String csvShopItems = "";
        for(Supplier s:MampfMobil.suppliers){
            for(ShopItem sI: s.shopItems){
                csvShopItems = csvShopItems + sI.id + "," + sI.quantity + "\n";
            }
        }
        String fileName5 = "shopItems.csv";
        CSVFileHelper.saveCSVFile(context, fileName5, csvShopItems);


        // ID,CUSTOMER_ID, DELIVERY(TRUE/FALSE),STATE,DELIVERER-ID(0 when none),SUPPLIER-ID,
        // bestellungsteil besteht aus  ITEM-ID1, QUANTITY1, ITEM-ID2, QUANTITY2, ITEM-ID3, QUANTITY3 .... \n
        String csvBestellungen = "";
        for(Customer c:MampfMobil.customers){
            for(Bestellung b: c.bestellungen){
                csvBestellungen = csvBestellungen + b.id + ","+ b.costumer.id+ "," + b.delivery + "," + b.state + ",";
                if(b.deliverer == null){
                    csvBestellungen = csvBestellungen + "0,";

                }
                else{
                    csvBestellungen = csvBestellungen + b.deliverer.id + ",";

                }
                csvBestellungen = csvBestellungen + b.supplier.id;


                for (BestellungsTeil bT: b.bestellungsTeile){
                    csvBestellungen = csvBestellungen +","+ bT.item.id + "," + bT.quantity ;

                }
                csvBestellungen = csvBestellungen + "\n";

            }
        }
        String fileName6 = "bestellungen.csv";
        CSVFileHelper.saveCSVFile(context, fileName6, csvBestellungen);




        // bestellungsteil besteht aus  Customer-ID, ShopItem-ID
        String csvFavoriten = "";
        for(Customer c:MampfMobil.customers){
            for(ShopItem sI: c.favoriten){
                csvFavoriten = csvFavoriten + c.id + ","+ sI.id + "\n";
            }
        }
        String fileName7 = "favoriten.csv";
        CSVFileHelper.saveCSVFile(context, fileName7, csvBestellungen);

    }

    public static void takefromPersistance() {

        String fileName = "deliverer.csv";
        String csvData = CSVFileHelper.loadCSVFile(context, fileName);
        if (csvData != null) {
            String[] lines = csvData.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 4) {
                    int id = Integer.parseInt(data[0]);
                    String vorname = data[1];
                    String nachname = data[2];
                    String adresse = data[3];

                    // Verwende die Daten, um den Deliverer-Objekt zu erstellen oder zu aktualisieren
                    Deliverer deliverer = new Deliverer(id, vorname, nachname, adresse);
                    // Füge den Deliverer-Objekt deiner Datenstruktur hinzu
                    MampfMobil.deliverers.add(deliverer);
                }
            }
        }


        String fileName2 = "customer.csv";
        String csvData2 = CSVFileHelper.loadCSVFile(context, fileName2);
        if (csvData2 != null) {
            String[] lines = csvData2.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 4) {
                    int id = Integer.parseInt(data[0]);
                    String vorname = data[1];
                    String nachname = data[2];
                    String adresse = data[3];

                    // Verwende die Daten, um den Deliverer-Objekt zu erstellen oder zu aktualisieren
                    Customer customer = new Customer(id, vorname, nachname, adresse);
                    // Füge den Deliverer-Objekt deiner Datenstruktur hinzu
                    MampfMobil.customers.add(customer);
                }
            }
        }


        String fileName3 = "supplier.csv";
        String csvData3 = CSVFileHelper.loadCSVFile(context, fileName3);
        if (csvData3 != null) {
            String[] lines = csvData3.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");
                if (data.length >= 3) {
                    int id = Integer.parseInt(data[0]);
                    String name = data[1];
                    String adresse = data[2];

                    // Verwende die Daten, um den Deliverer-Objekt zu erstellen oder zu aktualisieren
                    Supplier supplier = new Supplier(id, name, adresse);
                    // Füge den Deliverer-Objekt deiner Datenstruktur hinzu
                    MampfMobil.suppliers.add(supplier);
                }
            }
        }


        String fileName4 = "items.csv";
        String csvData4 = CSVFileHelper.loadCSVFile(context, fileName4);
        if (csvData4 != null) {
            String[] lines = csvData4.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 4) {
                    int itemId = Integer.parseInt(data[0]);
                    String name = data[1];
                    double preis = Double.parseDouble(data[2]);
                    int supplierId = Integer.parseInt(data[3]);

                    // Verwende die Daten, um den Deliverer-Objekt zu erstellen oder zu aktualisieren
                    for (Supplier s : MampfMobil.suppliers) {
                        if (s.id == supplierId) {

                            Item tempItem = new Item(itemId,name,preis,s);
                            s.shopItems.add(new ShopItem(itemId,tempItem,0));
                            //s.itemAdd(itemId, name, 0, preis);
                        }
                    }
                }
            }
        }


        String fileName5 = "shopItems.csv";
        String csvData5 = CSVFileHelper.loadCSVFile(context, fileName5);
        if (csvData5 != null) {
            String[] lines = csvData5.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 2) {
                    int itemId = Integer.parseInt(data[0]);
                    int quantity = Integer.parseInt(data[1]);


                    // Verwende die Daten, um den Deliverer-Objekt zu erstellen oder zu aktualisieren
                    for (Supplier s : MampfMobil.suppliers) {
                        for (ShopItem sI : s.shopItems) {
                            if (sI.id == itemId) {
                                sI.quantity = quantity;
                            }
                        }
                    }
                }
            }

        }


        String fileName6 = "bestellungen.csv";
        String csvData6 = CSVFileHelper.loadCSVFile(context, fileName6);
        if (csvData6 != null) {
            String[] lines = csvData6.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 6) {
                    int ID = Integer.parseInt(data[0]);
                    int customerId = Integer.parseInt(data[1]);
                    String deliver = data[2];
                    boolean delivery = false;
                    if (deliver.equals("true")) {
                        delivery = true;
                    }
                    String state = data[3];
                    int delivererId = Integer.parseInt(data[4]);
                    int supplierId = Integer.parseInt(data[5]);

                    Supplier tempSup = null;
                    for (Supplier s : MampfMobil.suppliers) {
                        if (s.id == supplierId) {
                            tempSup = s;
                        }
                    }
                    Customer tempCus = null;
                    for (Customer c : MampfMobil.customers) {
                        if (c.id == customerId) {
                            tempCus = c;
                        }
                    }

                    Bestellung temp = new Bestellung(ID, tempSup, delivery, state, tempCus, delivererId);


                    for (int i = 6; i < data.length; i += 2) {
                        int ItemId = Integer.parseInt(data[i]);
                        int quantity = Integer.parseInt(data[i + 1]);

                        Item tempItem = null;
                        for (Supplier s : MampfMobil.suppliers) {
                            for (ShopItem sI : s.shopItems) {
                                if (sI.id == ItemId) {
                                    tempItem = sI.item;
                                }
                            }
                        }

                        temp.addBestellungsteil(new BestellungsTeil(tempItem, quantity));
                    }

                    tempCus.addBestellung(temp);
                }
            }
        }



        String fileName7 = "favoriten.csv";
        String csvData7 = CSVFileHelper.loadCSVFile(context, fileName7);
        if (csvData7 != null) {
            String[] lines = csvData7.split("\n");
            for (String line : lines) {
                String[] data = line.split(",");

                if (data.length >= 2) {
                    int cusId = Integer.parseInt(data[0]);
                    int shopItemId = Integer.parseInt(data[0]);

                    for (Customer c : MampfMobil.customers) {
                        if (c.id == cusId) {
                            for (Supplier s : MampfMobil.suppliers) {
                                for (ShopItem sI : s.shopItems) {
                                    if (sI.id == shopItemId) {
                                        c.addToFavourits(sI);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}