Note that there are some explanatory texts on larger screens.

plurals
  1. POusing Java interfaces
    primarykey
    data
    text
    <p>I need to create interface MultiLingual, that allows to display object's data in different languages (not data itself, but introduction like "Author", "Title" etc.). </p> <p>Printed data looks like this :</p> <pre><code>3 grudnia 1998 10th of June 1924 Autor: Tolkien Tytul: LoTR Wydawnictwo: Amazon 2010 Author: Mitch Albom Title: Tuesdays with Morrie Publishing House: Time Warner Books 2003 37 360,45 PLN 5,850.70 GBP 3rd of December 1998 10th of June 1924 Author: Tolkien Title: LoTR Publishing House: Amazon 2010 Author: Mitch Albom Title: Tuesdays with Morrie Publishing House: Time Warner Books 2003 37,360.45 GBP 5,850.70 GBP </code></pre> <p>Test code looks like this :</p> <pre><code>public class Main { public static void main(String[] args){ MultiLingual gatecrasher[]={ new Data(3,12,1998), new Data(10,6,1924,MultiLingual.ENG), new Book("LoTR", "Tolkien", "Amazon", 2010), new Book("Tuesdays with Morrie", "Mitch Albom", "Time Warner Books",2003, MultiLingual.ENG), new Money(1232895/33.0,MultiLingual.PL), new Money(134566/23.0,MultiLingual.ENG), }; for(int i=0;i &lt; gatecrasher.length;i++) System.out.println(gatecrasher[i]+"\n"); for(int i=0;i &lt; gatecrasher.length;i++) System.out.println(gatecrasher[i].get(MultiLingual.ENG)+"\n"); } } </code></pre> <p>So i need to introduce constants ENG, PL in MultiLingual interface, as well as method get(int language) :</p> <pre><code>public interface MultiLingual { int ENG = 0; int PL= 1; String get(int lang); } </code></pre> <p>And then I have class Book. Problem starts with the constructors. One of them needs to take MultiLingual.ENG as argument, but how to achieve that ? Is this the proper way? :</p> <pre><code>class Book implements MultiLingual { private String title; private String publisher; private String author; public Book(String t, String a, String p, int y, MultiLingual lang){ } </code></pre> <p>Or should I treat this MultiLingual.ENG as int variable , that will just change automatically constants in interface?</p> <p>Second constructor for book doesn't take MultLingual as argument, but following implementation is somehow wrong :</p> <pre><code>public Book(String t, String a, String p, int y){ Book someBook = new Book(t, a, p, y, MultiLingual m); } </code></pre> <p>I could just send int m in place of MultiLingual m but then I will have no control if language is set to PL or ENG.</p> <p>And finally get() method for Boook but I think at least this should be working fine:</p> <pre><code>public String get(int lang){ String data; if (lang == ENG){ data = "Author: "+this.author+"\n"+ "Title: "+this.title+"\n"+ "Publisher: "+this.publisher+"\n"; } else { data = "Autor: "+this.author+"\n"+ "Tytul: "+this.title+"\n"+ "Wydawca: "+this.publisher+"\n"; } return data; } @Override public String toString(){ return ""; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload