Skip to content
Snippets Groups Projects
MampfMobil.java 21.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • fheyming's avatar
    fheyming committed
    package com.example.mampfmobil.ui;
    
    
    fheyming's avatar
    fheyming committed
    import android.content.Context;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    import android.util.Log;
    
    
    fheyming's avatar
    fheyming committed
    import com.example.mampfmobil.R;
    
    fheyming's avatar
    fheyming committed
    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;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    import com.example.mampfmobil.ui.Classes.Item;
    import com.example.mampfmobil.ui.Classes.ShopItem;
    
    fheyming's avatar
    fheyming committed
    import com.example.mampfmobil.ui.Classes.Supplier;
    
    import java.util.Vector;
    
    public class MampfMobil {
    
        public static Vector<Customer> customers;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        public static Customer currentCustomer;
    
    fheyming's avatar
    fheyming committed
        public static Vector<Deliverer> deliverers;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        public static Deliverer currentDeliverer;
    
    fheyming's avatar
    fheyming committed
        public static Vector<Supplier> suppliers;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        public static Supplier currentSupplier;
    
    fheyming's avatar
    fheyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        static Context context;
    
    fheyming's avatar
    fheyming committed
    
    
        public static boolean isInitialized = false;
    
    fheyming's avatar
    fheyming committed
        public MampfMobil(Context context){
            this.context = context;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
            if(!isInitialized) {
                customers = new Vector<>();
                deliverers = new Vector<>();
                suppliers = new Vector<>();
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                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"));
    
    fheyming's avatar
    fheyming committed
                suppliers.add(new Supplier("FreeShop", "Freeadress"));
    
                suppliers.add(new Supplier("Rewe", "Rewestr"));
    
    fheyming's avatar
    fheyming committed
                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);
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                suppliers.get(1).itemAdd("DummyBanane", 5, 0.8);
    
                suppliers.get(2).itemAdd("FreeApfel", 5, 3.4);
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                suppliers.get(2).itemAdd("FreeBanane", 5, 5.8);
    
    fheyming's avatar
    fheyming committed
    
                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);
    
    fheyming's avatar
    fheyming committed
    
                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;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                savePersistant();
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            }*/
    
    fheyming's avatar
    fheyming committed
        }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
        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;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            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;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            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;
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            return true;
        }
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        public static int getItemQuantity(Item item){
            for(Supplier s:suppliers){
                for(ShopItem sI: s.shopItems){
                    if(sI.item == item){
                        return sI.quantity;
                    }
                }
            }
            return 0;
        }
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
        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);
    
    
    
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            // ID/ITEM-ID(immer gleich), AMOUNT
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            String csvShopItems = "";
            for(Supplier s:MampfMobil.suppliers){
                for(ShopItem sI: s.shopItems){
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    csvShopItems = csvShopItems + sI.id + "," + sI.quantity + "\n";
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                }
            }
            String fileName5 = "shopItems.csv";
            CSVFileHelper.saveCSVFile(context, fileName5, csvShopItems);
    
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            // ID,CUSTOMER_ID, DELIVERY(TRUE/FALSE),STATE,DELIVERER-ID(0 when none),SUPPLIER-ID,
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            // 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,";
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    }
                    else{
                        csvBestellungen = csvBestellungen + b.deliverer.id + ",";
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    csvBestellungen = csvBestellungen + b.supplier.id;
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
                    for (BestellungsTeil bT: b.bestellungsTeile){
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                        csvBestellungen = csvBestellungen +","+ bT.item.id + "," + bT.quantity ;
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                    csvBestellungen = csvBestellungen + "\n";
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                }
            }
            String fileName6 = "bestellungen.csv";
            CSVFileHelper.saveCSVFile(context, fileName6, csvBestellungen);
    
    
    
            //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, csvFavoriten);
    
    
            //Customer-ID, Item-ID, quantity
            String csvShoppingCart = "";
            for(Customer c:MampfMobil.customers){
                for(BestellungsTeil bT: c.shoppingCart){
                    csvShoppingCart = csvShoppingCart + c.id + ","+ bT.item.id + "," + bT.quantity +"\n";
                }
            }
            String fileName8 = "shoppingCart.csv";
            CSVFileHelper.saveCSVFile(context, fileName8, csvShoppingCart);
    
    fheyming's avatar
    fheyming committed
        }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        public static void takefromPersistance() {
    
            String fileName = "deliverer.csv";
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            String csvData = CSVFileHelper.loadCSVFile(context, fileName);
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            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;
                                }
                            }
                        }
                    }
                }
    
            }
    
    
    fheyming's avatar
    fheyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
            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);
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                        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;
                                    }
                                }
                            }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
    
    
    Fabio Heyming's avatar
    Fabio Heyming committed
                            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(",");
    
                    Log.d("myTag", "ADDED 1 fav");
    
                    if (data.length >= 2) {
                        int cusId = Integer.parseInt(data[0]);
    
                        int shopItemId = Integer.parseInt(data[1]);
                        Log.d("myTag", "ADDED 2 fav");
    
                        for (Customer c : MampfMobil.customers) {
                            if (c.id == cusId) {
    
                                Log.d("myTag", "ADDED 3 fav");
    
                                for (Supplier s : MampfMobil.suppliers) {
                                    for (ShopItem sI : s.shopItems) {
    
                                        Log.d("myTag", sI.id + "" + shopItemId);
    
                                        if (sI.id == shopItemId) {
                                            c.addToFavourits(sI);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    
    
    
    
            String fileName8 = "shoppingCart.csv";
            String csvData8 = CSVFileHelper.loadCSVFile(context, fileName8);
            if (csvData8 != null) {
    
                String[] lines = csvData8.split("\n");
                for (String line : lines) {
                    String[] data = line.split(",");
                    if (data.length >= 3) {
                        int cusId = Integer.parseInt(data[0]);
                        int itemId = Integer.parseInt(data[1]);
                        int amount = Integer.parseInt(data[2]);
                        for (Customer c : MampfMobil.customers) {
                            if (c.id == cusId) {
                                for (Supplier s : MampfMobil.suppliers) {
                                    for (ShopItem sI : s.shopItems) {
                                        if (sI.item.id == itemId) {
                                            c.addToShoppingCart(new BestellungsTeil(sI.item,amount));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    Fabio Heyming's avatar
    Fabio Heyming committed
        }
    
    fheyming's avatar
    fheyming committed
    }