1. Trang chủ
  2. » Giáo Dục - Đào Tạo

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

55 42 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 55
Dung lượng 231,06 KB

Nội dung

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 * ALoBook s rest 1 Empty Cons - Book first - ALoBooks rest first 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"; } Course - String name - int credits - ALoStudents students first students ALoStudents rest 1 ConsLoStudents - Student first - ALoStudents rest EmptyLoStudents first Student - int id - String name - ALoCourses courses courses ALoCourses rest EmptyLoCourses ConsLoCourses - Course first - ALoCourse rest public class CourseTest extends TestCase { public void test() { // create courses Course math = new Course("Math", 4); Course physics = new Course("Physics", 5); Course biology = new Course("Biology", 5); Course music = new Course("Music", 2); // create student Jenna Student jenna = new Student(1123, "Jenna"); jenna.addCourse(math); jenna.addCourse(physics); jenna.addCourse(biology); System.out.println(jenna); // create student John Student john = new Student(1345, "John"); john.addCourse(biology); john.addCourse(music); john.addCourse(physics); System.out.println(john); // create student Ernie Student ernie = new Student(4323, "Ernie"); ernie.addCourse(biology); ernie.addCourse(music); System.out.println(ernie); // create student Ernie Student rob = new Student(3213, "Rob"); rob.addCourse(biology); rob.addCourse(physics); System.out.println(rob); } } System.out.println(math); System.out.println(physics); System.out.println(biology); System.out.println(music); public class Course { private String name; private int credits; private ALoStudents students; public Course(String name, int credits) { this.name = name; this.credits = credits; this.students = new EmptyLoStudents(); } public String getName() { return this.name; } } public String toString(){ return this.name+ ", "+this.credits+", cac sinh vien theo hoc:"+ this.students; } public class Student { private int id; private String name; private ALoCourses courses; public Student(int id, String name ) { this.id=id; this.name=name; this.courses = new EmptyLoCourses(); } public String getName(){ return this.name; } } public String toString(){ return this.id+ "-"+ this.name+",cac mon dang ky:"+this.courses.getNames(); } registrar's information Q: How can a student add a course ? // class Student public void addCourse(Course course){ course.addStudent(this); this.courses = new ConsLoCourses(course, this.courses); } Q: How can a course keep track of enrolled student? // class Course public void addStudent(Student student) { this.students = new ConsLoStudents(student, this.students); } class ALoStudents public abstract class ALoStudents { public abstract String getNames(); } class EmptyLoStudents public class EmptyLoStudents extends ALoStudents { public String getNames() { return ""; } } class ConsLoStudents public class ConsLoStudents extends ALoStudents { private Student first; private ALoStudents rest; public ConsLoStudents(Student first, ALoStudents rest) { this.first = first; this.rest = rest; } } public String getNames() { return this.first.getName() + " " + this.rest.getNames(); } class ALoCourses public abstract class ALoCourses { public abstract String getNames(); } class EmptyLoCourses public class EmptyLoCourses extends ALoCourses { public String getNames() { return ""; } } class ConsLoCourses public class ConsLoCourses extends ALoCourses { private Course first; private ALoCourses rest; public ConsLoCourses(Course first, ALoCourses rest) { this.first = first; this.rest = rest; } } public String getNames() { return this.first.getName() + " " + this.rest.getNames(); } Exercises Exercise 2.1.1 • Develop the data definition for classes that represent employees and their project groups Each employee (identified by a name and year of birth) belongs to one or more project groups, and may be a leader of one or more groups Each group has a name, one leader, and a list of employees in the group Exercise 2.1.2 • Develop the data definition for classes that represent the teams in the town youth baseball league League has a list of teams and a list of coaches Each team has a name, a coach, a list of players, and the current record of wins and losses A coach may coach more than one team For each coach we record the name, phone number, and coach's teams For each player we need to know the name, year of birth, and the team Player can be on only one team Relax & …Do Exercises … Too much hard exercises now Try again, never stop practicing! GOOD JOB! ... 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... 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 •... } public String toString() { return this.first.getName() + " " + this.rest; } Discuss • Compare with another implementation in book “ How to design class hierarchies” The University Registrar

Ngày đăng: 29/03/2021, 10:41

TỪ KHÓA LIÊN QUAN