Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Báo cáo tuần 4 Thực hành lập trình hướng đối tượng

Tên sinh viên : Chu Việt Anh


MSSV : 20214983

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

1. Import the existing project into the workspace of Eclipse


2. Additional requirements of AIMS
3 Creating the Book class
3.1 Add fields to the Book class

2
3.2 Create the accessor methods

3.3 Create addAuthor(String authorName) and removeAuthor(String


authorName) for the Book class

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.2.1 Book class

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

5.3. Open the CompactDisc class

- Add 2 fields to this class

8
- Create method addTrack() and removeTrack()

- Create the getLength() method

9
6 Create the Playable interface
- Create Playable interface

- Implement the Playable with CompactDisc, DigitalVideoDisc and Track

10
- Implement play() for DigitalVideoDisc and Track

- Implement play() for CompactDisc

7 Update the Cart class to work with Media

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

- For the Media class:

- For the Track class:

11 Polymorphism with toString() method

- Create a toString() method

- 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 đó.

12 Sort media in the cart

- Create two classes of comparators, one for each type of ordering

14
- Add the comparators as attributes of the Mediaclass:

- Test the method:

15
and the result is:

Trả lời câu hỏi:


- Lớp nên implement the Comparable interface là lớp Media.
- Đối với các lớp đó, đối với thứ tự xếp theo giá giảm dần, nếu giá ở sản phẩm phía
sau cao hơn giá ở sản phẩm phía trước ta sẽ tiến hành đổi vị trí cho chúng. Tương
tụ với sắp xếp theo tên sản phẩm tăng dần
- Chúng ta vẫn có thể áp dụng so sánh theo 2 quy tắc đặt hàng của mặt hàng (thoe
tiêu đề rồi đến giá và ngược lại)
- Nếu 1 DVD có quy tắc sắp xếp khác với các loại phương tiện khác:

16
13 Create a complete console application in the Aims class
Mã nguồn
/**

* Author: Chu Việt Anh

*/

package Store;

import java.util.Scanner;

import Cart.Cart;

import Media.Book;

import Media.CompactDisc;

import Media.DigitalVideoDisc;

import Media.Media;

