Question: Programming Assignment #4 - CheckoutDriver New Concepts introduced from previous assignment (CheckoutInput): JOptionPane for input. We will switch from using Scanner to JOptionPane Creating a
Programming Assignment #4 - CheckoutDriver
New Concepts introduced from previous assignment (CheckoutInput):
JOptionPane for input. We will switch from using Scanner to JOptionPane
Creating a User defined Java Class that is not a main() program, defining class attributes and class methods, and Constructor.
Creating a Driver program that has main() and creates and calls methods on the object.
Use of Double.parseDouble() and Integer.parseInt() static methods to convert string to numeric.
Use of import.
The output format will remain same as previous assignment. You can copy and paste from previous assignment. However, apply corrections you learned and from feedback I provided.
----------------------------------------------------------------------------------------------------------------------------
Project Overview:
This project will contain 2 classes. Create these 2 classes:
Cart
CheckoutDriver
Your program will simulate a purchase of an item in a shopping cart. Using a JOptionPane Dialog window, you will prompt the user to enter the:
Customers First name,
Customers Last name, the
number of items purchased,
and the cost for each item.
The program will calculate the state tax and total cost including tax.
The output will be displayed as shown at bottom of page. This should be exactly like output in previous assignment. Apply feedback from the previous assignment.
Guiding Steps:
Create a new BlueJ Project called: CheckoutDriverProj
You will then create 2 Classes: Cart and CheckoutDriver
First File: Cart.java
Using BlueJ open the CheckoutDriverProj project, Create a new Cart class called: Cart.
This file will only contain attributes and methods for the Cart class.
This is NOT the Driver program so there will NOT be a main() method in this class/file. The Driver program will contain main() and will be in the second file you create later in project.
NOTE: Naming convention for class names.The first character in a Class name should always have an Upper Case character.
This creates a Cart.java source file.
All Java Classes typically contain the following:
Class Attributes
Constructors (specialized methods)
Methods
The Cart Class will contain the following Class Attributes. We will set the access modifier to private so the attributes cannot be directly accessed from an object outside of this class (encapsulation). Choose the appropriate data type for each attribute.:
firstName - customers first name.
lastName - customers last name.
numItems - number of items purchased.
unitCost - cost of one item.
STATE_TAX_RATE as a constant of 7%.
The Cart Class will contain the following Constructor:
Cart(String fn, String ln)
This constructor has 2 input parameters (fn and ln)
Write the logic for the Cart constructor to initialize the class attributes.
NOTE: The name of the Constructor is always the Same Name as the Class.
NOTE: The Constructor will have 2 input parameters (fn and ln). The first parameter will be the customers first name and the second parameter will be the customers last name.
The Constructor will use the fn, and ln parameters to initialize the firstName and lastName instance variables.
The Constructor will initialize numItems and unitCost to be 0.
The Cart Class will contain the following Class Methods:
setUnitCost - this is a setter/mutator for the unitCost attribute. Setter will have input and no output.
setNumItems - this is a setter/mutator for the numItems attribute. Setter will have input and no output.
getFirstName this is a getter/accessor for the firstName attribute. Getter will return value but no input.
getLastName - this is a getter/accessor for the lastName attribute. Getter will return value but no input.
calculateSubTotal - this method calculates the subtotal excluding tax. No input needed. Method will use instance data contained within the class and return a calculated subtotal.
calculateTaxes - this method calculates the tax on the purchase. No input needed. Method will use instance data contained within the class to calculate and return the taxes for the purchase.
calculateTotalPurchase this method calculates total purchase amount by adding tax to subtotal and returns the total purchase amount.
NOTE: do not create setters for first and last name. I want you to use the Constructor to initialize the first and last name.
Second File: CheckoutDriver.java
Using BlueJ open the CheckoutDriverProj project (if not already open), Create a new Driver Class called: CheckoutDriver.
This creates a CheckoutDriver.java file in your project directory. The CheckoutDriver class will:
interact with the user and pop up dialog boxes to collect input from user.
call the Cart constructor to instantiate and initialize the Cart object.
display the purchase of the Cart. This logic should not need changed from previous assignment. Apply improvements.
Use the JOptionPane class to allow user to enter data Using the Dialog boxes. Do not use Scanner. Remember to import JOptionPane from javax.swing.JOptionPane.
The CheckoutDriver class will (within the main() method):
Prompt the user with Dialog box to input their First Name and initialize local variable.
Prompt the user with Dialog box to input their Last Name and initialize local variable.
Construct a Cart object using the first and last name as input. Assign to a Cart reference variable.
Prompt the user with Dialog box to input the number of items being purchased.
Set Cart objects number of items using the Cart class setter/mutator.
Prompt the user with Dialog box to input the unit cost for the item.
Set Cart objects unit cost using the Cart class setter/mutator.
Print output of Cart as described in output section below. Use the Cart object when needing to print info about Cart:
Call the Cart objects calculateSubTotal method to Calculate the Subtotal for the items purchased.
Call the Cart objects calculateTaxes method to Calculate the State Tax for the Subtotal.
Call the Cart objects calculateTotalPurchase method to Calculate the Total Cost adding the Subtotal and State Tax.
NOTE: you should only need firstName, lastName, a Cart reference variable, and dialogInput string declared as local variables in the main() method. Use the Cart object to hold information about the Cart.
From the description of the problem above,
think about what variables need to be declared,
what should the data types be for each of those variables,
are there any constants,
what calculation
s need executed.
Your program Output to the Console will then:
Use the Cart object attributes when printing all data in output below. No not use the local variables.
Print the first column left justified.
Print the second column right-aligned and also so decimal positions line up.
Print the Dollar amounts so they will be aligned by the Decimal point and 9 characters wide.
Print calculated field amounts with comma and as rounded with 2 decimal positions.
Print entire Last Name displayed in UPPER CASE. You need to use a String method to upper case.
Hint: When formatting output, you will be using printf and formatting specifiers on columns with and number of decimal positions to display.
Sample output format below:
Sample output format below:
|
YOUR PURCHASE
Name: LASTNAME, firstname
Number of nights: x
|
Unit Cost: $xx,xxx.xx
Sub Total: $xx,xxx.xx
+ NJ State Tax: $xx,xxx.xx
= Total: $xx,xxx.xx
Example output:
YOUR PURCHASE
Name: SMITH, john
Number of items: 2
Unit Cost: $ 13.45
Subtotal: $ 67.25
+ NJ State Tax: $ 4.71
= Total: $ 71.96
Test Your Program: Run your program using different inputs. Check to make sure program runs with different inputs.
Type in your last Name with some lower case letters.
Type in your first name.
Try different number of units.
Try different Unit Costs.
Make sure the subtotal is calculated correctly
Make sure the State Tax is calculated correctly.
Make sure the Total is calculated correctly.
Make sure the output is formatted as described above.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
