create a class named purchase. Each purchase contains an invoice number, amount
ID: 663026 • Letter: C
Question
create a class named purchase. Each purchase contains an invoice number, amount of sale , and amount of sales tax. Include set methods for the invoice number and sales amount. Within the set() method for the sale amount, calculate the sales tax as 5 % of the sale amount. Also include a display method that displays a purchase's details. Save the file as Purchase.java
Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a nonnegative value. After a valid Purchase object has been created, display the object's invoice number, sale amount, and sales tax. Save the file as CreatePurchase.jave
Explanation / Answer
/////////////////////////////////////////////////////////////////////////////////////////////
Purchase.java
///////////////////////////////////////////////////////////////////////////////////////////
public class Purchase {
public static double set(double sale_amount)
{
double tax=0.05;
double sale_tax;
sale_tax=sale_amount*tax;
return sale_tax;
}
public static void display(double tax,double sale_amount,int invoice_no)
{
System.out.println("Invoice Number="+invoice_no);
System.out.println("Sale Amount="+sale_amount);
System.out.println("Sale Tax="+tax);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CreatePurchase.java
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
/**
*
* @author anurag
*/
public class CreatePurchase {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Purchase op=new Purchase();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Purchase Details: ");
System.out.println("invoice number(between 1000 and 8000):");
int Invoice_no=0;
double sale_amount=0;
int flag=0;
while(flag==0)
{
Invoice_no=sc.nextInt();
if(Invoice_no>=1000&&Invoice_no<=8000)
{
flag=1;
}
else
{
System.out.println("invoice number(between 1000 and 8000):");
}
}
System.out.println("Sale Amount= ");
flag=0;
while(flag==0)
{
sale_amount=sc.nextDouble();
if(sale_amount<0)
{
System.out.println("Sale Amount (Should be non negative) = ");
}
else
flag=1;
}
op.display(op.set(sale_amount),sale_amount,Invoice_no);
}
}
///////////////////////////////////////////////////////////////
Output
///////////////////////////////////////////////////////////////
Case:1.
Enter the Purchase Details:
invoice number(between 1000 and 8000):
1500
Sale Amount=
2000
Invoice Number=1500
Sale Amount=2000.0
Sale Tax=100.0
////////////////////////////////////////////////////////////////////
Case:2.
Enter the Purchase Details:
invoice number(between 1000 and 8000):
700
invoice number(between 1000 and 8000):
2000
Sale Amount=
-500
Sale Amount (Should be non negative) =
1000
Invoice Number=2000
Sale Amount=1000.0
Sale Tax=50.0
//////////////////////////////////////////////////////////
this program is implemented on NetBeans IDE 7.1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.