Total Cost Solver in Java

Download
Download is available until [expire_date]
  • Version
  • Download 45
  • File Size 4.41 KB
  • File Count 1
  • Create Date May 12, 2016
  • Last Updated May 12, 2016

Total Cost Solver in Java

Total Cost Solver in Java

A sample source codes in java that will compute the total cost you have purchased. The formula is total cost is equal to the price of the item multiply to the quantity of the item you want to purchase.

Ex:
Apple is $1.50 each and you want to get 4 apples so the total cost is $6.
Total cost = $1.50 x 4 which is $6.

Source code:

import javax.swing.*;
public class TotalCostSolverGUI
{ 
public static void main(String args[]) {

String input1; 
int qty ; 
double result=0;
String cont="n";
String operator;

do
{
	operator=JOptionPane.showInputDialog(null, "Please select item:" + "
" + "1 for strawberry($1.99/pc)" + "
" + "2 for apple($2.50/pc)" + "
" + "3 for orange($2.66/pc)" + "
" + "4 for pineapple($3.50/pc)" + "
" + "5 for banana($1.50/pc)");
	
	input1 = JOptionPane.showInputDialog(null,"Enter quantity: ");
	qty=Integer.parseInt(input1);


if (operator.matches("1")){
result = 1.99 * qty ;
JOptionPane.showMessageDialog(null,"you have purchased " + qty + " strawberry($1.99/pc) total cost: " + result );
} else if (operator.matches("2")){
result = 2.50 * qty ;
JOptionPane.showMessageDialog(null,"you have purchased " + qty + " apple($2.50/pc) total cost: " + result );
}else if (operator.matches("3")){
result = 2.66 * qty ;
JOptionPane.showMessageDialog(null,"you have purchased " + qty + " orange($2.66/pc) total cost: " + result );
} else if (operator.matches("4")){
result = 3.50 * qty ;
JOptionPane.showMessageDialog(null,"you have purchased " + qty + " pineapple($3.50/pc) total cost: " + result );
} else if (operator.matches("5")){
result = 1.50 * qty ;
JOptionPane.showMessageDialog(null,"you have purchased " + qty + " banana($1.50/pc) total cost: " + result );	
}
else{
JOptionPane.showMessageDialog(null,"Invalid input" 	);
	}


	
cont =	JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
	}while (cont.matches("y"));

JOptionPane.showMessageDialog(null,"Program closing"  );


	}
}

, , , , , ,

Post navigation

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.