Write Java methods for a Purchase class. This class must have methods to set and
ID: 3701010 • Letter: W
Question
Write Java methods for a Purchase class. This class must have methods to set and get the bar code (a string of 1s and 0s) set and get the product name set and get the unit price set and get the number of units compute the value of the purchase get the value toString method that returns all of the purchase information as a string Remember you must guard against wrong values for price, number of units, and bar code. The bar code must be checked to insure that it is 16 bits and contains only ones and zeroes. Compile the Purchase class. Note: You will not be able to execute the Purchase class because it does not have a main method. Create a testing program to test the all methods of the Purchase class. Save the testing program in the same directory as the Purchase class. Compile, execute, and test the testing program to be certain the Purchase class is correct. Turn in Purchase class testing program
Explanation / Answer
ScreenShot
------------------------------------------------------------------------------------------------------
Program
/* Packages */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Purchase class */
class purchase{
/* Variable declaration */
String productname,barCode;
int noUnit;
double unitPrice,tCost;
/* Default Constructor */
purchase(){
this.productname="";
this.barCode="";
this.noUnit=0;
this.unitPrice=0.0;
}
/* Mutators */
public void setProductname(String pName){
this.productname=pName;
}
public void setBarcode(String bCode){
this.barCode=bCode;
}
public void setNoUnit(int nu){
this.noUnit=nu;
}
public void setUnitPrice(double up){
this.unitPrice=up;
}
/* Acessors */
public String getProductname(){
return productname;
}
public String getBarcode(){
return barCode;
}
public int setNoUnit(){
return noUnit;
}
public double setUnitPrice(){
return unitPrice;
}
/* Total cost calculating function */
public double totalCost(){
tCost=noUnit*unitPrice;
return tCost;
}
/* toString method to print values */
public String toString() {
totalCost();
return "Product name=" + productname + " Product cost=" +unitPrice+" Number of product="+noUnit+" Total cost="+tCost;
}
}
/* Main class */
class Ideone
{
// Main method */
public static void main (String[] args) throws java.lang.Exception
{
/* Read data using scanner */
Scanner sc=new Scanner(System.in);
/* variable declaration for data read */
String productname,barCode;
int noUnit;
double unitPrice;
/* User prompt for user input */
System.out.println("Enter Product name:");
productname=sc.nextLine();
System.out.println("Enter Barcode:");
barCode=sc.nextLine();
/* Check the bar code conditions */
while(barCode.length()==16){
System.out.println("Enter Barcode:");
barCode=sc.nextLine();
}
for (int index = 0; index < barCode.length();index++) {
char aChar = barCode.charAt(index);
if(aChar!='0' && aChar!='1'){
System.out.println("Enter Barcode:");
barCode=sc.nextLine();
}
}
System.out.println("Enter product Unit Price:");
unitPrice=sc.nextDouble();
System.out.println("Enter Number Of Unit's Sold:");
noUnit=sc.nextInt();
/* set user entered data */
purchase p=new purchase();
p.setProductname(productname);
p.setBarcode(barCode);
p.setUnitPrice(unitPrice);
p.setNoUnit(noUnit);
/* call toString method */
System.out.println(p);
}
}
------------------------------------------------------------------------------
Output
----------------------------------------------------------------------------------------------------
Note:-
I assume you want a purchase class wit set, get and toString() methods.
If any clarification,let me know
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.