0

I want to assign an ouputstream to the constructor of my Card class in my card game.
The class that instantiates the object should decide which stream to assign the the cardobject to.
It could be the outputstream that goes to the user console
or to a stream that is assigned to a socket.
How to get it correct? I have this:

public class SimplePlayCard {
   private String cardType;
   private int cardValue;
   public OutputStream output;

   public SimplePlayCard(String cardType, int cardValue, OutputStream output){
      this.output = output;
   }  
   public void toDisplay(){
    output.println("This cardtype: " + this.cardType );
   }

}

Then I want to do

 SimplePlayCard a = new SimplePlayCard("spades",1, System.out);

or

 SimplePlayCard a = new SimplePlayCard("spades",1, stream connected to a socket);
Lasse Karagiannis
  • 471
  • 2
  • 6
  • 12
  • 5
    Show us your codes please – Suresh Atta Oct 26 '15 at 08:17
  • 2
    so what is the question? (Besides it sounds like a design flaw to have the card class holding an outputstream) – Emerson Cod Oct 26 '15 at 08:18
  • Agree with @EmersonCod. Why not have a `card.println(outputStream)` and have the caller decide what that stream is ? Or just a `toString` method? – Thilo Oct 26 '15 at 08:26
  • If you have code to show, then please [edit] your question. – Tom Oct 26 '15 at 09:01
  • I wonder why you want to do it like that. For example `toDisplay`. It would be much easier for you if this method would just generate the String which _can_ be displayed somewhere. Like `return "This cardtype: " + this.cardType`. Then you can think about [implementing a proper `toString`](http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) method, so you can do something like this `System.out.println(new SimplePlayCard("spades",1))` to get the desired output. And the calling method handles the used `OutputStream`. – Tom Oct 26 '15 at 09:56
  • My idea is to have a class that does not need modification if I do a server-client application. – Lasse Karagiannis Oct 26 '15 at 10:37
  • And that would be the case if the `SimplePlayCard` wouldn't care about `OutputStream`. You would have a problem with your current version if you don't have an output stream and you try to print the information in a webbrowser using an HTML template. So let this class just create the data string and let the calling method decide what to do with it. – Tom Oct 26 '15 at 10:44
  • Ok, I understand Tom, but I really wanted a "fire an d forget"-structure, don't want the carddeck to care of the that string and channel it to the casinotable that decides what to do with it, just want to give it a stream and the function that instantiates the casinotable decides if it shoud go to strd out or to a object that deals with a socket. – Lasse Karagiannis Oct 26 '15 at 11:38

0 Answers0