0% found this document useful (0 votes)
5 views4 pages

OOP Discount System and Polyline Class

The document outlines a lab assignment for creating an object-oriented discount system for a beauty salon, which includes three membership types with varying discounts. It also describes a polyline class that utilizes an ArrayList to manage points, detailing methods for appending points and calculating total length. Additionally, a test driver for the polyline class is provided to demonstrate its functionality.

Uploaded by

Minh Khôi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

OOP Discount System and Polyline Class

The document outlines a lab assignment for creating an object-oriented discount system for a beauty salon, which includes three membership types with varying discounts. It also describes a polyline class that utilizes an ArrayList to manage points, detailing methods for appending points and calculating total length. Additionally, a test driver for the polyline class is provided to demonstrate its functionality.

Uploaded by

Minh Khôi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HaQT Object-Oriented Programming

Lab 6. OOP

1 The Discount System


You are asked to write a discount system for a beauty saloon, which provides services and sells
beauty products. It offers 3 types of memberships: Premium, Gold and Silver. Premium,
gold and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services
provided. Customers without membership receive no discount. All members receives a flat
10% discount on products purchased (this might change in future). Your system shall consist
of three classes: Customer, Discount and Visit, as shown in the class diagram. It shall
compute the total bill if a customer purchases x of products and y of services, for a visit.
Also write a test program to exercise all the classes.

The class DiscountRate contains only static variables and methods (underlined in the class
diagram).

1
HaQT Object-Oriented Programming

2 Polyline of Points with ArrayList

A polyline is a line with segments formed by points. Let’s use the ArrayList (dynamically
allocated array) to keep the points, but upcast to List in the instance variable. (Take note
that array is of fixed-length, and you need to set the initial length).

1 p u b l i c c l a s s Po in t {
private int x ;
3 private int y ;
p u b l i c P oi nt ( i n t x , i n t y ) { . . . . . . }
5 p u b l i c i n t getX ( ) { . . . . . . }
p u b l i c i n t getY ( ) { . . . . . . }
7 p u b l i c v o i d setX ( i n t x ) { . . . . . . }
p u b l i c v o i d setY ( i n t y ) { . . . . . . }
9 p u b l i c i n t [ ] getXY ( ) { . . . . . . }
p u b l i c v o i d setXY ( i n t x , i n t y ) { . . . . . . }
11 public String toString () { . . . . . . }
p u b l i c d o u b l e d i s t a n c e ( Po in t a n o t h e r ) { . . . . . . }
13 }

1 import j a v a . u t i l . ∗ ;
p u b l i c c l a s s PolyLine {
3 p r i v a t e L i s t <Point> p o i n t s ; // L i s t o f P oi nt i n s t a n c e s

5 // C o n s t r u c t o r s
p u b l i c P o l y L i n e ( ) { // d e f a u l t c o n s t r u c t o r
7 p o i n t s = new A r r a y L i s t <Point >() ; // implement with A r r a y L i s t
}
9 p u b l i c P o l y L i n e ( L i s t <Point> p o i n t s ) {
this . points = points ;
11 }

2
HaQT Object-Oriented Programming

13 // Append a p o i n t ( x , y ) t o t h e end o f t h i s p o l y l i n e
p u b l i c v o i d appendPoint ( i n t x , i n t y ) {
15 P oint newPoint = new P oi nt ( x , y ) ;
p o i n t s . add ( newPoint ) ;
17 }

19 // Append a p o i n t i n s t a n c e t o t h e end o f t h i s p o l y l i n e
p u b l i c v o i d appendPoint ( Po in t p o i n t ) {
21 p o i n t s . add ( p o i n t ) ;
}
23
// Return { ( x1 , y1 ) ( x2 , y2 ) ( x3 , y3 ) . . . . }
25 public String toString () {
// Use a S t r i n g B u i l d e r t o e f f i c i e n t l y b u i l d t h e r e t u r n S t r i n g
27 S t r i n g B u i l d e r sb = new S t r i n g B u i l d e r ( ”{ ” ) ;
f o r ( Point a Point : p o i n t s ) {
29 sb . append ( aP oint . t o S t r i n g ( ) ) ;
}
31 sb . append ( ” } ” ) ;
r e t u r n sb . t o S t r i n g ( ) ;
33 }

35 // Return t h e t o t a l l e n g t h o f t h i s p o l y l i n e
p u b l i c d o u b l e get L eng t h ( ) { . . . . . . }
37 }

