SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 313 Weeks Ago, 6 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 14 Nov 2017 My Price 10.00

Invoice application that validates the data the user enters.

I would like help editing this code for this assignment. I can't even seem to get started on it. An example of what I need would be nice. Attached is the code and also the instructions. Its for netbeans.

Murachs Java Programming Page 165

 

In this exercise, you’ll add code to the Invoice application that validates the data the user enters. That includes exception handling code as well as specific data validation methods.

1. Open the project named ch05_exl_Invoice in the ex_starts directory. Then, test the application to see how it works.

2. As you test the application, enter an invalid customer type code to see what happens. Then, enter an invalid subtotal entry like $1000 to see what happens when the application crashes.

Validate the customer type code

3. Modify the application so it will only accept customer type codes r and c. It should also discard any extra entries on the customer type line. If the user enters an invalid code, the application should display an error message andask the user to enter a valid code. This should be done before the user enters a subtotal. Then, test this enhancement.

4. Code a static method named getYalidCustomerType that does the validation of step 3. This method should include one parameter that receives a Scanner object, and it should return a valid customer type code. The method should get an entry from the user, check it for validity, display an error message if it’s invalid, and discard any other user entries whether or not the entry is valid. This method should continue getting user entries until one is valid. The easiest way to add the code for this method is to copy the code you wrote in step 3.

5. Modify the application so it uses this method. Then, test this enhancement. Validate the subtotal

6. Add a try statement that catches any InputMismatchException that the nextDouble method of the Scanner class might throw. The catch block should display an error message and issue a continue statement to jump to the beginning of the while loop. It should also discard the invalid entry and any other entries on the line. For this to work, you’ll need to import the InputMismatchException class, and you’ll need to declare the subtotal variable before the try statement so it can be used outside that statement. Test this enhancement.

7. Code a static method named getValidSubtotal that uses the hasDouble method of the Scanner class to validate the subtotal entry so the InputMismatchException won’t occur. This method should require one parameter that receives a Scanner object, and it should return a valid subtotal. This method should get an entry from the user, check that it’s a valid double value, check that it’s greater than zero and less than 10000, display appropriate error messages if it isn’t valid, and discard any other user entries whether or not the entry is valid. This should continue until the method gets avalid subtotal entry.

8. Modify the code within the try statement so it uses this method. Then, test this enhancement so you can see that an InputMismatchException is no longer caught by the catch block.

Discard any extra entries for the Continue prompt

9. Run the application again. When the Continue prompt is displayed, enter two or more values to see what happens.

10. Modify the code so the application works right even if the user enters two or more values when asked if he wants to continue. To do that, you need to discard any extra entries. Then, test this enhancement.

At this point, the application should be bulletproof. It should only accept valid

entries for customer type and subtotal, and it should work even if the user

makes two or more entries for a single prompt.

 

---------------------------------------------------------------------------------------------------------------------

 

import java.util.*;

import java.text.*;

 

public class FutureValueApp

{

    public static void main(String[] args)

    {

System.out.println("Welcome to the Future Value Calculator\n");

 

        Scanner sc = new Scanner(System.in);

        String choice = "y";

 

        while (choice.equalsIgnoreCase("y"))

        {

            // get the input from the user

System.out.println("DATA ENTRY");

            double monthlyInvestment = getDoubleWithinRange(sc,

                "Enter monthly investment: ", 0, 1000);

            double interestRate = getDoubleWithinRange(sc,

                "Enter yearly interest rate: ", 0, 30);

int years = getIntWithinRange(sc,

                "Enter number of years: ", 0, 100);

 

            // calculate the future value

            double monthlyInterestRate = interestRate/12/100;

int months = years * 12;

            double futureValue = calculateFutureValue(

monthlyInvestment, monthlyInterestRate, months);

 

            // get the currency and percent formatters

NumberFormat currency = NumberFormat.getCurrencyInstance();

NumberFormat percent = NumberFormat.getPercentInstance();

percent.setMinimumFractionDigits(1);

 

            // format the result as a single string

            String results =

                  "Monthly investment:\t"

                      + currency.format(monthlyInvestment) + "\n"

                + "Yearly interest rate:\t"

                      + percent.format(interestRate/100) + "\n"

                + "Number of years:\t"

                      +  years + "\n"

                + "Future value:\t\t"

                      + currency.format(futureValue) + "\n";

 

            // print the results

System.out.println();

System.out.println("FORMATTED RESULTS");

System.out.println(results);

 

            // see if the user wants to continue

System.out.print("Continue? (y/n): ");

            choice = sc.next();

sc.nextLine();  // discard any other data entered on the line

System.out.println();

        }

    }

 

    public static double getDouble(Scanner sc, String prompt)

    {

        double d = 0.0;

booleanisValid = false;

        while (isValid == false)

        {

System.out.print(prompt);

            if (sc.hasNextDouble())

            {

                d = sc.nextDouble();

isValid = true;

            }

            else

            {

System.out.println("Error! Invalid decimal value. Try again.");

            }

sc.nextLine();  // discard any other data entered on the line

        }

        return d;

    }

 

    public static double getDoubleWithinRange(Scanner sc, String prompt,

    double min, double max)

    {

        double d = 0.0;

booleanisValid = false;

        while (isValid == false)

        {

            d = getDouble(sc, prompt);

            if (d <= min)

System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (d >= max)

System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

isValid = true;

        }

        return d;

    }

 

    public static intgetInt(Scanner sc, String prompt)

    {

inti = 0;

booleanisValid = false;

        while (isValid == false)

        {

System.out.print(prompt);

            if (sc.hasNextInt())

            {

i = sc.nextInt();

isValid = true;

            }

            else

            {

System.out.println("Error! Invalid integer value. Try again.");

            }

sc.nextLine();  // discard any other data entered on the line

        }

        return i;

    }

 

    public static intgetIntWithinRange(Scanner sc, String prompt,

int min, int max)

    {

inti = 0;

booleanisValid = false;

        while (isValid == false)

        {

i = getInt(sc, prompt);

            if (i<= min)

System.out.println(

                    "Error! Number must be greater than " + min + ".");

            else if (i>= max)

System.out.println(

                    "Error! Number must be less than " + max + ".");

            else

isValid = true;

        }

        return i;

    }

 

    public static double calculateFutureValue(double monthlyInvestment,

    double monthlyInterestRate, int months)

    {

        double futureValue = 0;

        for (inti = 1; i<= months; i++)

        {

futureValue =

                (futureValue + monthlyInvestment) *

                (1 + monthlyInterestRate);

        }

        return futureValue;

    }

}

Attachments:

Answers

(5)
Status NEW Posted 14 Nov 2017 07:11 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)