Question
Draw a UML structure diagram showing the relationship between the classes in your solution. You may hand draw or use any tool of your choice
- Draw a UML structure diagram showing the relationship between the classes in your solution. You may hand draw or use any tool of your choice but the diagram must be submitted in a format that I can read (one of .docx, .pdf, .jpg, .txt). If I cannot read it, I cannot grade it.
- Draw a UML class diagram for your ShoppingCart class (may be combined with the structure diagram).
import java.text.DecimalFormat;
// Defines class GroceryItem
public class GroceryItem
{
// Defines decimal format
String decFormat = "####,####.##";
// Creates an object of the class DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat(decFormat);
// Instance variables to store item information
private String name;
private double price;
private int qty;
// Parameterized constructor to assign parameter values to instance variables
public GroceryItem (String name, double price, int qty)
{
this.name = name;
this.price = price;
this.qty = qty;
}
// Getter methods
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public int getQty()
{
return qty;
}
// Setter methods
public void setName(String name)
{
this.name = name;
}
public void setPrice(double price)
{
this.price = price;
}
public void setQty(int qty)
{
this.qty = qty;
}
// Overrides equals() method to return true
// if parameter object item name is equals to implicit object item name
// Otherwise returns false
public boolean equals(Object item)
{
// Checks if parameter object is equals to implicit object
if(item == this)
return true;
// Check if parameter object is not an instance of GroceryItem
if(!(item instanceof GroceryItem))
return false;
// Type-cast parameter object to GroceryItem
GroceryItem gi = (GroceryItem) item;
// Compare the parameter object name with implicit object name
if(gi.getName().equalsIgnoreCase(name))
return true;
else
return false;
}
// Overrides toString() method to return item information
public String toString()
{
return String.format(" %-16s", name) +
String.format("%-12s", decimalFormat.format(price)) +
String.format("%-10d", qty) +
String.format("%-10s", decimalFormat.format(qty * price)) + " ";
}
}// End of class GroceryItem
//Defines class ShoppingCart
public class ShoppingCart
{
//Instance variables to store shopping cart information
String customerName;
//Declares an array of object of class GroceryItem
GroceryItem order[];
int numItems;
//Default constructor to assign default values to instance variables
//and creates the order array of size 10
ShoppingCart()
{
customerName = "Harun";
order = new GroceryItem[10];
numItems = 0;
}
//Overloads constructor to assign parameter customer name to instance variable
//name and creates the order array of size 10
ShoppingCart(String name)
{
customerName = name;
order = new GroceryItem[10];
numItems = 0;
}
//Overloads constructor to assign parameter customer name and capacity
//to instance variable name
//and creates the order array of size parameter capacity
ShoppingCart(String name, int capacity)
{
customerName = name;
order = new GroceryItem[capacity];
numItems = 0;
}
//Method to add an item to cart
//If added successfully then returns true
//Otherwise returns false
boolean add(String name, double price, int quantity)
{
// Checks if current number of items is equals to length of the array
// displays the error message and returns false
if(numItems == order.length)
{
System.out.println(" Unable to add item cart is full.");
return false;
}
// Otherwise add the item to caret
else
{
// Creates an object of class GroceryItem using parameterized constructor
// Assigns it to numItems index position of order
// and increases the numItems by one
order[numItems++] = new GroceryItem(name, price, quantity);
return true;
}
}
//Method to search the parameter name in the cart
//If found then returns the found item index position
//Otherwise returns -1
int find(String name)
{
// Loops till number of items available in the shopping cart
for(int c = 0; c < numItems; c++)
// Checks if parameter item name is equals current object
// item name then returns loop variable as found index position
if(name.equals(order[c].getName()))
return c;
// Otherwise returns -1 for not found
return -1;
}
//Method to calculate and returns the total amount of products available
//in the cart
double getTotalBeforeTax()
{
double amount = 0;
// Loops till number of items available in the shopping cart
for(int c = 0; c < numItems; c++)
// Calculates total
amount += (order[c].getQty() * order[c].getPrice());
// returns amount
return amount;
}
//Method to calculate and returns the total tax for the products available
//in the cart
double getTax(double taxRate)
{
double tax = 0;
// Loops till number of items available in the shopping cart
for(int c = 0; c < numItems; c++)
// Calculates tax of each product and adds it
tax += (order[c].getQty() * order[c].getPrice()) * (taxRate / 100.0);
// returns total tax
return tax;
}
//Method to return number of items available in the cart
int getNumGroceryItems()
{
return numItems;
}
//Method to return total number quantity available in the cart
int getTotalQuantity()
{
int total = 0;
// Loops till number of items available in the shopping cart
for(int c = 0; c < numItems; c++)
// Calculates total for the quantity
total += order[c].getQty();
// returns total quantity
return total;
}
//Overrides toString() method to return cart information
public String toString()
{
String result = " ******************************************";
result += " \t Customer Name: " + customerName +
" ******************************************" +
String.format(" %-16s", "Item") +
String.format("%-12s", "Price") + String.format("%-10s", "Qty") +
String.format("%-10s ", "Total");
// Loops till number of items available in the shopping cart
for(int c = 0; c < numItems; c++)
result += order[c].toString();
result += String.format(" %-14s $%-10.2f", "Total:", getTotalBeforeTax());
result += String.format(" %-14s $%-10.2f", "Tax: ", getTax(7.5));
result += String.format(" %-14s $%-10.2f", "Grand total: ",
(getTotalBeforeTax() + getTax(7.5)));
return result;
}
}// End of class ShoppingCart
public class ShoppingCartDriver
{
// main method definition
public static void main(String ss[])
{
// Creates an object of class ShoppingCart using
// one argument parameterized constructor
ShoppingCart one = new ShoppingCart("Harun");
// Creates an object of class ShoppingCart using
// two argument parameterized constructor
ShoppingCart two = new ShoppingCart("Ali", 2);
// Calls the method to add item to cart one
one.add("Rice", 1.50, 3);
one.add("Sushi", 8.25, 2);
one.add("Sambusa", 5.80, 4);
// Displays cart one information with report
System.out.println(one);
System.out.println(" Number of items available in the cart: " +
one.getNumGroceryItems());
System.out.println(" Total quantity available in the cart: " +
one.getTotalQuantity());
// Calls the method to add item to cart one
two.add("Sugar", 52.22, 9);
two.add("Milk Powder", 118.90, 4);
two.add("Coffee", 22.57, 1);
// Displays cart one information with report
System.out.println(two);
System.out.println(" Number of items available in the cart: " +
one.getNumGroceryItems());
System.out.println(" Total quantity available in the cart: " +
one.getTotalQuantity());
}
}// End of driver class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started