Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Design Patterns in the Gourmet Coffee System Prerequisites, Goals, and Outcomes Prerequisites: Before you begin this exercise, you need mastery of the following: Object

Using Design Patterns in the Gourmet Coffee System
Prerequisites, Goals, and Outcomes
Prerequisites: Before you begin this exercise, you need mastery of the following:
Object-oriented Programming
How to define interfaces
How to implement interfaces
Design Patterns:
Knowledge of the singleton pattern
Knowledge of the strategy pattern
Goals: Reinforce your ability to use the singleton and strategy patterns
Outcomes: You will demonstrate mastery in the following:
Producing applications that use the singleton pattern
Producing applications that use the strategy pattern
Background
In this assignment, you will create another version of the Gourmet Coffee System. This version will present the user with four choices:
[0] Quit
[1] Display sales (Plain Text)
[2] Display sales (HTML)
[3] Display sales (XML)
choice>
The user will be able to display the sales information in three formats: plain text, HTML, or XML. Part of the work has been done for you and is provided in the student archive. You will implement the code that formats the sales information. This code will use the singleton and strategy patterns.
Description
The following class diagram shows how the singleton and strategy pattern will be used in your implementation:
Figure 1 Portion of Gourmet Coffee System class diagram
Figure 1 Portion of Gourmet Coffee System class diagram
The elements of the pattern are:
Interface SalesFormatter declares a method called formatSales that produces a string representation of the sales information.
Class PlainTextSalesFormatter implements formatSales. Its version returns the sales information in a plain-text format.
Class HTMLSalesFormatter implements formatSales. Its version returns the sales information in an HTML format.
Class XMLSalesFormatter implements formatSales. Its version returns the sales information in an XML format.
Class GourmetCoffee is the context class. It also contains client code. The client code calls:
Method GourmetCoffee.setSalesFormatter to change the current formatter
Method GourmetCoffee.displaySales to display the sales information using the current formatter
In this assignment, you should implement the following interface and classes:
SalesFormatter
PlainTextSalesFormatter
HTMLSalesFormatter
XMLSalesFormatter
GourmetCoffee (a partial implementation is provided in the student archive)
Complete implementations of the following classes are provided in the student archive:
Coffee
CoffeeBrewer
Product
Catalog
OrderItem
Order
Sales
Interface SalesFormatter
Interface SalesFormatter declares the method that every "Formatter" class will implement.
Method:
public String formatSales(Sales sales). Produces a string representation of the sales information.
Class PlainTextSalesFormatter
Class PlainTextSalesFormatter implements the interface SalesFormatter. This class is implemented as a singleton so a new object will not be created every time the plain-text format is used.
Static variable:
singletonInstance. The single instance of class PlainTextSalesFormatter.
Constructor and methods:
static public PlainTextSalesFormatter getSingletonInstance(). Static method that obtains the single instance of class PlainTextsalesFormatter.
private PlainTextSalesFormatter(). Constructor that is declared private so it is inaccessible to other classes. A private constructor makes it impossible for any other class to create an instance of class PlainTextSalesFormatter.
public String formatSales(Sales sales). Produces a string that contains the specified sales information in a plain-text format. Each order in the sales information has the following format:
------------------------
Order number
quantity1 code1 price1
quantity2 code2 price2
...
quantityN codeN priceN
Total = totalCost
where
number is the order number.
quantityX is the quantity of the product.
codeX is the code of the product.
priceX is the price of the product.
totalCost is the total cost of the order.
Each order should begin with a dashed line. The first order in the sales information should be given an order number of 1, the second should be given an order number of 2, and so on.
Class HTMLSalesFormatter
Class HTMLSalesFormatter implements the interface SalesFormatter. This class is implemented as a singleton so a new object will not be created every time the HTML format is used.
Static variable:
singletonInstance. The single instance of class HTMLSalesFormatter.
Constructor and methods:
static public HTMLSalesFormatter getSingletonInstance(). Static method that obtains the single instance of class HTMLSalesFormatter.
private HTMLSalesFormatter(). Constructor that is declared private so it is inaccessible to other classes. A private constructor makes it impossible for any other class to create an instance of class HTMLSalesFormatter.
public String formatSales(Sales sales). Produces a string that contains the specified sales information in an HTML format.
The string should begin with the following HTML:
code: code1
quantity: quantity1
price: price1
code: codeN
quantity: quantityN
price: priceN
code: C001
quantity: 5
price: 17.99
code: C002
quantity: 2
price: 18.75
code: A001
quantity: 2
price: 9.0
code: B002
quantity: 1
price: 200.0
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

What is the purpose of the risk score, and how is it calculated?

Answered: 1 week ago