Reputation:
I am taking a programming course at the local college. My instructor doesn't speak the best English, so sometimes it's hard to understand what he's asking for.
The first part of the assignment, which I have already completed, is as follows: A mechanic shop has 100 customers, each of which is an object in an array, which holds various data. The Base class is Service, and there are four derived classes for specific services (tires, oil, radiator, and brakes). Each customer is assigned one service. A list of all customers with the details is printed.
The second part, I do not understand what he is asking for at all: We are printing coupons if the last service was done over a certain number of months ago. I have already made calculations for the dates, and if the service was done a certain amount of time ago, I need to pass one object in the service array to the abstract coupon class. His exact instructions are these:
Coupon is an Abstract parent class. The abstract class should have variable year, month and day. Make a Service Coupon extend the Abstract coupon class. Several child classes - BrakeService Coupon, Tire Coupon, Engine Oil Coupon, Radiator Coupon extend the Service Coupons. Add an Expiry date to a random date in 2013 and Make it final in the constructor of the objects. The service object calls the coupon passing the service object. The coupon determines the type of service object it receives and accordingly prints the message. This should be simple.
Here is what I think it is asking for (purely as an exercise for demonstrating we know how to use abstract and inheritance):
http://www.java-forums.org/attachments/new-java/4632-passing-object-abstract-class-rtc3ild.png
Am I supposed to be passing the object to the abstract class? If so, am I supposed to take my main class and make it an extension of the abstract coupon class?
What I have now is the abstract Coupon class extending the concrete Service class, with the ServiceCoupon class extending Coupon, and four concrete classes extending that. I am not sure how to call the Coupon class or pass it the object from its base class though.
Sorry that I can't be more clear. I don't know what he wants either.
edit: **The main problem I am having is that I want to pass an object to a different class that is abstract. I cannot instantiate this class because it is abstract, so I do not know how to pass an object to the method.
Upvotes: 2
Views: 1577
Reputation: 13066
I Guess the second part of question is asking for something like this:
import java.util.*;
abstract class Coupon
{
int year;
int month;
int day;
}
class Service extends Coupon
{
public String getCouponMessage(Coupon coupon)
{
if (coupon instanceof BrakeService)
{
return "BreakService message";
}
else if (coupon instanceof Tire)
{
return "TireService message";
}
else if (coupon instanceof Engine_Oil)
{
return "Engin_Oil Message";
}
else if (coupon instanceof Radiator)
{
return "Radiator Message";
}
else
{
return "Invalid Service";
}
}
}
class BrakeService extends Service
{
final Date exp_date;
public BrakeService(int day,int month , int year)
{
Calendar cal = Calendar.getInstance();
this.day = day;
this.month=month;
this.year=year;
cal.set(year, month, day);
exp_date = cal.getTime();
}
}
class Tire extends Service
{
final Date exp_date;
public Tire(int day,int month , int year)
{
Calendar cal = Calendar.getInstance();
this.day = day;
this.month=month;
this.year=year;
cal.set(year, month, day);
exp_date = cal.getTime();
}
}
class Engine_Oil extends Service
{
final Date exp_date;
public Engine_Oil(int day,int month , int year)
{
Calendar cal = Calendar.getInstance();
this.day = day;
this.month=month;
this.year=year;
cal.set(year, month, day);
exp_date = cal.getTime();
System.out.println(exp_date);
}
}
class Radiator extends Service
{
final Date exp_date;
public Radiator(int day,int month , int year)
{
Calendar cal = Calendar.getInstance();
this.day = day;
this.month=month;
this.year=year;
cal.set(year, month, day);
exp_date = cal.getTime();
}
}
Upvotes: 0
Reputation: 355
Coupon should not extend the Service class. You should have two inheritance trees, one for coupon and one for services.
Upvotes: 0
Reputation: 61148
I think what he wants is a factory pattern, i.e. you pass in your Service
and the factory determines the type of Service
and then returns the appropriate Coupon
implementation. Something based on a Map
would probably do the trick
final Map<Class<? extends Service>, Class<? extends Coupon>> myMap;
public Coupon getCoupon(final Service service) {
final Coupon coupon = myMap.get(service.getClass()).newInstance();
//init stuff
return coupon;
}
Obviously this would only work if you had a noargs constructor for your Coupon
.
Upvotes: 1