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
}
{
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
name= new Scanner(System.in).nextLine();
System.out.println("Enter your Address"); //Instruction to user
System.out.println("Enter the cost price of Laptop: "); //Instruction to user
purchase= new Scanner(System.in).nextDouble();
}
{
System.out.println("Enter your Name: "); //Instruction to user
name= new 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 Laptop: "); //Instruction to user
purchase= new Scanner(System.in).nextDouble();
}
public void inputDesktop() //Non return type,Non parameterized method
{
System.out.println("Enter your Name: "); //Instruction to user
System.out.println("Enter your Address"); //Instruction to user
System.out.println("Enter the cost price of Desktop: "); //Instruction to user
}
{
System.out.println("Enter your Name: "); //Instruction to user
name= new 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
purchase= new Scanner(System.in).nextDouble();
}
public double calcLaptop() //Return type,Non parameterized method
{
//Conditions according to the above criteria
if(purchase<=25000)
{
finalPrice= purchase-0;
}
else if(purchase>25000 && (purchase<=57000))
{
finalPrice= purchase-((5*purchase)/100);
}
else if(purchase>57000 && (purchase<=100000))
{
finalPrice= purchase-((7.5*purchase)/100);
}
else if(purchase>100000)
{
finalPrice= purchase-((10*purchase)/100);
}
return finalPrice; //Returning the result value of variable finalprice
}
{
//Conditions according to the above criteria
if(purchase<=25000)
{
finalPrice= purchase-0;
}
else if(purchase>25000 && (purchase<=57000))
{
finalPrice= purchase-((5*purchase)/100);
}
else if(purchase>57000 && (purchase<=100000))
{
finalPrice= purchase-((7.5*purchase)/100);
}
else if(purchase>100000)
{
finalPrice= purchase-((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)
{
finalPrice= purchase-((5*purchase)/100);
}
else if(purchase>25000 && (purchase<=57000))
{
finalPrice= purchase-((7.5*purchase)/100);
}
else if(purchase>57000 && (purchase<=100000))
{
finalPrice= purchase-((10*purchase)/100);
}
else if(purchase>100000)
{
finalPrice= purchase-((15*purchase)/100);
}
return finalPrice; //Returning the result value of variable finalprice
}
{
//Conditions according to the above criteria
if(purchase<=25000)
{
finalPrice= purchase-((5*purchase)/100);
}
else if(purchase>25000 && (purchase<=57000))
{
finalPrice= purchase-((7.5*purchase)/100);
}
else if(purchase>57000 && (purchase<=100000))
{
finalPrice= purchase-((10*purchase)/100);
}
else if(purchase>100000)
{
finalPrice= purchase-((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);
}
}
{
//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
Post a Comment