Search This Blog

Wednesday 16 March 2022

Make a JAVA program for a Toystore. This program suggest the toy based on the inputs users gives like age , gender , etc

 //editor used - IntelliJ IDEA


package ToyStore;

import javax.swing.*;

import java.awt.event.*;

import java.util.Scanner;

//here you can edit the available toys in the store according to the specific price and gender

class ToyStore {

     final String[] BrdToys =new String[]{"Chess"," Ludo"," Chinese Checkers"," Monopoly"," Pictionary" };  //String

     final int[] BrdToysPrice= new int[]{ 15,12,20,25,30}; //price in euros

     final String[] SoftToys = new String[]{"Stuffed Animals","Plush Pillows","Teddy Bear","Octopus","Unicorn"};

     final int[] STPrice=new int[] {20,26,25,20,26};

    final String[] AfigureF = new String[]{"Barbie","Black Widow","Wonder Woman","Valkyrie","Catwoman"};

     final int[] AfigFPrice= new int[]{20,25,20,26,30,25};

     final String [] AfigureB=new String[]{"Iron Man","Spider Man","Hulk ","Thor"," AquaMan"};

    final int[] AfigBPrice= new int[]{20,25,20,26,30,25};

     final String[] CarToys= new String[]{"Monster Truck","HotWheels","RC Cars\t","Trucks","RoboCar"};

     final int[] CarToyP= new int[]{20,35,20,26,30,25};


     public void DisplayAllToys()

     {

         System.out.println("<---Board Games---> " );

         for (int i = 0; i <5 ; i++) {

             System.out.print(BrdToys[i]);

         }

         System.out.println("");

         System.out.println("<---Soft Toys---> " );

         for (int i = 0; i <5 ; i++) {

             System.out.print(SoftToys[i]+" ");

         }

         System.out.println("");

         System.out.println("<---Action Figures---> " );

         for (int i = 0; i <5 ; i++) {

             System.out.print(AfigureB[i]+" \t"+AfigureF[i]+" ");

         }

         System.out.println("");

         System.out.println("<---Toy Cars---> " );

         for (int i = 0; i <5 ; i++) {

             System.out.print(CarToys[i]+" ");

         }

     }

}

 class Customers extends ToyStore{

        static String gender;

        static int age;

        static double priceInput;

        public void input()

        {

            Scanner sc = new Scanner(System.in);

            System.out.println("WELCOME TO GUCCI TOY STORE!");

            System.out.println("Enter your child's age: ");

            age = sc.nextInt();

            if(age<=13 && age>=3) //assuming that maximum and minimum age for a child are 13y and 6y respectively

            {

                boolean x = true;

                while(x)

                {

                    System.out.println("Enter your child's gender (Male/Female) :");

                    gender = sc.next();

                    if (gender.equalsIgnoreCase("male") || gender.equalsIgnoreCase("female"))

                    {

                        x = false;

                        boolean y = true;

                        while(y)

                        {

                            System.out.println("Enter your maximum budget (>10 Euros)"); //assuming the toy with the lowest price is 10 Euros

                            priceInput = sc.nextDouble();

                            if (priceInput < 10.0 || priceInput > 69.99) {

                                System.out.println("Entered budget must at least be 10 Euros or at max be 60 Euros!");


                            }else

                            y = false;

                        }

                    }

                else

                    System.out.println("Wrong input!");

                }

            }

            else

                System.out.println("Please enter a valid age (b/w 6 and 13 years old)");

            //return age,priceInput;

        }

    void Choice()

    {

        System.out.println(" Press 1 to Display all Toys.  Press 2 for Filling Details");

        ToyStore ts=new ToyStore();

        Customers cs=new Customers();


            Scanner sc= new Scanner(System.in);


           while(true) {

               int ch=sc.nextInt();

                if (ch == 1) {


                    ts.DisplayAllToys();

                    System.out.println(" \nPress 2 for Filling Details");


                }

                else if(ch ==2 )

                {

                    cs.input();

                    break;

                }

                else

                {

                    System.out.println("Enter Valid Choice");

                }

           }

        }

    public static void main(String[] args)

    {

            ToyPicker tp=new ToyPicker();

            Customers cs = new Customers();

            cs.Choice();

            tp.assistant();

    }

 }

public class ToyPicker extends ToyStore {
    ToyStore ts = new ToyStore();
    Scanner sc = new Scanner(System.in);
    int cat;

