Question

Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing...

Programming Assignment 6: Object Oriented Programming
Due date: Check Syllabus and Canvas
Objectives: After successfully completing this assignment, students will practice Object Oriented design and
programming by creating a Java program that will implement Object Oriented basic concepts.
RetailItem Class:
Part 1: Write a class named RetailItem that holds data about an item in a retail store. The class should have the
following fields:
• description. The description field references a String object that holds a brief description of the item.
• unitsOnHand. The unitsOnHand field is an int variable that holds the numbers of units currently in inventory.
• price. The price filed is a double that holds the item's retail price.
Write a constructor that accepts arguments for each field, appropriate mutator methods that store values in these
fields, and accessor methods that returns the values in these fields.
After this, write a method in the RetailItem class called purchaseItem. This method will be used to simulate an item(s)
purchase. It will accept as a parameter the variable totalItemsToPurchase representing a number of units to buy (e.g.
10). The method should check if its unitsOnHand is enough to satisfy the purchase. If it is not, notify the user. If it is
enough, update unitsOnHand with the new value (unitsOnHand – totalItemsToPurchase) and return to the user the total
cost of the purchase (itemsToPurchase * price).
Hint: remember you can use the this instruction to access member variables inside a class
Once you have written the class, write a driver class that creates three RetailItem objects and stores the following data
in them:
Description (object’s name) Units on Hand Price
Jacket 12 59.95
Shoes 40 34.95
Pants 20 24.95
Use the accessor methods you created to display the initial value of all three items. Then, simulate purchases by calling
the purchaseItem method for each object following the list below:
Description (object’s name) totalItemsToPurchase
Jacket 10
Shoes 20
Pants 5
Jacket 5
Shoes 10
Pants 3
Run/test the program and save the output as a comment at the end of your cod
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

RetailItem.java

/**
* Create a class to keep retail store items details
* @author deept
*
*/
public class RetailItem {
   //Attributes
   private String description;
   private int unitsOnHand;
   private double price;
   //Constructor set values of attributes
   public RetailItem(String desc,int units,double pr) {
       setDescription(desc);
       setUnitsOnHand(units);
       setPrice(pr);
   }
   //Mutators
   public void setDescription(String description) {
       this.description = description;
   }
   public void setUnitsOnHand(int unitsOnHand) {
       if(unitsOnHand<0) {
           this.unitsOnHand = 0;
       }
       else {
           this.unitsOnHand = unitsOnHand;
       }
   }
   public void setPrice(double price) {
       if(price<0) {
           this.price=0;
       }
       else {
           this.price=price;
       }
   }
   //Accessors
   public String getDescription() {
       return description;
   }
   public int getUnitsOnHand() {
       return unitsOnHand;
   }
   public double getPrice() {
       return price;
   }
   //Method to purchase item from store
   public void purchaseItem(int totalItemsToPurchase) {
       if( totalItemsToPurchase<=unitsOnHand) {
           unitsOnHand-= totalItemsToPurchase;
           System.out.printf("%-20s%.2f\n",description,totalItemsToPurchase*price);
       }
       else {
           System.out.println("Item not enough to purchase!!!");
       }
   }
}

RetailItemDriver.java

/**
* Test class to check RetailItem class
* @author deept
*
*/
public class RetailItemDriver {

   public static void main(String[] args) {
       // Create 3 RetailItem objects
       RetailItem item1=new RetailItem("Jacket",12,59.95);
       RetailItem item2=new RetailItem("Shoes",40,34.95);
       RetailItem item3=new RetailItem("Pants",20,24.95);
       //Display initial details of items
       System.out.println("Description   Units on Hand   Price");
       System.out.printf("%-20s%-10d%.2f\n",item1.getDescription(),item1.getUnitsOnHand(),item1.getPrice());
       System.out.printf("%-20s%-10d%.2f\n",item2.getDescription(),item2.getUnitsOnHand(),item2.getPrice());
       System.out.printf("%-20s%-10d%.2f\n",item3.getDescription(),item3.getUnitsOnHand(),item3.getPrice());
       //Purchase the following items
       System.out.println();
       System.out.printf("%-20s%s\n","Description","Purchase Cost");
       //Jacket 10
       item1.purchaseItem(10);
       //Shoes 20
       item2.purchaseItem(20);
       //Pants 5
       item3.purchaseItem(5);
       //Jacket 5
       item1.purchaseItem(5);
       //Shoes 10
       item2.purchaseItem(10);
       //Pants 3
       item3.purchaseItem(3);
   }
}

Output

Description   Units on Hand   Price
Jacket              12        59.95
Shoes               40        34.95
Pants               20        24.95

Description         Purchase Cost
Jacket              599.50
Shoes               699.00
Pants               124.75
Item not enough to purchase!!!
Shoes               349.50
Pants               74.85

Add a comment
Know the answer?
Add Answer to:
Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing...
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 a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,...

    Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class, Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields description: The description field is a String object that holds a brief description of the item . unitsOnHand: The unitsOnHand field is an int variable that holds the number of units currently in inventory Price: The price field is a double that...

  • USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds...

    USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...

  • 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....

  • *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented...

    *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows:  For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method.  For each...

  • in python Write a class named RetaiI_Item that holds data about an item in a retail...

    in python Write a class named RetaiI_Item that holds data about an item in a retail store. The class should store the following data in attributes: • Item Number • Item Description • Units in Inventory • Price Create another class named Cash_Register that can be used with the Retail_Item class. The Cash_Register class should be able to internally keep a list of Retail_Item objects. The class should include the following methods: • A method named purchase_item that accepts a...

  • Week 6: Lab Overview TABLE OF CONTENTS Lab Overview Scenario/Summary Write a windows console application that...

    Week 6: Lab Overview TABLE OF CONTENTS Lab Overview Scenario/Summary Write a windows console application that holds data about an item in a retail store. Your class should be named RetailItem and should hold data about an item in a retail store. The class will have the following member variables. Description - string holding the description of the item, unitsOnHand - int that holds the number of units in inventory Price - double that holds the price of the item...

  • Programming Assignment 5: UML Diagram Objectives: After successfully completing this assignment, students will practice Object Oriented...

    Programming Assignment 5: UML Diagram Objectives: After successfully completing this assignment, students will practice Object Oriented design by creating an UML diagram for a Java program. Program Statement: Bank System You were asked to create a simple UML diagram for a bank system. Each bank has a specific identification number, a name, and a location that needs to be stored. Tellers serve customers’ loans, checking and savings accounts. The bank must know each tellers’ name and identification number for record...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • Assignment 6 Due: Apr 12, 2019 at 11:59 PM A6 OOP 3 "PreferredCustomer Class" (need to...

    Assignment 6 Due: Apr 12, 2019 at 11:59 PM A6 OOP 3 "PreferredCustomer Class" (need to create Person, Customer classes described in the previous problem) Access A6 from pdf assignment file. Turn in the following files: a6main.java Customer.java Person.java PreferredCustomer.java program compile and run screenshots design document (including UML) A6 7. Person and Customer Classes esign a class named Person with fields for holding a person's name, address, and telephone number. Write one or more constructors and the appropriate mutator...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

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