import javax.swing.*;

public class ShopProgram 
{
        public static String id;
        public static String brand;
        public static int stock;
        public static double price;
        public static double discount;
        
        public static ElectronicShop Products[] = new ElectronicShop[100];
        public static int counter = 0;
        
    public static void main(String[] args) throws Exception
    {   
        int mainChoice;
        
        JOptionPane.showMessageDialog(null, "## Welcome to ##\nThomas's Electronic Shop Program");
        do
        {
            mainChoice=Integer.parseInt(JOptionPane.showInputDialog
                    ("Main Menu" +
                    "\n-------------"+
                    "\n1) Add Product"+
                    "\n2) Display Products"+
                    "\n3) Search for a Product"+
                    "\n4) Update a Product's detail"+
                    "\n5) Buy a Product"+
                    "\n6) Exit Program"+
                    "\nPlease Enter The Choice Number:"));

            if(mainChoice == 1)
                addModule();
            else if(mainChoice == 2)
                displayModule();
            else if(mainChoice == 3)
                searchModule();
            else if(mainChoice == 4)
                updateModule();
            else if(mainChoice == 5)
                buyModule();
            else if(mainChoice == 6)
                JOptionPane.showMessageDialog
                    (null, "Good Bye!\nThank You For Using\nThomas's Electronic Shop Program");
            else
                JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again..");
        }while(mainChoice!=6);
    }
    
    public static void addModule()
    {
        int choice;
        do
        {
            id=JOptionPane.showInputDialog("Enter product id (0 - 999)");
            brand=JOptionPane.showInputDialog("Product Brand");
            stock=Integer.parseInt(JOptionPane.showInputDialog("Quantity of product in stock (data type: integer, example: 10)"));
            price=Double.parseDouble(JOptionPane.showInputDialog("Price (data type: double, example: 500.5)"));
            discount=Double.parseDouble(JOptionPane.showInputDialog("Discount (data type: double, example: 0.05 (0.01 - 1))"));
            
            Products[counter]= new ElectronicShop(id,brand,stock,price,discount);
            counter++;
           
            do
            {
                choice=Integer.parseInt(JOptionPane.showInputDialog
                    ("Add Another product?" +
                    "\n--------------------" +
                    "\n1) Yes" +
                    "\n2) No" +
                    "\nPlease enter the choice number:"));
                if(choice== 1)
                    JOptionPane.showMessageDialog(null, "Please Enter the next product's details");
                else if (choice == 2)
                    JOptionPane.showMessageDialog(null, "Back To Main Menu");
                else
                    JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again...");
            }while((choice!=1)&&(choice!=2));
        }while(choice!=2);
    }
    
    public static void displayModule()
    {
        for(int i=0;i<counter;i++)
        {
            JOptionPane.showMessageDialog(null, Products[i].displayDetail());
        }
        JOptionPane.showMessageDialog(null, "Back to Main Menu");
    }
    
