Question

Write the following program in Java using Eclipse.

A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class:

numberOfItems: this variable will keep track of the number of items added to this receipt. This is int value.

items: this is an array of Item class. The size of the array will be set in the constructor.

numberOfSales: this variable should keep track of all the Receipts.

Constructor: this constructor should accept an int value. This value will be used to set the size of the items array.

add(Item): this method accepts an item object and adds it to the items array.

add(name, price, qty, id): this is an overloaded add method. It works the same way as the add method above.

printReceipt(): this method displays a receipt with all the items. Including each item’s name, price, qty and subtotal. In addition, it displays the sales tax collected as well as the total after tax.

You don’t have to create the Item class. Use the attached one. However, you have to add some code to enforce the input validation in the following methods:

setName

setPrice

setQty

Input Validation:

Don't accept any id less than 1000.

Don't accept any negative input for price or qty

Don't accept an empty or null string for the name.

When your program gets an invalid input, it should print an error message and exit.

Here is the Sample Output:

ID Name Price Qty SubTotal 2222 Milk $3.99 4 $15.96 1222 Eggs $2.99 $8.97 1000 0J $5.99 1 $5.99 1223 Butter $1.49 10 $14.9 $3.49 7.625% Sales Tax: $80.76 Total After Tax

Here is the content of the main method, that was used to get the output shown above:

       public static void main(String[] args) {

              int maxNumberOfItems = 4;

//            create an instance of CashRegister class

              Receipt receipt = new Receipt(maxNumberOfItems);

//            create an instance of Item class

              Item item1 = new Item("Milk", 3.99, 4, 2222);

//            create an instance of Item class

              Item item2 = new Item("Eggs", 2.99, 3, 1222);

//            add an item to the cash register

              receipt.add(item1);

//            add an item to the cash register

              receipt.add(item2);

//            add an item to the cash register using the overloaded add method

              receipt.add("OJ", 5.99, 1, 1000);

//            add an item to the cash register using the overloaded add method

              receipt.add("Butter", 1.49, 10, 1223);

             

//            display a receipt with all the items

              receipt.printReceipt();   

       }

First create a project and call it pa3

Add a package; call the package edu.century.pa3

Add the two Receipt and Item classes to edu.century.pa3 package

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Item.java

package pa3;

public class Item {

   private int id;
   private String name;
   double price;
   int qty;
  
  
   public Item(){
      
   }
  
   public Item(String name,double price,int qty,int id){
       this.name=name;
       this.id=id;
       this.price=price;
       this.qty=qty;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
   public int getQty() {
       return qty;
   }
   public void setQty(int qty) {
       this.qty = qty;
   }
  
}

Receipt.java

package pa3;

public class Receipt {

   private Item[] items;
   private static int count=0;
   public Receipt(int size){
       items=new Item[size];
   }
  
   public void add(Item item){
      
       //check all the constraint before adding item class object
       if(item.getName()!="" && item.getName()!=null){
           if(item.getId()>=1000){
               if(item.getPrice()>0 && item.getQty()>0){
                   items[count]=item;
                   count++;
               }
           }
       }
      
      
   }
  
   public void add(String name,double price,int qty,int id){
      
       //check all the constraint before adding item class object
       if(name!="" && name!=null){
           if(id>=1000){
               if(price>0 && qty>0){
                   Item I=new Item(name,price,qty,id);
                   items[count]=I;
                   count++;
               }
           }
       }
   }
  
   public void printReceipt(){
       System.out.println("ID\t\tName\t\tPrice\t\tQty\t\tSubTotal");
       System.out.println("-----------------------------------------------------------------------------------");
       double sum=0;
       for(int i=0;i<items.length;i++){
          
           double total=items[i].getPrice()*items[i].getQty();
           sum+=total;
           System.out.println(items[i].getId()+"\t\t"+items[i].getName()+"\t\t$"+items[i].getPrice()+"\t\t"+
           items[i].getQty()+"\t\t$"+total);
       }
      
       System.out.println("-----------------------------------------------------------------------------------");
       System.out.println("-----------------------------------------------------------------------------------");
       double temp=((sum*7.625)/100);
       System.out.println("7.625% Service tax:\t\t\t\t\t\t$"+temp);
      
       sum+=temp;
       System.out.println("Total after tax:\t\t\t\t\t\t$"+sum);
   }
  
   public static void main(String[] args) {
int maxNumberOfItems = 4;
// create an instance of CashRegister class
Receipt receipt = new Receipt(maxNumberOfItems);
// create an instance of Item class
Item item1 = new Item("Milk", 3.99, 4, 2222);
// create an instance of Item class
Item item2 = new Item("Eggs", 2.99, 3, 1222);
// add an item to the cash register
receipt.add(item1);
// add an item to the cash register
receipt.add(item2);
// add an item to the cash register using the overloaded add method
receipt.add("OJ", 5.99, 1, 1000);
// add an item to the cash register using the overloaded add method
receipt.add("Butter", 1.49, 10, 1223);

// display a receipt with all the items
receipt.printReceipt();   
}
}

Output:

Eile Edit ource Refactor Navigate Search Projec Bun Window Help Quick AccessJava EE Java MarkersProperties ServersDato Source

Add a comment
Know the answer?
Add Answer to:
Write the following program in Java using Eclipse. A cash register is used in retail stores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code in Java programming language. The ShoppingCart class will be composed with an array of...

    Write code in Java programming language. The ShoppingCart class will be composed with an array of Item objects. You do not need to implement the Item class for this question, only use it. Item has a getPrice() method that returns a float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart class should have: Constructors: A no-argument and a single argument that takes an array of Items. Fields: • Items: an array of Item objects...

  • Please implement the following only using Javascript. design a cash register class that can be used...

    Please implement the following only using Javascript. design a cash register class that can be used with inventory item class. The cash register class needs to: 1. ask user for item and quantity being purchased. 2. get the items cost from inventory item object 3. add 30% profit to the cost to get the items unit price 4. multiply the unit price times the quantity being purchased to get purchase subtotal 5. compute a 6% sales tax on the subtotal...

  • Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...

    Java 1. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT