Coders Packet

Making Online Shopping Portal Using JAVA

By Gaurav Dhavala

Hi, Today we will learn how to make an online shopping portal using JAVA with two categories of customers.

We will differentiate normal and prime customers in which prime customers will get discounts automatically and regular customers will have to apply coupons to avail discounts wrt the cart value.

To make this we will use the overloading concept. In when JAVA when two or more methods have the same name and have the same class but have different parameters, so they act as two different functions, and the method is called overloading. 

Algorithm

1) First we will create a check class.

2) Using overloading in check we will create two methods, both will have the same name "checkOut" having different parameters. One checkOut is for prime members and one for regular members.

3) Now we will create our main class "OnlineShoppingPortal" which will give ask the customer's category and will give discounts accordingly.

Logic

If the cart value is more than 1000, a prime customer will get a discount of 30% else they will get 20% discount. For regular customers, if the cart value is more than 500 and if they provide the promo code "GET10" then they will get 10% discount else they will get Rs. 10 off.

Code

import java.util.Scanner;

class Check {
    public static float checkOut(float amount) {
        float final_amount = 0;
        if (amount > 0 && amount < 1000) {
            final_amount = (float) (amount - amount * (20 / 100.0));
        } else if (amount > 1000) {
            final_amount = (float) (amount - amount * (30 / 100.0));
        }
        return final_amount;
    }

    public static float checkOut(String promocode, float amount) {
        float final_amount;
        if (amount > 500 && promocode.equals("GET10")) {
            final_amount = (float) (amount - amount * 10 / 100.0);
        } else {
            final_amount = (float) (amount - 10.0);
        }
        return final_amount;
    }
}

class OnlineShoppingPortal {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Do you have Prime membership? ");
        String s1 = input.next();
        if(s1.equals("yes")){
            System.out.println("Enter cart value:");
            float total_amount = input.nextFloat();
            System.out.println("Total amount is " + total_amount +" and final amount after discount is "+ Check.checkOut(total_amount));
    }
        else{
            System.out.println("Enter cart value:");
            float total_amount = input.nextFloat();
            System.out.println("Enter promocode");
            String code = input.next();
            System.out.println("Total amount is "+ total_amount +" and final amount after discount is "+ Check.checkOut(code, total_amount));
        }
    }
}

Output

output

Download project

Reviews Report

Submitted by Gaurav Dhavala (gauravdhavala)

Download packets of source code on Coders Packet