Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using object oriented programming approach, write a Cash Register program. Java

ID: 3808830 • Letter: U

Question

Using object oriented programming approach, write a Cash Register program. Java Programs

Define a class Product (Product.java)

Three private data members: product name, unit price, units to purchase

One static constant: sales tax rate = 0.0675

Two overloaded constructors

Ensure that both unit price and units to purchase are valid ( > = 0 )

Get and set methods for each private data member

Ensure that both unit price and units to purchase are valid ( > = 0 ) in setters

A method to calculate and return the purchase price before tax

price before tax = unit price * units to purchase

A method to calculate and return the purchase price after tax

price after tax = price before tax * ( 1 + sales tax rate )

toString() method

(CashRegister.java) Write a menu-driven client program that can simulate the sale at a retail store.

1 – Purchase

Create a Product object based on the user input and calculate its purchase prices before and after tax

2 – Check Out

Print the receipt (the receipt should include all the purchases)

Explanation / Answer

Product.java

class Product{
   private String name;
   private double unitPrice;
   private int noOfUnitsToPurchase;
  
   final static double sales_tax_rate = 0.0675;
  
   public Product(){
       this.name = "";
       this.unitPrice = 0.0;
       this.noOfUnitsToPurchase = 0;
   }
   public Product(String name,double unitPrice,int noOfUnitsToPurchase){
       this.name = name;
       //checking whether unitPrice and noOfUnitsToPurchase are >=0
       if(unitPrice>=0)
       this.unitPrice = unitPrice;
       else this.unitPrice = 0.0;
       if(noOfUnitsToPurchase>=0)
       this.noOfUnitsToPurchase = noOfUnitsToPurchase;
       else this.noOfUnitsToPurchase = 0;
   }
  
   //getters
   public String getName(){
       return this.name;
   }
   public double getUnitPrice(){
       return this.unitPrice;
   }
   public int getNoOfUnitsToPurchase(){
       return this.noOfUnitsToPurchase;
   }
  
   //setters
   public void setName(String name){
       this.name = name;
   }
   public void setUnitPrice(double uPrice){
       if(uPrice>=0)
       this.unitPrice = uPrice;
   }
   public void getNoOfUnitsToPurchase(int noOfUnitsToPurchase){
       if(noOfUnitsToPurchase>=0)
       this.noOfUnitsToPurchase = noOfUnitsToPurchase;
   }
  
   public double calculatePurchasePriceBeforeTax(){
       return this.noOfUnitsToPurchase*this.unitPrice;//before tax price
   }
   public double calculatePurchasePriceAfterTax(){
       return calculatePurchasePriceBeforeTax()*(1+this.sales_tax_rate);//after tax price
   }
  
   public String toString(){
       return this.name+" "+this.unitPrice+" "+this.noOfUnitsToPurchase;//overriding toString method in Object class
   }
  
}


CashRegister.java

import java.util.Scanner;
import java.util.ArrayList;
class CashRegister{
   public static void main(String a[]){
       Scanner input = new Scanner(System.in);//to take input from user
       ArrayList<String> listOfPurchases = new ArrayList<String>();//stores all purchase reciepts(before and after tax purchases)
       int choice;
       do{
           System.out.println("Enter -1 to exit");
           System.out.println("1-Purchase 2-Check Out");
           choice = input.nextInt();
           switch(choice){
               case 1:
               //taking inputs from user to create a product object
                   System.out.println("Enter name of product:");
                   String name = input.next();
                   System.out.println("Enter Unit Price:");
                   double unitPrice = input.nextDouble();
                   System.out.println("Enter number of units to purchase:");
                   int noOfUnitsToPurchase = input.nextInt();
                   //instantiating product object
                   Product prdt = new Product(name,unitPrice,noOfUnitsToPurchase);
                   //calculating purchases
                   String purchases = prdt.calculatePurchasePriceBeforeTax()+" "+prdt.calculatePurchasePriceAfterTax();
                   //adding this purchase reciept to our arraylist
                   listOfPurchases.add(purchases);
                   System.out.println(purchases);
                  
               break;
               case 2:
                   System.out.println("Check Out list:");
                   //prints all reciepts of purchases
                   for(String purchase:listOfPurchases){
                       System.out.println(purchase);
                   }
              
               break;
               case -1:
               // if input is -1 we will do nothing.after going out from Switch block, loop will terminate.
               break;
               default:
               System.out.println("Enter valid number!!!");//asking user to enter valid numbers that are 1 or 2 or -1;
              
           }
          
       }
       while(choice!=-1);//loop untill user enters -1;
   }
}


Thankyou!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote