Tải bản đầy đủ (.pptx) (55 trang)

Methods with effects (lập TRÌNH cơ bản SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (145.03 KB, 55 trang )

A2 Programming Course

Methods with effects
Week 14,15


Cyclical Data


Bookstore Manager’s Problem


A Variation of the Bookstore Manager’s
Problem
• Develop a program that assists a bookstore
manager. The manager's program should keep a
record for each book
• The record must include information about the
author, the book's title, its price, and its
publication year
• The information about the author includes
author's name, year of birth, and a list of books
written by this author
• Note: Remember to implement toString()


Examples
• The bookstore has the following books written by Jack
London, born 1876:






Call of the Wild (1903), published in 1995, $10
The Sea-Wolf (1904), published in 1999, $12
Martin Eden (1913), published in 1998, $12
White Fang (1906), published in 2001, $10

• The bookstore has the following books written by
Danielle Steel, born in 1955, all in their original edition:






Daddy (1989), $20
Heartbeat (1992), $15
Jewels (1993), $22
Wings (1995), $25
The Ghost (1995) $28


Class Diagram
Author
- String name
- int birthYear
- ALoBooks books
author


books
1..*

<<abstract>>
ALoBook s

rest
1

1

Empty

Cons
- Book first
- ALoBooks rest

first

1

Book
- Author author
- String title
- double price
- int publicationYear


Problem in Designing Constructors
• To create a new object in the class Book we need

to refer to the Author. But each Author refers to a
list of books
• Q: The egg comes first or the hen comes first?
• Solution: A reasonable solution is to define an
Author object with an empty list of books. Then,
as the author writes a new book, we create a Book
object, add it to the bookstore list of books, and
add this book to the author's list of books


public class AuthorTest extends TestCase {
public void test() {
Author jackLondon = new Author("Jack London", 1876);
Book cotw = new Book(jackLondon, "Call of the Wild", 1995, 10);
Book tsw = new Book(jackLondon, "The Sea-Wolf", 1999, 12);
Book me = new Book(jackLondon, "Martin Eden", 1998, 12);
Book wf = new Book(jackLondon, "White Fang", 2001, 10);
System.out.println(jackLondon);

}

}

Author danielleSteel = new Author("Danielle Steel", 1955);
Book d = new Book(danielleSteel, "Daddy", 1989, 20);
Book h = new Book(danielleSteel, "Heartbeat", 1992, 15);
Book j = new Book(danielleSteel, "Jewels", 1993, 22);
Book w = new Book(danielleSteel, "Wings", 1995, 25);
Book tg = new Book(danielleSteel, "The Ghost", 1995, 28);
System.out.println(danielleSteel);



public class Author {
private String name;
private int birthYear;
private ALoBooks books;
public Author(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
this.books = new Empty();
}
String getName() {
return this.name;
}
public String toString() {
return this.name + ", " + this.birthYear + "\n" + this.books;
}
}


public class Book {
private Author author;
private String title;
private double cost;
private int publishYear;
public Book(Author author, String title, int publishYear, double cost) {
this.author = author;
this.title = title;
this.cost = cost;
this.publishYear = publishYear;

this.author.addBook(this);
}

}

public String toString() {
return this.author.getName() + ", " + this.title + ", " +
this.cost + ", " + this.publishYear + "\n";
}


addBook() in Author
// class Author
void addBook(Book book) {
this.books = this.books.add(book);
}


class ALoBooks
public abstract class ALoBooks {
public abstract ALoBooks add(Book book);
}


class Empty
public class Empty extends ALoBooks {
public ALoBooks add(Book book) {
return new Cons (book, new Empty());
}
public String toString() {

return "\n";
}
}


Class Cons
public class Cons extends ALoBooks {
private Book first;
private ALoBooks rest;
public Cons(Book first, ALoBooks rest) {
this.first = first;
this.rest = rest;
}
public ALoBooks add(Book book) {
return new Cons (book, this);
}

}

public String toString() {
return this.first + " " + this.rest;
}


New add(Book book) version
public abstract class ALoBooks {
public ALoBooks add(Book book){
return new Cons(book, this);
}
}



The Transportation Network
Problem


Problem Statement
• The Metropolitan Transit Agency has a web
site that allows the user to display information
about any of its stations and its train routes.
• The user may view the list of all stations along
a given route.
• The user may also find out what are all the
routes that serve a given station


Examples
• The list of stations is: Kenmore, Park, Center,
North, Science, Ashmont, Downtown, Charles,
State, Bowdoin, Maverick, Wonderland
• Green Line runs from Kenmore through Park,
Center, North to Science.
• Red Line runs from Ashmont through
Downtown and Park to Charles.
• Blue Line runs from Bowdoin through Center,
State, Maverick, to Wonderland


Ashmont


Downtown

State

Kenmore

Park

Center

Charles

Bowdoin

Maverick

Wonderland

North

Science


The Problem
• The class Station needs to have fields for its
name, and a list of Routes.
• The class Route needs to have a field for its
name, the origin, the destination, and for a
list of stations.
• Again, we cannot build a route without

knowing all the stations, but the station
should know what routes it serves.


Solution
• To create a representation of this data we first
build all station objects, omitting the route
information.
• Next, for each route, we first build a list of its
stations.
• Then, create the route object, and instruct the
constructor to notify every station that it now
serves this route, as well as the origin and
destination station.


Station
- String name
- ALoRoutes routes
first

1

routes
1

ALoRoutes

rest
1


2
origin, destination
EmptyLoRoutes

ConsLoRoutes
- Route first
- ALoRoutes rest

first

1

Route
- String name
- Station origin
- Station destination
- LoStations stations

stations

ALoStations

1

EmptyLoStations

rest
1


ConsLoStations
- Station first
- ALoStations rest


A Simpler Class Diagram

Station
- String name
- List routes

*

Route
- String name
- Station origin
* - Station destination
- List stations


public class RouteTest extends TestCase {
public void test() {
// create stations for green route
Station kentmore = new Station("Kentmore"); Station park = new Station("Park");
Station center = new Station("Center"); Station north = new Station("North");
Station science = new Station("Science");
// create green route
Route green = new Route("Green", kentmore, science);
green.addStation(park);
green.addStation(center);

green.addStation(north);
System.out.println(green);
// create stations for red route
Station ashmont = new Station("Ashmont");
Station charles = new Station("Charles");
Station downtown = new Station("Downtown");
// create red route
Route red = new Route("Red", ashmont, charles);
red.addStation(downtown);
red.addStation(park);
System.out.println(red);
// create stations for blue route
Station bowdoin = new Station("Bowdoin");
Station maverick = new Station("Maverick");

Station state = new Station("State");
Station wonderland = new Station("Wonderland");

// create blue route
Route blue = new Route("Blue", bowdoin, wonderland);
blue.addStation(center);
blue.addStation(state);
blue.addStation(maverick);
System.out.println(blue);

}

}

// print stations on green route

System.out.println(kentmore);
System.out.println(center);
System.out.println(science);

System.out.println(park);
System.out.println(north);

// print stations on red route
System.out.println(ashmont);
System.out.println(charles);

System.out.println(downtown);

// print stations on blue route
System.out.println(bowdoin);
System.out.println(maverick);

System.out.println(state);
System.out.println(wonderland);


public class Station {
private String name;
private ALoRoutes routes;
public Station(String name) {
this.name = name;
this.routes = new EmptyLoRoutes();
}
public String getName() {
return this.name;

}
public String toString() {
return "Station " + this.name + ": " + this.routes.getName();
}
}


×