Java calculate discount

By | 11/11/2014

The program asks for a price, checks if it is higher than 1000. If the price is less, it checks if it’s higher than 600 and prints the discounted price or a notification that the price wasn’t high enough.

CalculateDiscount.java:

import java.util.Scanner;

public class CalculateDiscount {
 public void run()
 {
  Scanner reader=new Scanner(System.in);
  String over6hundred="8%", over1k="16%", discount;
  double productPrice;
		
  System.out.print("Enter a product's price in euros: ");
  productPrice=reader.nextDouble();
  reader.close();
		
  if(productPrice>1000){
   discount=String.format("%.2f",(productPrice*0.84));
   System.out.println("You're entitled to a "+over1k+" discount. New price: "+discount+"€");
  }
  else if(productPrice>600){
   discount=String.format("%.2f",(productPrice*0.92));
   System.out.println("You're entitled to a "+over6hundred+" discount. New price: "+discount+"€");
  }
  else{
   System.out.println("You are not entitled to any discounts.");
  }
 }
 public static void main(String[] args)
 {
  CalculateDiscount program = new CalculateDiscount();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *