• Status: Solved
• Priority: Medium
• Security: Public
• Views: 13433

# SALES TAX Java problem solution??

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
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
Jnaneshwar
1 Solution

Chief Technology OfficerCommented:
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

Commented:
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

// print reciept

}

public void printReciept(Vector<PurchaseItem> basket) throws Exception
{
float totPrice = 0.0f;
float totTax = 0.0f;

{

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;

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();

no++;

System.out.print("More Item[y/n]: ");
userInput = keyboard.nextLine();
if(userInput.toLowerCase().equals("n"))
break;
}
}
}

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;
}
}
``````
0

Chief Technology OfficerCommented:
@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

Commented:
Hi mwvisa1,
You are definitely more qualified than me. Given the certification that you earned over the year.
Cheer.
0

Chief Technology OfficerCommented:
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

Chief Technology OfficerCommented:
Guess your e-mail is not working, so drop me a line when you get a chance (see my profile).
0

Author Commented:
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

Chief Technology OfficerCommented:
Not a big deal.  That is why you got the answer you did from sciuriware.  Anyway, glad you got solution to your issue. :)
0

Commented:
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

Commented:
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

Tackle projects and never again get stuck behind a technical roadblock.