Description
River Kelly and Peyton Dorsh
Class Diagram
Java Code
Console Output
—————————————ESOF-322: Homework 3
—————————————-
Step 1: Select an Inventory Module
—————————————-
1 – Inventory1
2 – Inventory2
3 – Inventory3
(’c’ to exit) > 1
New Inventory Module Created
—————————————Inventory Module: Inventory1
Default sorting method: BubbleSort
Step 2: Perform (default) Sorting Method
—————————————-
–> Inventory1::sort()
The sorting method is: BubbleSort
–> BubbleSort::sort()
Performing BubbleSort…
Step 3: Request User Selection (Sorting Method)
—————————————-
1 – BubbleSort
2 – MergeSort
3 – QuickSort
(’c’ to exit) > 3
Step 4: Dynamically Change Sorting Method
—————————————-
–> Inventory1::setSortMethod(ISortingMethod method) Dynamically set sorting method to: QuickSort
Step 5: Perform Sorting Method
—————————————-
–> Inventory1::sort()
The sorting method is: QuickSort
–> QuickSort::sort()
Performing Quicksort…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Client Class
// Client.java import java.util.Scanner;
/**
* Client class
*
* – Main driver class for Homework 3
1
2
3
4
5
6
7
8
9 * – (i.e. has public static void main(String[] args))
10 * 11 */
public class Client {
// main method public static void main(String[] args) {
// initialize local Inventory variable
InventoryModule inventory = null;
// initialize scanner, to read in user input from console
Scanner console = new Scanner(System.in);
// variable to store user input String user_selection = null; int user_int_selection = 0;
/*
* The following variables are used to by the application. Either for output to
* the console or to maintain the running state
*/
InventoryModule[] inventory_arr = new InventoryModule[3]; int inv_index = 0;
// string “constant”, used for output to the user
String dv = “—————————————-“;
// continue running application loop with true
Boolean running = true;
// output to user (via console)
System.out.printf(“%s %s %s “, dv, “ESOF-322: Homework 3″, dv); do {
// Step 1: Select an Inventory Module
// —————————————————–// – prompt the user to select an Inventory module. // – create the selected Inventory module and assign it // to the local variable ’inventory’.
//
// output to user
System.out.printf(” %s %s “, “Step 1: Select an Inventory
Module”, dv);
System.out.printf(“%d – %s %d – %s %d – %s “, 1, “Inventory1”,
2, “Inventory2”, 3, “Inventory3”);
// get user input from console (Scanner) System.out.print(“(’c’ to exit) > “); user_selection = console.nextLine();
// checked if user wants to exit if (user_selection.charAt(0) == ’c’) { // user wants to exit running = false; // set running state to false continue;
}
try { user_int_selection = Integer.parseInt(user_selection);
} catch (Exception e) {
System.out.printf(“%s: %s “, “User Input Error”, e.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
getMessage());
67 continue;
68 }
inv_index = user_int_selection – 1; if (inventory_arr[inv_index] == null) {
System.out.printf(” %s %s “, “New Inventory Module Created
“, dv); switch (user_int_selection) { case 1: inventory = new Inventory1(); break;
case 2: inventory = new Inventory2(); break;
case 3: inventory = new Inventory3(); break;
default:
System.out.println(” Invalid input: ” +
user_selection);
continue;
} inventory_arr[inv_index] = inventory;
} else {
inventory = inventory_arr[inv_index];
}
// Step 2: Perform default sorting method
// —————————————————–//
System.out.printf(” %s %s “, “Step 2: Perform (default)
Sorting Method”, dv);
inventory.preformSort();
// Step 3: Request User Selection
// ——————————————————
// Get a new sorting method from the user to dynamically
// change the inventory’s sorting behavior
//
System.out.printf(” %s %s “, “Step 3: Request User Selection ( Sorting Method)”, dv);
System.out.printf(“%d – %s %d – %s %d – %s “, 1, “BubbleSort”,
2, “MergeSort”, 3, “QuickSort”);
// get user input from console (Scanner) System.out.print(“(’c’ to exit) > “); user_selection = console.nextLine();
// checked if user wants to exit if (user_selection.charAt(0) == ’c’) { // user wants to exit running = false; // set running state to false continue;
}
// Step 4: Dynamically Change Sorting Method
// ——————————————————
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 // Set the inventory’s sorting method dynamically
122 //
123
124 System.out.printf(” %s %s”, “Step 4: Dynamically Change Sorting
Method”, dv);
ISortingMethod new_sorting_method; switch (user_selection) { case “1”: // Bubble Sort new_sorting_method = new BubbleSort(); break;
case “2”: // Merge Sort new_sorting_method = new MergeSort(); break;
case “3”: // Quick Sort new_sorting_method = new QuickSort(); break;
default: // Invalid selection from user input
System.out.println(” Invalid input: ” + user_selection); continue;
}
inventory.setSortMethod(new_sorting_method); // Change
dynamically to BubbleSort
// Step 5: Perform Sort
// ——————————————————
// Invoke the inventories sorting method behavior
//
System.out.printf(” %s %s “, “Step 5: Perform Sorting Method”,
dv);
inventory.preformSort();
} while (running); // END of application’s loop console.close(); // Close the console (Scanner)
} // END of Client::main method
} // END of Client class
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
ISortingMethod Interface
// ISortingMethod.java
/**
* ISortingMethod (Interface)
*
* This is the “behavior” interface for the sorting_method
*
* Each of the unique sorting method type
* must implement this interface, allowing the * Inventory Modules to sort a sorting behavior * as a composite value.
*
* Each child class must provide:
* – method “sort()”
*
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface ISortingMethod {
// all Sorting classes must implement public void sort();
} // END of ISortingMethod class their own sort function
17
18
19
20
21
22
BubbleSort Class
// BubbleSort.java
/**
* BubbleSort class
*
* This is one of three possible sorting methods.
* Each sorting type implements the ISortingMethod interface.
*
*/ public class BubbleSort implements ISortingMethod {
// BubbleSorts implementation of the sort method public void sort() {
System.out.println(“–> BubbleSort::sort() Performing
);
} // END of sort()
} // END of BubbleSort class BubbleSort…”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MergeSort Class
// MergeSort.java
/**
* MergeSort class
*
* This is one of three possible sorting methods.
* Each sorting type implements the ISortingMethod interface.
*
*/ public class MergeSort implements ISortingMethod {
// MergeSorts implementation of the sort method public void sort() {
System.out.println(“–> MergeSort::sort() Performing } // END of sort()
} // END of MergeSort class MergeSort…”);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
QuickSort Class
// QuickSort.java
1
2
3 /**
* QuickSort class
*
* This is one of three possible sorting methods.
* Each sorting type implements the ISortingMethod interface.
*
*/ public class QuickSort implements ISortingMethod {
// QuickSorts implementation of the sort method public void sort() {
System.out.println(“–> QuickSort::sort() Performing } // END of sort()
} // END of QuickSort class Quicksort…”);
4
5
6
7
8
9
10
11
12
13
14
15
16
17
InventoryModule Class
// InventoryModule.java
/**
* Inventory class (Abstract)
*
* The individual Inventory Modules (Inventory1, Inventory2, and Inventory3) * extend this class to inherit common functionality.
*
* Child classes define the “default” sorting method, or behavior,
* in their constructors, which are passed to the parent constructor * via super(ISortingMethod)
*
* The property ’sorting_method’ or type ISortingMethod allows for * composition of a unique sorting method, and allows us to change * the behavior at runtime.
*
*/ abstract public class InventoryModule {
// The interface used by each unique sorting method type. public ISortingMethod sorting_method;
// constructor
public InventoryModule(ISortingMethod sort_method) {
// set the default sorting method sorting_method = sort_method;
// output statement to user
System.out.println(“Inventory Module: ” + this.toString().split(“@”)[0] + ” Default sorting method: ” + sorting_method.toString().split(“@”)[0]);
} // END of constructor
/*
* Sort()
*
* Invokes the sorting method behavior*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*/ public void preformSort() {
// output statement to user
System.out.println(“–> “+this.toString().split(“@”)[0] + “::sort() nThe sorting method is: ” + sorting_method.toString().split(“@”)[0]); sorting_method.sort();
} // END of sort()
/*
* setSortMethod()
*
* changes the default sorting method (dynamically) to the new one chosen by the user
*
*/ public void setSortMethod(ISortingMethod new_sort_method) { // dynamically set the sorting method (behavior) sorting_method = new_sort_method;
// cleanly prints out the new default sorting method to the user System.out.println(” –> “+this.toString().split(“@”)[0]+”::
setSortMethod(ISortingMethod method) Dynamically set sorting method to: ” + sorting_method.toString().split(“@”)[0]);
} // END of setSortMethod()
} // END of Inventory class
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Inventory1 Class
// Inventory1.java
/**
* Inventory1 class
*
* This inventory module extends the InventoryModule class.
*
* The default sorting method is: Bubble Sort
*/ public class Inventory1 extends InventoryModule {
// constructor public Inventory1() {
// call parent constructor super(new BubbleSort()); // set default sorting method: BubbleSort
} // END of constructor
} // END of Inventory1 class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Inventory2 Class
// Inventory2.java
1
2
3 /**
* Inventory2 class
*
* This inventory module extends the InventoryModule class.
*
* The default sorting method is: Merge Sort
*/ public class Inventory2 extends InventoryModule {
// constructor public Inventory2() {
// call parent constructor super(new MergeSort()); // set default sorting
} // END of constructor
} // END of Inventory2 class method: MergeSort
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Inventory3 Class
// Inventory3.java
/**
* Inventory3 class
*
* This inventory module extends the InventoryModule class.
*
* The default sorting method is: Quick Sort
*/ public class Inventory3 extends InventoryModule {
public Inventory3() {
// call parent constructor super(new QuickSort()); // set default sorting
} // END of constructor
} // END of Inventory3 class method: QuickSort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Sequence Diagram
Note: ”Fetch Inventory” Alternative Fragment is on the next page.




Reviews
There are no reviews yet.