2. Using object oriented programming approach, write a program to calculate the
ID: 3699532 • Letter: 2
Question
2. Using object oriented programming approach, write a program to calculate the purchase price for a retail store. -Define a class Productl (Product1.java) o Four private data members: product number, product name, unit price, units sold o Two overloaded constructors o Get and set methods for each private data member o A method to calculate and return the purchase price (unit price * units sold) o toString0 method -(Store.java) Write a client program that creates a Product1 object based on the user input, computes the purchase price, and outputs the sales information (include the item id, item name, unit price, units sold, and purchase priceExplanation / Answer
Solution -->
Here is my java code:
1.
/************************* Product1.java starts here*************************/
public class Product1 {
private int productNumber;
private String productName;
private double unitPrice;
private int unitsSold;
public Product1() {
this(1, "Table", 1500.00, 1);
}
public Product1(int productNumber, String productName, double unitPrice, int unitsSold){
this.productNumber = productNumber;
this.productName = productName;
this.unitPrice = unitPrice;
this.unitsSold = unitsSold;
}
public int getProductNumber() {
return productNumber;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getUnitsSold() {
return unitsSold;
}
public void setUnitsSold(int unitsSold) {
this.unitsSold = unitsSold;
}
public double purchasePrice () {
return this.unitPrice * this.unitsSold;
}
@Override
public String toString() {
return "Product Number : " + this.productNumber
+ " Product Name : " + this.productName
+ " Unit Price : " + this.unitPrice
+ " Units Sold : " + this.unitsSold
+ " ----------------------------"
+ " Purchase Price : " + this.purchasePrice();
}
}
/************************* Product1.java ends here*************************/
2.
/************************* Store.java starts here*************************/
import java.util.Scanner;
public class Store {
public static void main(String[] args) {
System.out.println(" *** Please Enter Product Details *** ");
Scanner reader = new Scanner(System.in);
System.out.print(" Enter Product Number : ");
int productNumber = reader.nextInt();
Scanner reader2 = new Scanner(System.in);
System.out.print("Enter Product Name : ");
String productName= reader2.nextLine();
System.out.print("Enter Unit Price : ");
double unitPrice = reader.nextDouble();
System.out.print("Enter Units Sold : ");
int unitsSold = reader.nextInt();
Product1 product = new Product1(productNumber, productName, unitPrice, unitsSold);
System.out.println(" Purchase Price : "+ product.purchasePrice()+" ");
System.out.println(product.toString());
}
}
/************************* Store.java ends here*************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.