Solved

SALES TAX Java problem solution??

Posted on 2008-12-15
10
12,903 Views
2 Endorsements
Last Modified: 2015-03-10
Can someone please let me know the solution to this problem ?

Problem statement :

Basic sales tax is applicable at a rate of 10% on all goods, except books,
food, and medical products that are exempt. Import duty is an additional
sales tax applicable on all imported goods at a rate of 5%, with no
exemptions.

When I purchase items I receive a receipt which lists the name of all the
items and their price (including tax), finishing with the total cost of the
items, and the total amounts of sales taxes paid.  The rounding rules for
sales tax are that for a tax rate of n%, a shelf price of p contains
(np/100 rounded up to the nearest 0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping
baskets...
INPUT:

Input 1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85

Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50

Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99
1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25

OUTPUT

Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83

Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15

Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68
2
Comment
Question by:Jnaneshwar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 23173780
Please post the code you have developed so far and specifics on compilation/runtime errors you are getting and/or questions as to why something is not working as expected and will gladly take a look.
0
 
LVL 9

Accepted Solution

by:
bluebelldiscovery earned 250 total points
ID: 23184561
Hi Jnaneshwar,

 This code will do the job :)
import java.util.*;
 
public class TaxMan
{
	public TaxMan()
	{
	}
 
	public static void main(String [] args) throws Exception
	{
		TaxMan taxMan = new TaxMan();
 
		// get purchase
		Vector<PurchaseItem> basket = taxMan.getPurchase();
		//System.out.println(basket);
 
		// print reciept
		taxMan.printReciept(basket);
 
	}
	
	public void printReciept(Vector<PurchaseItem> basket) throws Exception
	{
		float totPrice = 0.0f;
		float totTax = 0.0f;
 
		for(int i=0; i<basket.size(); i++)
		{
			PurchaseItem purchaseItem = basket.get(i);
 
			totPrice += purchaseItem.price;
			totTax += purchaseItem.tax;
			System.out.println(purchaseItem);
		}
 
		// round tax to near $0.05
		float reminder = totTax % 0.05f;
		if( reminder != 0.0f )
		{
			if( reminder > 0.025f ) // round up
				totTax -= reminder;
			else // round up
				totTax += (0.05f - reminder);
		}
 
		System.out.println("Sales Taxes: $"+totTax);
		System.out.println("Total: $"+(totPrice+totTax));
	}
 
	public Vector<PurchaseItem> getPurchase() throws Exception
	{
		// for getting keyboard input
		Scanner keyboard = new Scanner(System.in);
		String userInput = null;
 
		Vector<PurchaseItem> basket = new Vector<PurchaseItem>();
 
		int no = 1;
		while(true)
		{
			PurchaseItem purchaseItem = new PurchaseItem();
 
			System.out.print("Item"+no+" Qty: ");
			userInput = keyboard.nextLine();
			purchaseItem.qty = Integer.parseInt(userInput);
 
			System.out.print("Item"+no+" Name: ");
			userInput = keyboard.nextLine();
			purchaseItem.name = userInput;
 
			System.out.print("Item"+no+" Price: $");
			userInput = keyboard.nextLine();
			purchaseItem.price = Float.parseFloat(userInput);
 
			System.out.print("Item"+no+" Imported[y/n]: ");
			userInput = keyboard.nextLine();
			if(userInput.toLowerCase().equals("y"))
				purchaseItem.imported = true;
 
			System.out.print("Item"+no+" Exempted[y/n]: ");
			userInput = keyboard.nextLine();
			if(userInput.toLowerCase().equals("y"))
				purchaseItem.exempted = true;
 
			// compute tax
			purchaseItem.computeTax();
			
			// add to basket
			basket.add(purchaseItem);
			no++;
			
			System.out.print("More Item[y/n]: ");
			userInput = keyboard.nextLine();
			if(userInput.toLowerCase().equals("n"))
				break;
		}
		return basket;
	}
}
 
class PurchaseItem
{
	int qty = 0;
	String name = null;
	float price = 0.0f;
	boolean imported = false; // 10%
	boolean exempted = false; // 5%
 
	float tax = 0.0f;
	
	public String toString()
	{
		return qty+" "+name+" at $"+price+" (imported:"+imported+" exempted:"+exempted+")";
	}
	
	void computeTax()
	{
		float totTaxPercent = 0.0f;
 
		// check tax
		if(imported)
			totTaxPercent = 0.1f;
		if(!exempted)
			totTaxPercent += 0.05f;
 
		// compute tax
		tax = totTaxPercent * price;
	}
}

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 23184779
@bluebelldiscovery, this is a homework assignment and it is the policy of EE not to just post the final solution in those cases.  We can help the asker learn the topic and guide him/her to final answer yes, but simply posting code is discouraged.  BTW, I could have done that yesterday. :) Hence my request to Jnaneshwar.  Anyway, not too big a deal, but just thought you should be aware of that in case you were not already.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 9

Expert Comment

by:bluebelldiscovery
ID: 23185050
Hi mwvisa1,
  You are definitely more qualified than me. Given the certification that you earned over the year.
Cheer.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 23185231
Hope you don't mind, bluebelldiscovery, but I just sent you an e-mail.  Figured we don't need to fill question with my off-topic conversation. ;)

Cheers.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 23185325
Guess your e-mail is not working, so drop me a line when you get a chance (see my profile).
0
 

Author Comment

by:Jnaneshwar
ID: 23190751
Thanks so much for the soluton bluebelldiscovery, appreciate the quick solution .. yes I have noted the pointers provided by mwvisa1 as well regarding the workings of EE homework assignments, thanks for the same sir.

Regards
Jnaneshwar
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 23190761
Not a big deal.  That is why you got the answer you did from sciuriware.  Anyway, glad you got solution to your issue. :)
0
 

Expert Comment

by:sunsim24
ID: 24013316
Actually the above code was not giving the right output ....i am attaching a code which is giving the expected output....Kindly check if its fine....i have uploaded the files in .txt format kindly convert it into .java and then u can run it ....check with the package structure also
PriceCalculator.txt
PurchasedItemsDO.txt
TaxCalucator.txt
0
 

Expert Comment

by:Pinkyy
ID: 40657921
sunsim24 Did you limit the quantity of item to 1? I am not able to run it with 2 as qty. Please help
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question