    void assistant() {

        boolean x = true;
        while (x) {
            System.out.println("""
                Enter the type of Toy you want:\s
                 1 >> Board Toys\t(age 8+)
                 2 >> Soft Toys\t(age 3+ (F))
                 3 >> Action Figures\t(age 5+)
                 4 >> Toy Cars\t(age 3+ (M))
                 5 >> Exit Menu Press 5""");
            cat = sc.nextInt();  //for choosing the Toy Category
            switch (cat) {
                case (1) -> {
                    if (Customers.age >= 8) {
                        System.out.println("The toys suggested is 'MonoPoly' \n" +
                                "To show more suggestions press 9");         /*System.out.println("Your Child is underage for this Toy ,
                                You can browse other Toys")*/
                        ShowMore(ts.BrdToys, ts.BrdToysPrice);

                    } else
                        System.out.println("You are under age for this category of toy");
                }
                case (2) -> {
                    if (Customers.age >= 3)
                        if (Customers.gender.equals("female")) {
                            System.out.println("The toys suggested is 'Teddy Bear','Unicorn'\n To show more suggestions press 9");
                            ShowMore(ts.SoftToys, ts.STPrice);
                        } else
                            System.out.println("No Other Toys Available");
                }
                case (3) -> {
                    if (Customers.age >= 5) if (Customers.gender.equals("female")) {
                        System.out.println("The toys suggested is 'Barbie'\t\n To show more suggestions press 9");
                        ShowMore(ts.AfigureF, ts.AfigFPrice);
                    } else {
                        System.out.println("The toys suggested is 'Hulk'\t\n To show more suggestions press 9");
                        ShowMore(ts.AfigureB, ts.AfigBPrice);
                    }
                    else {
                        System.out.println("No toys available for this age");
                    }
                }
                case (4) -> {
                    if (Customers.age >= 3) {
                        System.out.println("The toys suggested is 'Monster Truck'\t\n To show more suggestions press 9");
                        ShowMore(ts.CarToys, ts.CarToyP);
                    }
                }
                case (5) -> x = false;
                default -> System.out.println("Enter valid choice only");
            }

        }
    }
    void ShowMore(String[] show, int[] showPrice)  //to show more Toys Options
    {
        int sM = sc.nextInt();
        if (sM == 9) {
            new Checkout(show, showPrice);
        } else {
            System.out.println("Unexpected Input:" + sM);
        }
    }
}


 class Checkout extends JFrame implements ActionListener {

    static int[] Price = new int[5];

    static String[] FinOrder = new String[5];

    JLabel l;

    JCheckBox cb1, cb2, cb3, cb4, cb5;

    JButton b;


    Checkout(String[] order, int[] price)

    //Checkout is to pop a window with all the category Toys and Let the user choose it and order.

    {

        for (int i = 0; i < 5; i++) {

            Price[i] = price[i];

            FinOrder[i] = order[i];

        }

        int i = 0;

        l = new JLabel("Toy Selection System");

        l.setBounds(100, 50, 1080, 50);

        while (i == 0) {

            cb1 = new JCheckBox(" " + order[i] + " \t@" + price[i]+ '€');

            cb1.setBounds(100, 100, 150, 20);

            cb2 = new JCheckBox(" " + order[i + 1] + " \t@" + price[i + 1] + '€');

            cb2.setBounds(100, 150, 150, 20);

            cb3 = new JCheckBox(" " + order[i + 2] + " \t@" + price[i + 2] + '€');

            cb3.setBounds(100, 200, 200, 20);

            cb4 = new JCheckBox(" " + order[i + 3] + " \t@" + price[i + 3] + '€');

            cb4.setBounds(100, 250, 150, 20);

            cb5 = new JCheckBox(" " + order[i + 4] + " \t@" + price[i+4]  + '€');

            cb5.setBounds(100, 300, 150, 20);

            b = new JButton("Order");

            b.setBounds(100, 400, 80, 30);

            b.addActionListener(this);

            add(l);add(cb1);add(cb2);add(cb3);add(cb4);add(cb5);add(b);

            setSize(500, 700);

            setLayout(null);

            setVisible(true);

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            i++;

        }

    }


    public void actionPerformed(ActionEvent e) {

        float amount = 0;

        String msg = " ";

        if (cb1.isSelected()) {

            amount += Checkout.Price[0];

            msg = Checkout.FinOrder[0] + " : " + Checkout.Price[0] + "€\n";

        }

        if (cb2.isSelected()) {

            amount += Checkout.Price[1];


            msg += Checkout.FinOrder[1] + " : " + Checkout.Price[1] + "€\n";

        }

        if (cb3.isSelected()) {

            amount += Checkout.Price[2];

            msg += Checkout.FinOrder[2] + " : " + Checkout.Price[2] + "€\n";

        }

        if (cb4.isSelected()) {

            amount += Checkout.Price[3];

            msg += Checkout.FinOrder[3] + " : " + Checkout.Price[3] + "€\n";

        }

        if (cb5.isSelected()) {

            amount += Checkout.Price[4];

            msg += Checkout.FinOrder[4] + " : " + Checkout.Price[4] + "€\n";

        }

        msg += "-----------------\n";

        JOptionPane.showMessageDialog(this, msg + "Total: " + amount+"€");

        if(amount<=Customers.priceInput)

        {

            JOptionPane.showMessageDialog(this, msg + "Transaction Successful: " + amount+"€");

        }

        else{

            JOptionPane.showMessageDialog(this, msg + "Transaction Unsuccessful: " + amount+"€");

        }

    }

}


Output 











Tuesday 8 March 2022

Two Sums Problem | Leet Code


Given an array of integers, return indices of the two numbers such that they add up to a specific target.

 You may assume that each input would have exactly one solution, and you may not use the same element twice.

 Example:

 Given nums = [2, 7, 11, 15], target = 9,

 Because nums[0] + nums[1] = 2 + 7 = 9,

 return [0, 1].



Code :


/*

Using a HashMap, O(n) space and O(n) time.

    Thinking process:

    Push everything into a HashMap.

    Check if one element exist in the HashMap, if so save it. Meanwhile, save the other one.

    Trick: after adding into the HashMap, we are looking for the 2nd index first.

        Always check (target - current) from the HashMap.

        If exist, that means index0 has already been pushed into the HashMap and current value is at index1.

    (key, value) = (numbers[i], i)

    Note: return index+1 because this is not 0-based.

*/

class Solution1 {

    public int[] twoSum(int[] nums, int target) {

        int[] rst = new int[2];

        if (nums == null || nums.length <= 1) return rst;


        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {

            if (map.containsKey(target - nums[i])) {

                rst[0] = map.get(target - nums[i]);

                rst[1] = i;

                break;

            }

            map.put(nums[i], i);

        }

        return rst;

    }

}

Make a JAVA program for a Toystore. This program suggest the toy based on the inputs users gives like age , gender , etc

 //editor used - IntelliJ IDEA package ToyStore; import javax.swing.*; import java.awt.event.*; import java.util.Scanner; //here you can edi...