3 Replies - 322 Views - Last Post: 05 May 2013 - 03:35 AM Rate Topic: -----

#1 dzeko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-May 13

Problem with setting Enum to Object

Posted 05 May 2013 - 02:27 AM

I have problem when I try set Enum to user created Object. No matter what I input in namen (as long as it is value defined in enum Namen) I get default TEK. I want to get NOGOMET if I input NOGOMET in namen.


Namen.java - enums
public enum Namen {
	TEK {
		public String toString() {
			return "tek";
		}
	},
	NOGOMET {
		public String toString() {
			return "nogomet";
		}
	},	
	KOSARKA {
		public String toString() {
			return "kosarko";
		}
	},
	SMUCANJE {
		public String toString() {
			return "smucanje";
		}
	},
	TENIS {
		public String toString() {
			return "tenis";
		}
	}
}



SportniCevelj.java - I create new Object (type SportniCevelj) through novSportniCevelj() using Namen namen = Namen.valueOf(vhod.readLine());
import java.io.*;
import java.util.Scanner;

public class SportniCevelj extends Cevelj {

	private Namen namen;
	
	public SportniCevelj() {
		super();
		this.namen = Namen.TEK;
	}
	
	public SportniCevelj(String z, String m, double v, String b, double c, int p, int za, Spol s, Namen n) {
		super(z,m,v,b,c,p,za,s);
		this.namen = n;
	}
	
	public void nastaviNamen(Namen n) {
		namen = n;
	}
	
	public Namen vrniNamen() {
		return namen;
	}
	
	public String toString() {
		return "Sportni cevelj " + znamka + " " + model + " je velikost " + velikost + " stane " + cena + " EUR in je namenjen za " + vrniNamen() + ".";
	}
	
	public void novSportniCevelj() throws Exception {
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("");
		System.out.println("Vnasanje sportnega cevlja!");
		System.out.println("Vnesi podatke sportnega cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Namen: ");
		Namen namen = Namen.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with setting Enum to Object

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with setting Enum to Object

Posted 05 May 2013 - 03:03 AM

Here's an example (modified directly from the Java Tutorials) that shows how to do what I think you're trying to do. If not, please describe the problem better.
public class TestNamen
{
    Namen namen;
    
    // constructor
    public TestNamen( Namen namen )
    {
        this.namen = namen;
    }
    
    // prints the designated Namen
    public void printNamen()
    {
        System.out.println( namen );
    }
    
    public static void main( String[] args )
    {
        TestNamen tekNamen1 = new TestNamen( Namen.TEK );
        tekNamen1.printNamen();
        TestNamen tekNamen2 = new TestNamen( Namen.NOGOMET );
        tekNamen2.printNamen();
        TestNamen tekNamen3 = new TestNamen( Namen.KOSARKA );
        tekNamen3.printNamen();
        TestNamen tekNamen4 = new TestNamen( Namen.SMUCANJE );
        tekNamen4.printNamen();
        TestNamen tekNamen5 = new TestNamen( Namen.TENIS );
        tekNamen5.printNamen();
        
    } // end method main()
    
} // end class TestNamen

Was This Post Helpful? 0
  • +
  • -

#3 dzeko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-May 13

Re: Problem with setting Enum to Object

Posted 05 May 2013 - 03:23 AM

In my MainProgram.java (my main is here) I create new object SportniCevelj.

	public static void main(String[] args) throws Exception {
		SportniCevelj sc = newSportniCevelj();
		sc.novSportniCevelj();
	}


That gets me to method in SportniCevelj.java, which is basically method for user input.
	public void novSportniCevelj() throws Exception {
		BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("");
		System.out.println("Vnasanje sportnega cevlja!");
		System.out.println("Vnesi podatke sportnega cevlja!");
		System.out.print("Znamka: ");
		znamka = vhod.readLine();
		System.out.print("Model: ");
		model = vhod.readLine();
		System.out.print("Velikost: ");
		velikost = Double.parseDouble(vhod.readLine());
		System.out.print("Barva: ");
		barva = vhod.readLine();
		System.out.print("Spol: ");
		Spol spol = Spol.valueOf(vhod.readLine());
		System.out.print("Namen: ");
		Namen namen = Namen.valueOf(vhod.readLine());
		System.out.print("Cena: ");
		cena = Double.parseDouble(vhod.readLine());
		System.out.print("Popust: ");
		popust = Integer.parseInt(vhod.readLine());
		System.out.print("Zaloga: ");
		zaloga = Integer.parseInt(vhod.readLine());
		System.out.println("Vnos uspesen!");
	}


Now the problem appears when I write let's say NOGOMET when I have to input Namen (enum). But when I try to print created SportniCevelj it says Namen is TEK which is what default Namen would be when I'd create SportniCevelj without parameters.

	public SportniCevelj() {
		super();
		this.namen = Namen.TEK;
	}



I'd like to write in Namen (when it asks me to do it) KOSARKA or NOGOMET or TENIS or SMUCANJE and then when I'd print it, it would be one of those values and not always TEK.


How do I do that?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with setting Enum to Object

Posted 05 May 2013 - 03:35 AM

Write another constructor that takes a parameter that could be a String or Namen object, and then assign the Namen object based on the constructor's parameter. Something like:
// constructor that takes a parameter that specifies the Namen
// object to be created
public SportniCevelj( String namen )
{
    super();
    
    // a decision tree, could be if/else or a switch
    if ( namen.equals( "NOGOMET" ) )
    {
        this.namen = Namen.NOGOMET;
    }
    else if ()
    {
        // etc. . . . 
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1