import java.text.DecimalFormat;
import java.util.Scanner;
class Calculations
{
//global declarations
private static final DecimalFormat tspaces = new DecimalFormat("0.00");
double totalamt;
public void DataAccept() //method to accept data
{
System.out.println("This program will determine the cash back amount of any purchases made using your visa.");
Scanner myInput = new Scanner(System.in);
System.out.println("Enter the amount of your purchase: ");
totalamt = myInput.nextDouble();
double famount = totalamt * 0.015;
double samount = totalamt * 0.0175;
double tamount = totalamt * 0.0225;
double fcashamt = totalamt - famount;
double scashamt = totalamt - samount;
double tcashamt = totalamt - tamount;
if(totalamt < 200) //conditions and calculations are done here
{
System.out.println("Your total cash amount earned is $"+ tspaces.format(famount));
String yesorno = "";
Scanner ynInput = new Scanner(System.in);
System.out.println("Would you like to apply the money to your purchase or save it for a future credit? (y) for purchase (n) for future credit:");
yesorno = ynInput.nextLine();
if(yesorno.equals("y") || yesorno.equals("Y")) //if user responses y, he gets a different output and if responses n then gets another output
{
System.out.println("You have decided to apply your cash credit to your current purchase of $" + tspaces.format(totalamt) + ".\nYour final bill after applying your cash credit amount of $"+ tspaces.format(famount) + " is now $"+ tspaces.format(fcashamt) + ".\nThank you for your purchase.");
}
else
{
System.out.println("You have opted to save your cash credit for future purchases. Your current purchase amount is $" + tspaces.format(totalamt) + ".\nYou have a credit of $" + tspaces.format(famount) + " that you may use for future purchases.");
}
}
else if(totalamt >= 200 && totalamt < 1000) //conditions and calculations are done here
{
System.out.println("Your total cash amount earned is $"+ tspaces.format(samount));
String yesorno = "";
Scanner ynInput = new Scanner(System.in);
System.out.println("Would you like to apply the money to your purchase or save it for a future credit? (y) for purchase (n) for future credit:");
yesorno = ynInput.nextLine();
if(yesorno.equals("y") || yesorno.equals("Y")) //if user responses y, he gets a different output and if responses n then gets another output
{
System.out.println("You have decided to apply your cash credit to your current purchase of $" + tspaces.format(totalamt) + ".\nYour final bill after applying your cash credit amount of $"+ tspaces.format(samount) + " is now $"+ tspaces.format(scashamt) + ".\nThank you for your purchase.");
}
else
{
System.out.println("You have opted to save your cash credit for future purchases. Your current purchase amount is $" + tspaces.format(totalamt) + ".\nYou have a credit of $" + tspaces.format(samount) + " that you may use for future purchases.");
}
}
else if(totalamt > 1000) //conditions and calculations are done here
{
System.out.println("Your total cash amount earned is $"+ tspaces.format(tamount));
String yesorno = "";
Scanner ynInput = new Scanner(System.in);
System.out.println("Would you like to apply the money to your purchase or save it for a future credit? (y) for purchase (n) for future credit:");
yesorno = ynInput.nextLine();
if(yesorno.equals("y") || yesorno.equals("Y")) //if user responses y, he gets a different output and if responses n then gets another output
{
System.out.println("You have decided to apply your cash credit to your current purchase of $" + tspaces.format(totalamt) + ".\nYour final bill after applying your cash credit amount of $"+ tspaces.format(tamount) + " is now $"+ tspaces.format(tcashamt) + ".\nThank you for your purchase.");
}
else
{
System.out.println("You have opted to save your cash credit for future purchases. Your current purchase amount is $" + tspaces.format(totalamt) + ".\nYou have a credit of $" + tspaces.format(tamount) + " that you may use for future purchases.");
}
}
}
}
public class CashVisa2
{
public static void main(String args[])
{
Calculations c = new Calculations(); //object of the class calculations
c.DataAccept(); //calling method of that class
}
}
This program has discount calculations on customer purchases. If it's less than 200$ then they get 1.5%, for more than 200$ they get 1.75% and for more than 1000$ they get 2.25%. I used basic if
/else
to get the output, but even the simplest code took 87 lines. And there is a 'yes'/'no' option to credit their discounted money or use it in the current bill, so I had to write different SOP statements in each loop so different output will be showed.
Is there any other way of doing it that will make the program smaller? (Less in size and lines)