Professional Documents
Culture Documents
Java Codes For Stamp Duty Calculator
Java Codes For Stamp Duty Calculator
// s3243335
// This file contains Part A, Part B, Part C, Part D and Part E
// of the requirements for Assignment 1
// Import Classes
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
import java.io.*;
public class MyAssignment1 {
/**
* @param args
*/
public static void main(String[] args)
throws IOException {
int enteredNumber = 0;
double purchaseAmount = 0;
String enteredString = "";
char selectedOption = 0;
// Menu option char constants
final char PRIME_NUMBER_CHECKER_OPTION = 'a';
final char STAMP_DUTY_OPTION = 'b';
final char VOWEL_COUNTER_OPTION = 'c';
final char EXIT_OPTION = 'x';
if (args.length > 0) {
String x = args[0];
selectedOption = x.charAt(0);
String y = args[1];
switch(selectedOption) {
case PRIME_NUMBER_CHECKER_OPTION:
enteredNumber = Integer.parseInt(y);
break;
case STAMP_DUTY_OPTION:
purchaseAmount = Double.parseDouble(y);
break;
case VOWEL_COUNTER_OPTION:
enteredString = y.toString();
// Create a BufferedReader to read entered text
BufferedReader myReader = new BufferedReader
(new InputStreamReader (System.in));
// Create a Scanner to read entered primitive types
Scanner input = new Scanner(System.in);
//////////////////
// Start Menu Loop
// The menu will be repeatedly displayed to the user
// while their selected menu option remains invalid
/////////////////
// initialize the loop control variable
//////////////////////////////////////////char selectedOption = 0;
// Start the menu loop
while (selectedOption != PRIME_NUMBER_CHECKER_OPTION
&& selectedOption != STAMP_DUTY_OPTION
&& selectedOption != VOWEL_COUNTER_OPTION
&& selectedOption != EXIT_OPTION) {
// Display application menu to user
System.out.println("*************** Assignment 1 Menu ***************\n");
System.out.println(PRIME_NUMBER_CHECKER_OPTION + " Prime
number checker.");
System.out.println(STAMP_DUTY_OPTION + " Stamp duty calculator.");
System.out.println(VOWEL_COUNTER_OPTION + " Vowel counter.");
System.out.println(EXIT_OPTION + " Exit the program.");
System.out.println("\n*************************************************");
System.out.print("Please select one of the above options: ");
// Capture selected option
if (selectedOption == 0) {
selectedOption = myReader.readLine().charAt(0);
}
// Switch statements to determine which statements to run
// The test condition is the value of the char entered by the user
switch(selectedOption) {
// Start Prime Number Checker
case PRIME_NUMBER_CHECKER_OPTION:
// Prompt the user for a number
// and capture the value
System.out.print("\nEnter an integer value: ");
////////int enteredNumber =
input.nextInt();
if (enteredNumber == 0) {
enteredNumber = input.nextInt();
}
// Use the Math.sqrt function to determine the
// highest possible factor for the entered number
int highestPossibleFactor = (int)
Math.sqrt(enteredNumber);
// Initialize loop control variable and
// initialize the prime number flag
int i = 2;
boolean isAPrime = true;
// Start the loop
// The loop continues while the control variable
// is less than or equal to the highest possible factor
// of the number entered but it may be terminated if
// the entered number % i does not produce a
remainder.
while (i <= highestPossibleFactor) {
if (enteredNumber % i == 0) {
// the prime flag is set to false, the user is
told
// the number is not a prime and the loop
stops
isAPrime = false;
System.out.println(enteredNumber + " is
not a prime number!\n");
break;
}
else
// the prime flag is set to true
isAPrime = true;
i++;
}
// If the prime flag remains true at the end of the loop
// the user is told the number is a prime
if (isAPrime == true){
System.out.println(enteredNumber + " is a
prime number!\n");
}
// Reset the loop control variable to 0
// and continue looping the menu
enteredNumber = 0;
selectedOption = 0;
continue;
// Start Stamp duty calculation
case STAMP_DUTY_OPTION:
// Declare the following constants to define duty
brackets
final double BRACKET_ONE = 20000;
final double BRACKET_TWO = 115000;
final double BRACKET_THREE = 870000;
final double BRACKET_FOUR = 870001;
// Prompt the user to enter the purchase
amount
// Capture the amount entered
System.out.print("\nEnter the purchase amount:
");
////////////////////////////////////////////////////double
purchaseAmount = input.nextInt();
if (purchaseAmount == 0) {
purchaseAmount = input.nextInt();
}
// Variables for calculation within the loop
double dutyPayable;
double difference;
// Declare a NumberFormat object to enable
the
// duty payable to be formatted as currency
NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance(Locale.US);
// If the purchase amount is less than bracket
one
// calculate the duty payable on 1.4% and
// display the duty payable to the user
if (purchaseAmount < BRACKET_ONE) {
dutyPayable = (purchaseAmount * 0.014);
System.out.println("\nYour stamp duty
payable " + (currencyFormat.format(purchaseAmount) + " is " +
(currencyFormat.format(dutyPayable) + "\n")));
}
// If the purchase amount is more than
bracket one
// and less than or equal to bracket two
// find how many dollars are within the
range (difference)
// Then calculate duty: bracket one
component @ 1.4%
// add this to the difference calculated @
2.4%
// display the duty payable to the user
else if (purchaseAmount >
BRACKET_ONE && purchaseAmount <= BRACKET_TWO) {
difference = (purchaseAmount
BRACKET_ONE);
dutyPayable = (BRACKET_ONE *
0.014) + (difference * 0.024);
System.out.println("\nYour stamp
duty payable " + (currencyFormat.format(purchaseAmount) + " is " +
(currencyFormat.format(dutyPayable) + "\n")));
}
// If the purchase amount is more than
bracket two
// and less than or equal to bracket three
// find how many dollars are within the
range (difference)
// Then calculate duty: bracket one
component @ 1.4%
// add this to bracket two component @
2.4
// add this to the difference calculated @
6%
// display the duty payable to the user
else if (purchaseAmount >
BRACKET_TWO && purchaseAmount <= BRACKET_THREE) {
difference = (purchaseAmount
BRACKET_TWO);
dutyPayable = (BRACKET_ONE *
0.014) + ((BRACKET_TWO BRACKET_ONE) * 0.024)
+ (difference * 0.06);
System.out.println("\nYour stamp
duty payable " + (currencyFormat.format(purchaseAmount) + " is " +
(currencyFormat.format(dutyPayable) + "\n")));
}
// If the purchase amount is more than
bracket four
// calculate duty as bracket four @ 5.5%
// display the duty payable to the user
else if (purchaseAmount >
BRACKET_FOUR) {
dutyPayable = (purchaseAmount *
0.055);
System.out.println("\nYour stamp
duty payable " + (currencyFormat.format(purchaseAmount) + " is " +
(currencyFormat.format(dutyPayable) + "\n")));
}
// Reset the loop control variable to 0
// and continue looping the menu
purchaseAmount = 0;
selectedOption = 0;
continue;
// Start Vowel Counter
case VOWEL_COUNTER_OPTION:
// Initialize the char array with each vowel character
char[] vowelCollection = new char[5];
vowelCollection[0] = 'a';
vowelCollection[1] = 'e';
vowelCollection[2] = 'i';
vowelCollection[3] = 'o';
vowelCollection[4] = 'u';
// Prompt the user to enter a string
// and capture the entry in a String
System.out.print("\nEnter a line of
text: ");
////////////////////////////////////////////String
enteredString = myReader.readLine();
if (enteredString == "") {
enteredString =
myReader.readLine();
}
// Initialize vowel counter
int sumOfVowels = 0;
// Start outer loop
// Outer loop iterates through each
character in the entered string
// Each character is captured
for (int j = 0 ; j <=
enteredString.length()1 ; j++) {
char charInPosition =
enteredString.charAt(j);
// Start inner loop
// Inner loop iterates through
each charcter in the array
// checking if there is a match
between the current character
// If a match is made, the vowel
counter is incremented
for (int v = 0 ; v <=
vowelCollection.length 1 ; v++) {
if (charInPosition ==
vowelCollection[v]) {
sumOfVowels += 1;
}
}
}
// Display the number of vowels
found to the user
System.out.println("\nThe are " +
sumOfVowels + " vowels in the string \"" + enteredString+ "\"\n");
// Reset the loop control variable to 0
// and continue looping the menu
enteredString = "";
selectedOption = 0;
continue;
// Start exit
case EXIT_OPTION:
// Display a goodbye message to the user
// and stop the loop
System.out.println("\nExiting the program
goodbye...\n"); //??????? How to exit program??????
break;
// Start default
default:
// Prompt the user to select a valid option
System.out.println("\nError! \"" +
selectedOption + "\" is not a valid menu option!\n");
}
}