test. a software house decides to develop a dvd renting program. the product manager identifies the...

18
Test

Upload: alberta-sherman

Post on 24-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Test

Page 2: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

A software house decides to develop a DVD renting program. The product manager identifies the following requirements:Every DVD will have a title, one or more actors, a production studio, a SIN (Standard Identification Number), and a rental charge (see below).Initially, the system will deal with three categories of DVD: Children, New Release and Regular.The rental charge for each is different:

Children: These DVDs cost £1.50 for the first three days of renting and then £1.50 for each further day.New Release: These DVDs cost £3 for every rental day.Regular: These DVDs cost £2 for the first two days of renting and then £1.50 for each further day.

The program must handle new categories of DVD in the future with minimal change to existing code. The chief designer of the DVD renting program asks you to design the classes that will represent the DVDs based on the above requirements. Finally, the chief designer indicates that it will be necessary to serialize the objects in the system so that they can be sent across the network or stored in a file. The chief designer has also provided you with two interfaces

HAS-A

IS-A

NO NEED FOR AN APPLICATION – BUT NOT WRONG

YOU HAVE TO USE THESE

Page 3: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

public interface Price extends Serializable{ public double getCharge(int daysRented);}

public interface Dvd extends Price{public String getTitle();public void setTitle(String title);public String getSin();public void setSin(String sin);public Actor[] getActors();public void setActors(Actor[] actors);public String getStudio();public void setStudio(String studio);

}

Page 4: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Remember!

• Interfaces cannot HAVE data• Minimise duplication – the kinds of Dvd must

inherit from something• You may override methods at a lower level

Page 5: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Some attempts might include

Having all three types of Dvd implement the interface – but then duplication

Page 6: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

DON’T FORGET THE ACTORS!

getCharge() is different for the 3 kinds of Movies – rest of methods and all attributes are same

Page 7: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

• Dvd and Price interfaces were included since they were specified. • By making Price extend Serializable, we know that most of our

classes will implement the interface. • Actor is also made to implement Serializable. • The Movie class has been made abstract and implements the Dvd

interface. It is abstract so that common attributes and code can be defined in one place. All the setter methods are common to all movies. A Movie has many Actors.

• It is only the getCharge method that changes, and indeed could itself be defined as abstract in the Movie class.

• The three concrete subclasses override getCharge() and the right one will be called at runtime, depending on the kind of Movie.

• It will be very easy to plug in further kinds of movie in the future by simply extending Movie.

• Note that getCharge method should really return a BigDecimal rather than than a double. It is more precise since double arithmetic will result in loss of precision, which is critical in this application.

Page 8: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Test some things – USE methodspublic class DvdTester {

public static void main(String [] args){Dvd nr1 = new NewRelease("No Place For Old Men");Dvd r1 = new Regular ("Men In Black");Dvd c1 = new Childrens ("Shrek");System.out.println(nr1.getTitle() + ‘ ‘ + nr1.getCharge(2));System.out.println(r1.getTitle() + ‘ ‘ + r1.getCharge(3));System.out.println(c1.getTitle() + ‘ ‘ + c1.getCharge(4));

}}

Page 9: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Code that IMPLEMENTS the getCharge method or methods

public abstract Class Movie implements Dvd { ... public abstract double getCharge(int days); ...}

public class NewRelease extends Dvd { ... @Override public double getCharge(int days) { return days * 3; } ...}

public class Childrens extends Movie { ... @Override public double getCharge(int days) { if (days > 3){ return (days - 3) * 1.5 + 1.5; } else { return 1.5; } }public class Regular extends Dvd { ... @Override public double getCharge(int days) { if (daysRented > 2){ return (days - 2) * 1.5 + 2; }else{ return 2; } }

Page 10: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

An object representing a particular category of DVD can have its category changed at run time, without the need to create a new object, for example, a New Release DVD

becomes a Children’s DVD after so many days

Note that now the Movie has an attribute of Price – which can be any of the particular implementing prices

Page 11: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

What next

• Tomorrow – I will talk about assignment for people who are really stuck

• We may have no 124 lectures on Friday??• There will be an exam review lecture in May• We plan to issue the problem for PART OF the

exam towards the end of this month so you will have a little while to think about it

Page 12: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Assignment

Page 13: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Have you started the assignment?2 Views of the same Model class

Page 14: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

How to start

• Implement the interface (add a few more methods – like how are you getting the thing to guess into the Model?)

• Write a driver and an application with a simple menu to run its methods – this is essentially the text based game

• Also write JUnit tests• Then look at GUI – Life and Bank in Topic00 examples

are a good place to start• Run system tests

Page 15: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

So, you need to write a Model class that allows you to put in a String – that will be the hidden thing, e.g. “hello” maybe done with constructor?Also probably stores:•the visible thing - to start with that would be “*****”•And the number of guesses left – 10 to start•And (probably as a String?) the letters they have guessed so far – to start with that might be “”

Page 16: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Text based Application – must have a link to the Model and the Model must have a word in it

runMenu() is something likewhile (model.guessLeft()>0) {

S.o.p(“guess word or letter”);read answer if answer is letter

read letterif (model.tryThis(letter)) break; // you have a winnerelse S.o.p(“no sorry – try again”)

else if answer is word read wordif (model.tryWord(word)) break; // you have a winnerelse S.o.p(“no sorry – try again”)

}if (model.guessLeft()==0) //say something nastyelse //say something nice

Page 17: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Something that extends JFrame

PicturePanel a JPanel with a picture in it

TopPanel - another JPanel with Buttons and Labels and TextFields in it

In background a Listener has to listen to the button presses and

then in an actionPerformed() method modify the Model and the

PicturePanel(the JFrame or the TopPanel could

be own Listeners – or have separate one)

the Model

Page 18: Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,

Keep it simple and build a bit at a time

• Use ‘stubs’ methods that just print ‘now trying letter X’ for instance

• I’d probably first always use a Model with the same word in it – when all methods work add reading from a file and picking a random one

• Same with GUI you can print ‘now clicked letter button’ or pop up a MessageDialog like in the Bank

• Leave enough time to make a jar file & document