1 /∗
∗ A Test D r i v e r f o r t h e P o l y L i n e c l a s s .
3 ∗/
import j a v a . u t i l . ∗ ;
5 public c l a s s TestPolyLine {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
7 // Test d e f a u l t c o n s t r u c t o r and t o S t r i n g ( )
P o l y L i n e l 1 = new P o l y L i n e ( ) ;
9 System . out . p r i n t l n ( l 1 ) ; // {}

11 // Test appendPoint ( )
l 1 . appendPoint ( new Point ( 1 , 2 ) ) ;
13 l 1 . appendPoint ( 3 , 4 ) ;
l 1 . appendPoint ( 5 , 6 ) ;
15 System . out . p r i n t l n ( l 1 ) ; // { ( 1 , 2 ) ( 3 , 4 ) ( 5 , 6 ) }

17 // Test c o n s t r u c t o r 2
L i s t <Point> p o i n t s = new A r r a y L i s t <Point >() ;
19 p o i n t s . add ( new Point ( 1 1 , 1 2 ) ) ;
p o i n t s . add ( new Point ( 1 3 , 1 4 ) ) ;
21 P o l y L i n e l 2 = new P o l y L i n e ( p o i n t s ) ;

3
HaQT Object-Oriented Programming

System . out . p r i n t l n ( l 2 ) ; // { ( 1 1 , 1 2 ) ( 1 3 , 1 4 ) }
23 }
}

Common questions

Powered by AI

Using an ArrayList for storing points in the PolyLine class offers several advantages over a fixed-length array. An ArrayList provides dynamic resizing, which means it can automatically adjust its capacity when new elements are added. This feature eliminates the need to define an initial fixed size and simplifies adding new points, as demonstrated by the appendPoint methods. ArrayLists also provide more convenient methods for insertion and manipulation of elements .

The use of a test program enhances the reliability by systematically verifying that each component of the Polyline class functions as expected. It allows developers to confirm correct initialization via constructors, validate the proper functioning of methods like appendPoint and toString, and ensure integration of points into the polyline through practical usages. This kind of thorough testing supports the stable operation of the Polyline class in broader applications .

The design of the discount system allows for flexibility because all members currently receive a flat 10% discount on products, which is encapsulated in the DiscountRate class containing only static variables and methods. This modular design can easily be adjusted in the future to change the discount rates for different membership types without altering the overall system structure .

It is crucial to use private access modifiers for data fields in the Point class to enforce encapsulation by restricting direct access from other classes. This helps maintain data integrity by preventing unauthorized or unintended modifications. It also facilitates future maintenance as internal changes can be made without impacting external class implementations, maintaining the internal class logic and interfaces .

The PolyLine class uses a StringBuilder to efficiently build its string representation. This approach is effective because StringBuilder is designed for scenarios where a lot of string manipulation is necessary, as it is faster than regular string concatenation in a loop due to its mutable nature and lack of creation of intermediate String objects .

Encapsulation in the Point class is achieved through the use of private fields (x and y), which restrict direct access from outside the class. Accessor (getX, getY) and mutator (setX, setY) methods are provided to allow controlled access and modification of these private fields. This encapsulation principle protects the integrity of the data and allows changes to the implementation without affecting external code .

Polymorphism in the beauty salon discount system is demonstrated through the membership discount application. The system applies different discounts based on the type of membership (Premium, Gold, Silver), which could be managed by overriding methods in classes derived from a common interface or base class. This allows for dynamic method dispatch based on the actual membership type of the Customer object during a visit .

When implementing the total bill computation for a customer visit, important design considerations include ensuring accuracy in applying different discounts based on membership type, handling possible future changes in discount rates especially on products, and the ability to easily expand to accommodate new types of services or memberships. Additionally, maintaining a clear separation of concerns through modular classes (such as Customer, Discount, Visit) will make the system more maintainable and scalable .

Aggregation is applied in the PolyLine class design through its use of a List to maintain a collection of Point objects. Each PolyLine object contains, or aggregates, multiple Point objects representing individual vertices of the polyline. This relationship allows for individual Point objects to exist independently of the PolyLine, allowing for flexibility in constructing complex shapes through simple component points without excessive coupling .

The Visit class contributes to extensibility by acting as an interaction interface between the customer and services/products offered, which can easily accommodate new features, such as additional services or promotional rates. By centralizing visit-related logic, new functionalities such as tracking visit frequency for additional discounts or integrating an appointment system can be added without disrupting the core discount computation mechanism .

You might also like