Professional Documents
Culture Documents
Lab 04
Lab 04
Mục lục
1. Import the existing project into the workspace of Eclipse .................................................................. 2
2. Additional requirements of AIMS ......................................................................................................... 2
3 Creating the Book class ........................................................................................................................... 2
3.1 Add fields to the Book class .............................................................................................................. 2
3.2 Create the accessor methods ............................................................................................................ 3
3.3 Create addAuthor(String authorName) and removeAuthor(String authorName) for the Book
class .......................................................................................................................................................... 3
4 Creating the abstract Media class .......................................................................................................... 4
4.1 Create the Media class in the project .............................................................................................. 4
4.2 Remove fields and methods from Book and DigitalVideoDisc classes ......................................... 4
4.2.1 Book class .................................................................................................................................... 4
4.2.2 DigitalVideoDisc class ................................................................................................................. 5
5 Creating the CompactDisc class ............................................................................................................. 6
5.1 Create the Disc class extending the Media class ............................................................................. 6
5.2. Create the Track class which models a track on a compact disc and will store information
incuding the title and length of the track .............................................................................................. 8
5.3. Open the CompactDisc class ........................................................................................................... 8
6 Create the Playable interface ................................................................................................................ 10
7 Update the Cart class to work with Media .......................................................................................... 11
8 Update the Store class to work with Media ......................................................................................... 11
9 Constructors of whole classes and parent classes................................................................................ 12
10 Unique item in a list ............................................................................................................................. 13
11 Polymorphism with toString() method ............................................................................................... 13
12 Sort media in the cart .......................................................................................................................... 14
13 Create a complete console application in the Aims class................................................................. 17
1
Mã nguồn ................................................................................................................................................ 17
2
3.2 Create the accessor methods
3
4 Creating the abstract Media class
4.1 Create the Media class in the project
4.2 Remove fields and methods from Book and DigitalVideoDisc classes
4
4.2.2 DigitalVideoDisc class
5
5 Creating the CompactDisc class
5.1 Create the Disc class extending the Media class
- Create the Disc
6
- Make the DigitalVideoDisc extending the Disc class
7
- Create the CompactDisc extending the Disc class
5.2. Create the Track class which models a track on a compact disc and will
store information incuding the title and length of the track
8
- Create method addTrack() and removeTrack()
9
6 Create the Playable interface
- Create Playable interface
10
- Implement play() for DigitalVideoDisc and Track
11
9 Constructors of whole classes and parent classes
12
10 Unique item in a list
- Create an ArrayList of Media, then add some media (CD, DVD or Book) into the
list.
13
- Result:
- Giải thích: ta thấy rằng phương thức toString() là phương thức trả về chuỗi thông
tin của sản phẩm. Sau khi thêm 3 sản phẩm là cd, dvd, book vào list ta sẽ dùng
vòng lặp để in ra thông tin từng sản phẩm có trong đó. Mặc dù ba lớp trên đều
không có phương thức toString(), nhưng do có kế thừa từ lớp cha Media nên chúng
có thể sử dụng được phương thức đó.
14
- Add the comparators as attributes of the Mediaclass:
15
and the result is:
16
13 Create a complete console application in the Aims class
Mã nguồn
/**
*/
package Store;
import java.util.Scanner;
import Cart.Cart;
import Media.Book;
import Media.CompactDisc;
import Media.DigitalVideoDisc;
import Media.Media;
17
public Aims() {
store.addMedia(media);
System.out.println("AIMS ");
System.out.println("--------------------------------");
System.out.println("0. Exit");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("Options: ");
System.out.println("--------------------------------");
18
System.out.println("4. See current cart");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("--------------------------------");
System.out.println("2. Play");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("--------------------------------");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
19
public static int addNewMedia() {
System.out.println("--------------------------------");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("--------------------------------");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("--------------------------------");
20
System.out.println("1. Filter by id");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("--------------------------------");
System.out.println("0. Back");
System.out.println("--------------------------------");
return scanner.nextInt();
System.out.println("Welcome to AIMS!");
return showMenu();
while (true) {
switch (choice) {
21
case 1:
viewStore();
break;
case 2:
updateStore();
break;
case 3:
viewCart();
break;
case 0:
System.out.println("Goodbye!");
return;
default:
System.out.println("Invalid choice!");
break;
store.print();
while (true) {
switch (ConsoleMenu.storeMenu()) {
case 1: {
scanner.nextLine();
if (foundMedia == null) {
22
} else {
foundMedia.printDetail();
switch (ConsoleMenu.mediaDetails()) {
case 1:
if (!cart.addMedia(foundMedia))
else
break;
case 2:
foundDisc.play();
} else {
break;
case 0:
break;
default:
System.out.println("Invalid choice!");
break;
break;
case 2: {
store.print();
23
scanner.nextLine();
if (foundMedia == null) {
} else {
if (cart.addMedia(foundMedia))
else
break;
case 3: {
scanner.nextLine();
if (foundMedia == null) {
foundDisc.play();
} else {
break;
case 4:
cart.displayCart();
break;
24
case 0:
return;
default:
System.out.println("Invalid choice!");
break;
while (true) {
switch (ConsoleMenu.updateStore()) {
case 1:
switch (ConsoleMenu.addNewMedia()) {
case 1: {
System.out.println("Title: ");
scanner.nextLine();
System.out.println("Category: ");
System.out.println("Cost: ");
store.addMedia(newBook);
break;
case 2: {
25
System.out.println("Title: ");
scanner.nextLine();
System.out.println("Category: ");
System.out.println("Cost: ");
System.out.println("Director: ");
scanner.nextLine();
System.out.println("Artist: ");
store.addMedia(newCD);
break;
case 3: {
System.out.println("Title: ");
scanner.nextLine();
System.out.println("Category: ");
System.out.println("Cost: ");
System.out.println("Director: ");
scanner.nextLine();
System.out.println("Length: ");
26
int length = scanner.nextInt();
store.addMedia(newDVD);
break;
case 0:
break;
default:
System.out.println("Invalid choice!");
break;
break;
case 2:
store.print();
scanner.nextLine();
if (foundMedia == null)
else {
store.removeMedia(foundMedia);
System.out.println("Deleted successfully!");
break;
case 0:
return;
default:
System.out.println("Invalid choice!");
break;
27
}
cart.displayCart();
while (true) {
switch (ConsoleMenu.viewCart()) {
case 1:
switch (ConsoleMenu.filterCart()) {
case 1: {
scanner.nextLine();
if (mediaFound == null)
else
System.out.println(mediaFound.toString());
break;
case 2: {
scanner.nextLine();
if (mediaFound == null)
else
System.out.println(mediaFound.toString());
28
break;
case 0:
break;
default:
System.out.println("Invalid choice!");
break;
break;
case 2:
switch (ConsoleMenu.sortCart()) {
case 1:
cart.sortCartByTitle();
cart.displayCart();
break;
case 2:
cart.sortCartByCost();
cart.displayCart();
break;
case 0:
break;
default:
System.out.println("Invalid choice!");
break;
break;
case 3: {
scanner.nextLine();
29
if (foundMedia == null)
else {
cart.removeMedia(foundMedia);
System.out.println("Deleted successfully!");
break;
case 4:
scanner.nextLine();
if (foundMedia == null) {
foundDisc.play();
} else {
break;
case 5:
System.out.println("Order is created!");
break;
case 0:
return;
default:
System.out.println("Invalid choice!");
30
break;
31