Alternative method to Calculate the final price of the Laptop or Desktop after certain discount of total cost according to criteria

import java.util.Scanner//Importing Scanner class from util package
public class Purchase
{
    //Declaring instance variables 
    private String name;
    private String address;
    private double purchase;
    private double finalPrice;

 
public static void main(String []args) //Main method
    {
        System.out.println("Press L for laptop and D for desktop"); //Instruction for User
        char ch=new Scanner(System.in).next().charAt(0); //Getting Character from user
                 
                if (ch=='L'|| ch=='l') //Checking condition
                {
                    Purchase l= new Purchase();
                    l.inputLaptop();
                    l.calcLaptop();
                    l.display();
                }
                else if(ch=='D' || ch=='d') //Checking condition
                {
                    Purchase d= new Purchase();
                    d.inputDesktop();
                    d.calcDesktop();
                    d.display();
                }
                else
                    System.out.println("oops!! Sorry, Please enter valid character"); //Printing error message
             
    }


public void inputLaptop() //Non return type,Non parameterized method
    {
   
        System.out.println("Enter your Name: "); //Instruction to user
        namenew Scanner(System.in).nextLine();
   
        System.out.println("Enter your Address"); //Instruction to user
        addressnew Scanner(System.in).nextLine();
   
        System.out.println("Enter the cost price of Laptop: "); //Instruction to user
        purchasenew Scanner(System.in).nextDouble();
   
    }



public void inputDesktop() //Non return type,Non parameterized method
    {
   
        System.out.println("Enter your Name: "); //Instruction to user
        namenew Scanner(System.in).nextLine();
   
        System.out.println("Enter your Address"); //Instruction to user
        address = new Scanner(System.in).nextLine();
   
        System.out.println("Enter the cost price of Desktop: "); //Instruction to user
        purchasenew Scanner(System.in).nextDouble();
   
    }


public double calcLaptop() //Return type,Non parameterized method
    {
        //Conditions according to the above criteria
        if(purchase<=25000)
        {
            finalPricepurchase-0;
        }
        else if(purchase>25000 && (purchase<=57000))
        {
            finalPricepurchase-((5*purchase)/100);
        }
        else if(purchase>57000 && (purchase<=100000))
        {
            finalPricepurchase-((7.5*purchase)/100);
        }
        else if(purchase>100000)
        {
            finalPricepurchase-((10*purchase)/100);
        }
        return finalPrice//Returning the result value of variable finalprice
    }



public double calcDesktop() //Return type,Non parameterized method
    {
        //Conditions according to the above criteria
        if(purchase<=25000)
        {
            finalPricepurchase-((5*purchase)/100);
        }
        else if(purchase>25000 && (purchase<=57000))
        {
            finalPricepurchase-((7.5*purchase)/100);
        }
        else if(purchase>57000 && (purchase<=100000))
        {
            finalPricepurchase-((10*purchase)/100);
        }
        else if(purchase>100000)
        {
            finalPricepurchase-((15*purchase)/100);
        }
        return finalPrice//Returning the result value of variable finalprice
    }



public void display() //Non return type,Non parameterized method
    {
        //Printing results
        System.out.println("Your Name = "+name);
        System.out.println("Your Address = "+address);
        System.out.println("Final Price after discount = "+finalPrice);
    }
}


 Output of the above program is:-

   Enter L to get access to Laptop section and D to get access to Desktop section
   l
   Enter your Name:
   Gaman Aryal
   Enter your Address
   Pepsicola
   Enter the cost price of Laptop:
   175000
   Your Name = Gaman Aryal
   Your Address = Pepsicola
   Final Price after discount = 157500.0 


Comments

Popular posts from this blog

Java Program to make a comparison between numbers

Java program using Scanner Class

Java program to print 'Hello World'