    public static void searchModule()throws Exception
    {
        int choice;
        String targetBrand;
        boolean found;
        
        do
        {
            found = false;
            targetBrand=JOptionPane.showInputDialog("Please enter the product Brand");
        
            for(int i=0;i<counter;i++)
            {
                if(Products[i].brand.equalsIgnoreCase(targetBrand))
                {
                    found = true;
                    JOptionPane.showMessageDialog(null, Products[i].displayDetail());
                }
            }
            
            if (found==true)
                JOptionPane.showMessageDialog(null,"Your Product has been sucessfully searched!");
            else
                JOptionPane.showMessageDialog(null, "Product Not Found!");
            
            do
            {
                choice=Integer.parseInt(JOptionPane.showInputDialog
                    ("Search Another product?" +
                    "\n--------------------" +
                    "\n1) Yes" +
                    "\n2) No" +
                    "\nPlease enter the choice number:"));
                if(choice== 1)
                    JOptionPane.showMessageDialog(null, "Continue Search Module");
                else if (choice == 2)
                    JOptionPane.showMessageDialog(null, "Back To Main Menu");
                else
                    JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again...");
            }while((choice!=1)&&(choice!=2));
        }while(choice!=2);
    }
    public static void updateModule()
    {
        int i;
        int choice;
        int editChoice;
        boolean valid = false;
        int confirm;
        String targetID;
        boolean found;
        
        String newBrand;
        String newType;
        String newModel;
        int newYear;
        int newStock;
        double newPrice;
        double newDiscount;
        
        do
        {
            found = false;
            targetID=JOptionPane.showInputDialog
                    ("Update A Product's detail:" +
                    "\n----------------------------" +
                    "\nPlease enter the product ID");
        
            for(i=0;i<counter;i++)
            {
                if(Products[i].id.equalsIgnoreCase(targetID))
                {
                    found = true;
                    break;
                }
            }
            
            if (found==true)
            {
                do
                {
                    newBrand = Products[i].brand;
                    newStock = Products[i].stock;
                    newPrice = Products[i].price;
                    newDiscount = Products[i].price;
                    
                editChoice=Integer.parseInt(JOptionPane.showInputDialog
                        (
                        "Your Product has been found!\n" +
                        Products[i].displayDetail() +
                        "\nPlease Select Details to Update" +
                        "\n1) Update Brand" +
                        "\n2) Update Stock Quantity" +
                        "\n3) Update Price" +
                        "\n4) Update Discount" +
                        "\n5) Exit" +
                        "\nPlease enter the choice number:"
                        ));
                if(editChoice==1)
                {
                    valid = true;
                    newBrand=JOptionPane.showInputDialog("Please Enter New Product Brand:");
                    do
                    {
                        confirm=Integer.parseInt(JOptionPane.showInputDialog
                                ("Confirmation" +
                                "\n-------------" +
                                "\nChange product Brand" +
                                "\nFrom : "+Products[i].brand+
                                "\nTo : "+newBrand+
                                "\n\n1) Yes" +
                                "\n2) No" +
                                "\nPlease enter the choice number:"));
                        if(confirm==1)
                            Products[i].brand=newBrand;
                        else if(confirm==2)
                            JOptionPane.showMessageDialog(null,"Update Brand Cancelled");
                        else
                            JOptionPane.showMessageDialog(null, "Invalid Choice! Please Try Again...");
                    }while((confirm!=1)&&(confirm!=2));
                }
                else if(editChoice==2)
                {
                    valid = true;
                    newStock=Integer.parseInt(JOptionPane.showInputDialog("Please Enter New Stock Quantity:"));
                    do
                    {
                        confirm=Integer.parseInt(JOptionPane.showInputDialog
                                ("Confirmation" +
                                "\n-------------" +
                                "\nChange product stock quantity" +
                                "\nFrom : "+Products[i].stock+
                                "\nTo : "+newStock+
                                "\n\n1) Yes" +
                                "\n2) No" +
                                "\nPlease enter the choice number:"));
                        if(confirm==1)
                            Products[i].stock=newStock;
                        else if(confirm==2)
                            JOptionPane.showMessageDialog(null,"Update Stock Quantity Cancelled");
                        else
                            JOptionPane.showMessageDialog(null, "Invalid Choice! Please Try Again...");
                    }while((confirm!=1)&&(confirm!=2));
                }
                else if(editChoice==3)
                {
                    valid = true;
                    newPrice=Double.parseDouble(JOptionPane.showInputDialog("Please Enter New Price:"));
                    do
                    {
                        confirm=Integer.parseInt(JOptionPane.showInputDialog
                                ("Confirmation" +
                                "\n-------------" +
                                "\nChange product price" +
                                "\nFrom : $"+Products[i].price+
                                "\nTo : $"+newPrice+
                                "\n\n1) Yes" +
                                "\n2)No" +
                                "\nPlease enter the choice number:"));
                        if(confirm==1)
                            Products[i].price=newPrice;
                        else if(confirm==2)
                            JOptionPane.showMessageDialog(null,"Update Product Price Cancelled");
                        else
                            JOptionPane.showMessageDialog(null, "Invalid Choice! Please Try Again...");
                    }while((confirm!=1)&&(confirm!=2));
                }
                else if(editChoice==4)
                {
                    valid = true;
                     newDiscount=Double.parseDouble(JOptionPane.showInputDialog("Please Enter New Discount:"));
                    do
                    {
                        confirm=Integer.parseInt(JOptionPane.showInputDialog
                                ("Confirmation" +
                                "\n-------------" +
                                "\nChange product Discount" +
                                "\nFrom : "+Products[i].discount*100+ "%"+
                                "\nTo : "+newDiscount*100+ "%"+
                                "\n\n1) Yes" +
                                "\n2) No" +
                                "\nPlease enter the choice number:"));
                        if(confirm==1)
                            Products[i].discount=newDiscount;
                        else if(confirm==2)
                            JOptionPane.showMessageDialog(null,"Update Product Discount Cancelled");
                        else
                            JOptionPane.showMessageDialog(null, "Invalid Choice! Please Try Again...");
                    }while((confirm!=1)&&(confirm!=2));
                }
                else if(editChoice==5)
                {
                    valid = true;
                    JOptionPane.showMessageDialog(null, "Back to Update Module");
                }
                else
                {
                    valid = false;
                    JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again...");
                }
                }while((valid=false)||(editChoice!=5));
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Product Not Found!");
            }
            
            do
            {
                choice=Integer.parseInt(JOptionPane.showInputDialog
                    ("Update Another product?" +
                    "\n--------------------" +
                    "\n1) Yes" +
                    "\n2) No" +
                    "\nPlease enter the choice number:"));
                if(choice== 1)
                    JOptionPane.showMessageDialog(null, "Continue Update Module");
                else if (choice == 2)
                    JOptionPane.showMessageDialog(null, "Back To Main Menu");
                else
                    JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again...");
            }while((choice!=1)&&(choice!=2));
        }while(choice!=2);
    }
    public static void buyModule()
    {
        String targetID;
        int i;
        boolean found;
        int choice;
        int buyChoice;
        boolean valid=false;
        boolean validQuantity;
        boolean correctDetail = false;
        int quantity;
        int confirm;
        
        
        do
        {
            found = false;
            targetID=JOptionPane.showInputDialog
                    ("Buy Product" +
                    "\n------------" +
                    "\nPlease enter the product ID that you want to buy (0 - 999)");
        
            for(i=0;i<counter;i++)
            {
                if(Products[i].id.equalsIgnoreCase(targetID))
                {
                    found = true;
                    break;
                }
            }
            
            if (found==true)
            {
                do
                {
                    buyChoice=Integer.parseInt(JOptionPane.showInputDialog
                        (
                        "Your Product has been found!\n" +
                        Products[i].displayDetail() +
                        "\n" + Products[i].stockStatus() +
                        "\n---------------------------------" +
                        "\nPlease Choose" +
                        "\n1) Proceed" +
                        "\n2) Cancel" +
                        "\nPlease enter the choice number:"
                        ));
                
                if(buyChoice==1)
                {
                    valid = true;
                    if(Products[i].stock==0)
                    {
                        JOptionPane.showMessageDialog
                                (null, "Sorry, this product is "+Products[i].stockStatus());
                    }
                    else
                    {
                        do
                        {
                            do
                            {
                                quantity=Integer.parseInt(JOptionPane.showInputDialog
                                    ("Buy Product" +
                                    "\n-------------" +
                                    "\nQuantity of item available in stock " + Products[i].stock +
                                    "\nHow many units that you want to buy? (0 - "+Products[i].stock+")"
                                    ));
                                if (quantity>Products[i].stock)
                                {
                                    validQuantity = false;
                                    JOptionPane.showMessageDialog
                                        (null, "Sorry, You cannot buy more than available stock.");
                                }
                                else
                                {
                                    validQuantity = true;
                                }
                            }while(validQuantity==false);
                        
                            do
                            {
                                confirm=Integer.parseInt(JOptionPane.showInputDialog
                                    ("Buy Product" +
                                    "\n-------------" +
                                    "\nYou choosed to buy:" +
                                    "\n"+Products[i].displayDetail() +
                                    "\nQuantity = "+ quantity +
                                    "\nTotal Price = $" + (Products[i].discountedPrice()*quantity) +
                                    "\n---------------------------------------" +
                                    "\nIs this information correct?" +
                                    "\n1) Yes" +
                                    "\n2) No, Change the quantity of products to buy" +
                                    "\n3) Exit and cancel buying this product"
                                    ));
                                if(confirm==1)
                                {
                                    correctDetail = true;
                                    JOptionPane.showMessageDialog(null, "Item Purchased!");
                                    Products[i].stock = Products[i].stock - quantity;
                                }
                                else if(confirm==2)
                                {
                                    correctDetail = false;
                                }
                                
                                else if(confirm==3)
                                {
                                    correctDetail = true;
                                    JOptionPane.showMessageDialog(null,"Cancelled!");
                                }
                                else
                                {
                                    JOptionPane.showMessageDialog
                                        (null, "Invalid Choice! Please Try Again...");
                                }
                            }while((confirm!=1)&&(confirm!=2)&&(confirm!=3));
                        }while(correctDetail==false);
                    }
                }
                else if(buyChoice==2)
                {
                    valid = true;
                    JOptionPane.showMessageDialog(null,"Buy Product Cancelled");
                }
                else
                {
                    valid = false;
                    JOptionPane.showMessageDialog(null, "Invalid Choice! Please try again...");
                }
                }while(valid==false);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Product Not Found!");
            }
            
            do
            {
                choice=Integer.parseInt(JOptionPane.showInputDialog
                    ("Buy Another product?" +
                    "\n--------------------" +
                    "\n1) Yes" +
                    "\n2) No" +
                    "\nPlease enter the choice number:"));
                if(choice== 1)
                    JOptionPane.showMessageDialog(null, "Continue Buy Product Module");
                else if (choice == 2)
                    JOptionPane.showMessageDialog(null, "Back To Main Menu");
                else
                    JOptionPane.showMessageDialog(null, "Invalid Choice!\nPlease try again...");
            }while((choice!=1)&&(choice!=2));
        }while(choice!=2);
    }
}
