3 Replies - 4067 Views - Last Post: 09 April 2011 - 06:24 PM Rate Topic: -----

#1 pravglad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-March 11

Java SE murrach's Use objects in the invoice application

Posted 08 April 2011 - 09:46 PM

Exercise 6-3 Use objects in the Invoice Application
In this exercise, you'll create an Invoice class and construct objects from it as you conver the Invoice application to an object-oriented application.

1. Open the InvoiceApp and Validator classes in the ch06\Invoice directory. This is yet another version of the Invoice application. Then, compile and run the classes to see how this application works.

2. Start a new class named Invoice and save in the same directory. Then, write the code for this class so it provides all of the data and all of the processing for an Invoice object. Its constructor should require the subtotal and customer type as its only parameters, and it should initialize instance variables for discount percent, discount amount, and invoice total. Its methods should include the required get and set methods, plus a method named getInvoice that returns a string that contains all of the data for an invoice in a printable format. When you're done, compile the Invoice class.

3. Modify the code in the InvoiceApp classes so it creates an Invoice object and uses its getInvoice method to get the formatted data fro invoice. That should simplify this class considerably. Then compile and test this class to make sure that this application works the way it did in step 1.



Invoice App Class
import java.text.NumberFormat;
import java.util.Scanner;

public class InvoiceApp
{
    public static void main(String[] args)
    {
        // display a welcome message
        System.out.println("Welcome to the Invoice Total Calculator");
        System.out.println();  // print a blank line

        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while(choice.equalsIgnoreCase("y"))
        {
            // get user entries
            String customerType = Validator.getString(sc,
                "Enter customer type (r/c):   ");
			double subtotal = Validator.getDouble(sc,
                "Enter subtotal:              ", 0, 10000);

            // calculate the discount amount and total
            double discountPercent = getDiscountPercent(subtotal, customerType);
            double discountAmount = subtotal * discountPercent;
            double total = subtotal - discountAmount;

            // format the values
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            NumberFormat percent = NumberFormat.getPercentInstance();
            String sSubtotal = currency.format(subtotal);
            String sCustomerType = "";
            if (customerType.equalsIgnoreCase("r"))
            	sCustomerType = "Retail";
            else if (customerType.equalsIgnoreCase("c"))
            	sCustomerType = "College";
          	String sDiscountPercent = percent.format(discountPercent);
            String sDiscountAmount = currency.format(discountAmount);
            String sTotal = currency.format(total);

            // format and display the results
            String message = "Subtotal:         " + sSubtotal + "\n"
                           + "Customer type:    " + sCustomerType + "\n"
                           + "Discount percent: " + sDiscountPercent + "\n"
                           + "Discount amount:  " + sDiscountAmount + "\n"
                           + "Total:            " + sTotal + "\n";
            System.out.println();
            System.out.println(message);

            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }
    }

    static double getDiscountPercent(double subtotal, String type)
    {
        double discountPercent = 0.0;
        if (type.equalsIgnoreCase("r"))
        {
            if (subtotal >= 500)
                discountPercent = .2;
            else if (subtotal >= 250 && subtotal < 500)
                discountPercent =.15;
            else if (subtotal >= 100 && subtotal < 500)
                discountPercent =.1;
            else if (subtotal < 100)
                discountPercent =.0;
        }
        else if (type.equalsIgnoreCase("c"))
        {
                discountPercent = .2;
        }
        else
            discountPercent = .05;

        return discountPercent;
     }
}



Validator class
import java.util.Scanner;

public class Validator
{
    public static String getString(Scanner sc, String prompt)
    {
        System.out.print(prompt);
        String s = sc.next();  // read user entry
        sc.nextLine();  // discard any other data entered on the line
        return s;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = 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 int getInt(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = 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 getDouble(Scanner sc, String prompt)
    {
        double d = 0;
        boolean isValid = 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 getDouble(Scanner sc, String prompt,
    double min, double max)
    {
        double d = 0;
        boolean isValid = 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;
    }
}


I got this much so far for invoice class.. but i am lost..wat should i do next..
import java.text.NumberFormat;

public class Invoice
{

	private String customerType;
	private double subtotal;
	private double discountPercent;
	private double discountAmount;
	private double total;
	
	public Invoice()
	{
		customerType = "";
		subtotal = 0;
		discountPercent = 0;
		discountAmount = 0;
		total = 0;
	}

	public Invoice( String customerType, double subtotal)
	{
		
		this.customerType = customerType;
		this.subtotal = subtotal;
	}

	
	public void setcustomerType(String customerType)
	{
		this.customerType = customerType;
	}

	public String getcustomerType()
	{
		return customerType;
	}

	public void setsubtotal(double subtotal)
	{
		this.subtotal = subtotal;
	}

	public double getsubtotal()
	{
		return subtotal;
	}
	
	
	

	private void setDiscountPercent(double DiscountPercent)
	{ 
	   this.discountPercent = DiscountPercent;
		
	}
	
	public double DiscountPercent()
	{
	    return discountPercent;		
	}
		
	
	private void setDiscountAmount(double DiscountAmount)
	{ 
	   this.discountAmount = DiscountAmount;
		
	}
	
	public double DiscountAmount()
	{
	    return discountAmount;		
	}	
	
	
		private void setTotal(double Total)
	{ 
	   this.total = total;
		
	}  
		public double Total()
	{
		return total;
	}
		
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java SE murrach's Use objects in the invoice application

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Java SE murrach's Use objects in the invoice application

Posted 08 April 2011 - 09:53 PM

Basically, rather than using the existing variables in the main() method to store information about the Invoice, you should create an Invoice object instead and use that.
Was This Post Helpful? 1
  • +
  • -

#3 pravglad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-March 11

Re: Java SE murrach's Use objects in the invoice application

Posted 09 April 2011 - 10:41 AM

so wat am i missing still in my invoice.. help would be greatly appreciated..thank you
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Java SE murrach's Use objects in the invoice application

Posted 09 April 2011 - 06:24 PM

Quote

plus a method named getInvoice that returns a string that contains all of the data for an invoice in a printable format.

You haven't even attempted this part from your instructions.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1