public class Aims {

private static final Scanner scanner = new Scanner(System.in);

private Store store = new Store();

private Cart cart = new Cart();

17
public Aims() {

public Aims(Media... mediaList) {

for (Media media : mediaList)

store.addMedia(media);

static class ConsoleMenu {

public static int showMenu() {

System.out.println("AIMS ");

System.out.println("--------------------------------");

System.out.println("1. View store");

System.out.println("2. Update store");

System.out.println("3. See current cart");

System.out.println("0. Exit");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2-3");

return scanner.nextInt();

public static int storeMenu() {

System.out.println("Options: ");

System.out.println("--------------------------------");

System.out.println("1. See a media\'s details");

System.out.println("2. Add a media to cart");

System.out.println("3. Play a media");

18
System.out.println("4. See current cart");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2-3-4");

return scanner.nextInt();

public static int mediaDetails() {

System.out.println(" Options: ");

System.out.println("--------------------------------");

System.out.println("1. Add to cart");

System.out.println("2. Play");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2");

return scanner.nextInt();

public static int updateStore() {

System.out.println("\nUpdate store - Options: ");

System.out.println("--------------------------------");

System.out.println("1. Add a new item");

System.out.println("2. Remove an item");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2");

return scanner.nextInt();

19
public static int addNewMedia() {

System.out.println("\nAdd new media - Options: ");

System.out.println("--------------------------------");

System.out.println("1. Add a new book");

System.out.println("2. Add a new CD");

System.out.println("3. Add a new DVD");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2-3");

return scanner.nextInt();

public static int viewCart() {

System.out.println("\nView cart - Options: ");

System.out.println("--------------------------------");

System.out.println("1. Filter medias in cart");

System.out.println("2. Sort medias in cart");

System.out.println("3. Remove media from cart");

System.out.println("4. Play a media");

System.out.println("5. Place order");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2-3-4-5");

return scanner.nextInt();

public static int filterCart() {

System.out.println("\nFilter cart - Options: ");

System.out.println("--------------------------------");

20
System.out.println("1. Filter by id");

System.out.println("2. Filter by title");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2");

return scanner.nextInt();

public static int sortCart() {

System.out.println("\nSort cart - Options: ");

System.out.println("--------------------------------");

System.out.println("1. Sort by title");

System.out.println("2. Sort by cost");

System.out.println("0. Back");

System.out.println("--------------------------------");

System.out.println("Please choose a number: 0-1-2");

return scanner.nextInt();

public static int welcome() {

System.out.println("Welcome to AIMS!");

return showMenu();

public void start() {

while (true) {

int choice = ConsoleMenu.showMenu();

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;

private void viewStore() {

store.print();

while (true) {

switch (ConsoleMenu.storeMenu()) {

case 1: {

System.out.println("Enter the title of media you want to search: ");

scanner.nextLine();

Media foundMedia = store.searchMedia(scanner.nextLine());

if (foundMedia == null) {

System.out.println("Media not found!");

22
} else {

foundMedia.printDetail();

switch (ConsoleMenu.mediaDetails()) {

case 1:

if (!cart.addMedia(foundMedia))

System.out.println("The cart is full. Can't add more!");

else

System.out.println("Added to cart successfully!");

break;

case 2:

if (foundMedia instanceof DigitalVideoDisc) {

DigitalVideoDisc foundDisc = (DigitalVideoDisc) foundMedia;

foundDisc.play();

} else {

System.out.println("Media is not DVD. Can't play!");

break;

case 0:

break;

default:

System.out.println("Invalid choice!");

break;

break;

case 2: {

store.print();

System.out.println("Enter the title of media you want to add to store: ");

23
scanner.nextLine();

Media foundMedia = store.searchMedia(scanner.nextLine());

if (foundMedia == null) {

System.out.println("Media not found!");

} else {

if (cart.addMedia(foundMedia))

System.out.println("Added to cart successfully!");

else

System.out.println("The media is already in the store. Can't add!");

break;

case 3: {

System.out.println("Enter the title of media you want to play: ");

scanner.nextLine();

Media foundMedia = store.searchMedia(scanner.nextLine());

if (foundMedia == null) {

System.out.println("Media not found!");

} else if (foundMedia instanceof DigitalVideoDisc) {

DigitalVideoDisc foundDisc = (DigitalVideoDisc) foundMedia;

foundDisc.play();

} else {

System.out.println("Media is not DVD. Can't play!");

break;

case 4:

cart.displayCart();

break;

24
case 0:

return;

default:

System.out.println("Invalid choice!");

break;

private void updateStore() {

while (true) {

switch (ConsoleMenu.updateStore()) {

case 1:

switch (ConsoleMenu.addNewMedia()) {

case 1: {

System.out.println("Enter new book informations: ");

System.out.println("Title: ");

scanner.nextLine();

String title = scanner.nextLine();

System.out.println("Category: ");

String category = scanner.nextLine();

System.out.println("Cost: ");

float cost = scanner.nextFloat();

Book newBook = new Book(title,category,cost);

store.addMedia(newBook);

break;

case 2: {

System.out.println("Enter new CD informations: ");

25
System.out.println("Title: ");

scanner.nextLine();

String title = scanner.nextLine();

System.out.println("Category: ");

String category = scanner.nextLine();

System.out.println("Cost: ");

float cost = scanner.nextFloat();

System.out.println("Director: ");

scanner.nextLine();

String director = scanner.nextLine();

System.out.println("Artist: ");

String artist = scanner.nextLine();

CompactDisc newCD = new CompactDisc(artist,title,category,cost,director);

store.addMedia(newCD);

break;

case 3: {

System.out.println("Enter new DVD informations: ");

System.out.println("Title: ");

scanner.nextLine();

String title = scanner.nextLine();

System.out.println("Category: ");

String category = scanner.nextLine();

System.out.println("Cost: ");

float cost = scanner.nextFloat();

System.out.println("Director: ");

scanner.nextLine();

String director = scanner.nextLine();

System.out.println("Length: ");

26
int length = scanner.nextInt();

DigitalVideoDisc newDVD = new DigitalVideoDisc(title, category, cost, director,length);

store.addMedia(newDVD);

break;

case 0:

break;

default:

System.out.println("Invalid choice!");

break;

break;

case 2:

store.print();

System.out.println("Enter the title of media you want to delete: ");

scanner.nextLine();

Media foundMedia = store.searchMedia(scanner.nextLine());

if (foundMedia == null)

System.out.println("Media not found!");

else {

store.removeMedia(foundMedia);

System.out.println("Deleted successfully!");

break;

case 0:

return;

default:

System.out.println("Invalid choice!");

break;

27
}

private void viewCart() {

cart.displayCart();

while (true) {

switch (ConsoleMenu.viewCart()) {

case 1:

switch (ConsoleMenu.filterCart()) {

case 1: {

System.out.println("Enter the id of media you want to filter: ");

scanner.nextLine();

Media mediaFound = cart.searchCart(scanner.nextInt());

if (mediaFound == null)

System.out.println("Media not found!");

else

System.out.println(mediaFound.toString());

break;

case 2: {

System.out.println("Enter the title of media you want to filter: ");

scanner.nextLine();

Media mediaFound = cart.searchCart(scanner.nextLine());

if (mediaFound == null)

System.out.println("Media not found!");

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: {

System.out.println("Enter the title of media you want to delete: ");

scanner.nextLine();

Media foundMedia = cart.searchCart(scanner.nextLine());

29
if (foundMedia == null)

System.out.println("Media not found!");

else {

cart.removeMedia(foundMedia);

System.out.println("Deleted successfully!");

break;

case 4:

System.out.println("Enter the title of media you want to play: ");

scanner.nextLine();

Media foundMedia = cart.searchCart(scanner.nextLine());

if (foundMedia == null) {

System.out.println("Media not found!");

} else if (foundMedia instanceof DigitalVideoDisc) {

DigitalVideoDisc foundDisc = (DigitalVideoDisc) foundMedia;

foundDisc.play();

} else {

System.out.println("Media is not DVD. Can't play!");

break;

case 5:

System.out.println("Order is created!");

cart = new Cart();

break;

case 0:

return;

default:

System.out.println("Invalid choice!");

30
break;

31

You might also like