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

An electronics shop has announced the following seasonal discounts on the purchase of certain items. 


Purchase Amount in Rs.
 Discount on Laptops
Discount on Desktop PC
 0 - 25,000
0.0%
5.0%
25,001 – 57,000
 5.0%
 7.5%
 57,001 – 1,00,000
7.5%
 10.0%
 More than 1,00,000
 10.0%
 15.0%


  • At first Scanner class is called to read the value from the user.
  • Then Required variables are declared.
  • Then String type is declared to enter the section of the laptop or desktop.
  • Then according to the question if the given string is L,l then the program enters into laptop section otherwise it enters int desktop section.

you need some data and instruction from the user in this program So, you need to import the Scanner class. Here 'Scanner' class, the built-in class inside the 'util' package of 'java' project is imported to get value from the keyboard.
 If you want to know more about 'Scannerclass then, please visit this link. (https://programmerbrothers.blogspot.com/2019/04/scanner-class-in-java-scanner-is-class.html)

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
    {
        String type;
     
        System.out.println("Enter L to get access to Laptop section and D to get access to Desktop section ");
        type = new Scanner(System.in).nextLine();
     
        String s =new String(type); //Creating object of String class
     
        boolean s1 = s.equals("l");
        boolean s2 = s.equals("L");
        boolean s3 = s.equals("d");
        boolean s4 = s.equals("D");

        if (s1==true || (s2==true))
        {
            Purchasel= new Purchase();
            l.inputLaptop();
            l.calcLaptop();
            l.display();
        }
        else if(s3==true || (s4==true))
        {
            Purchased= new Purchase();
            d.inputDesktop();
            d.calcDesktop();
            d.display();
        }
        else
        {
            System.out.println("oops!! Sorry, Please enter valid character"); //Printing error message
        }
    }
 
Many methods like inputLaptop(), inputDesktop()and etc.. are defined in this program to solve the problem easily. Methods really make the programs easier. And if you are interested to know more about the method then visit this link. (https://programmerbrothers.blogspot.com/2019/04/basic-concept-of-method-in-java.html)

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
        purchasenewScanner(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 using Scanner Class

Java Program to make a comparison between numbers

Calculate the area of Rectangle Using Constructor Overloading rules