package Modèle;
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : FonctionObjectif.java
//  @ Date : 25/04/2011
//  @ Author : 
//
//

import java.util.*;

public class FonctionObjectif {
	protected String objectif; // valeur de objectif : maximiser ou minimiser
	protected Vector<Integer> coeffs;
	protected Vector<Variable> variables;
	protected String nom;

	public FonctionObjectif(String objectif, String fonction) {
		this.objectif = objectif;
		coeffs = new Vector<Integer>();
		variables = new Vector<Variable>();
		while (!parser(fonction)) {
			System.out.println(fonction + " : Erreur");
			System.out.println("La fonction objectif n'est pas valide. Elle doit être de la forme Z=3x1-5x2 ou Z=3*x1-5*x2");
			System.out.println("Donnez la nouvelle fonction objectif");
			Scanner sc = new Scanner(System.in);
			fonction = sc.nextLine();
		}
	}

	public boolean parser(String fonction) {
		try {
			if (!fonction.contains("="))
				return false;
			nom = fonction.substring(0, fonction.indexOf("="));
			if (nom.equals(""))
				return false;
			String f = fonction.substring(fonction.indexOf("=") + 1);
			if (f.equals(""))
				return false;
			Vector<String> elements = new Vector<String>();
			String[] tabPlus;
			String[] tabMoins = f.split("-");
			for (int i = 1; i < tabMoins.length; i++)
				tabMoins[i] = "-" + tabMoins[i];
			for (int i = 0; i < tabMoins.length; i++)
				if (!tabMoins[i].equals("")) {
					if (tabMoins[i].contains("+")) {
						tabPlus = tabMoins[i].split("\\+");
						for (int j = 0; j < tabPlus.length; j++)
							elements.add(tabPlus[j]);
					}
					else
						elements.add(tabMoins[i]);
				}
			String[] tab;
			for (int i = 0; i < elements.size(); i++) {
				if (elements.get(i).contains("*")) { // fonction de la forme
					// 3*x1-5*x2
					tab = elements.get(i).split("\\*");
					if (tab.length == 1) {
						coeffs.add(1);
						variables.add(new Variable(tab[0], null));
					}
					else {
						coeffs.add(Integer.parseInt(tab[0]));
						variables.add(new Variable(tab[1], null));
					}
				}
				else {// fonction de la forme 3x1-5x2
					if (elements.get(i).charAt(0) == '-') {
						if (Character.isLetter(elements.get(i).charAt(1))) {
							coeffs.add(-1);
							variables.add(new Variable(elements.get(i).substring(1, elements.get(i).length()), null));
						}
						else {
							int indexVar = 1;
							while (indexVar < elements.get(i).length() && Character.isDigit(elements.get(i).charAt(indexVar)))
								indexVar++;
							if (indexVar == elements.get(i).length()) {
								coeffs.add(Integer.parseInt(elements.get(i)));
								variables.add(new Variable("", null));
							}
							else {
								coeffs.add(Integer.parseInt(elements.get(i).substring(0, indexVar)));
								variables.add(new Variable(elements.get(i).substring(indexVar, elements.get(i).length()), null));
							}
						}
					}
					else {
						if (Character.isLetter(elements.get(i).charAt(0))) {
							coeffs.add(1);
							variables.add(new Variable(elements.get(i), null));
						}
						else {
							int indexVar = 0;
							while (indexVar < elements.get(i).length() && Character.isDigit(elements.get(i).charAt(indexVar)))
								indexVar++;
							if (indexVar == elements.get(i).length()) {
								coeffs.add(Integer.parseInt(elements.get(i)));
								variables.add(new Variable("", null));
							}
							else {
								coeffs.add(Integer.parseInt(elements.get(i).substring(0, indexVar)));
								variables.add(new Variable(elements.get(i).substring(indexVar, elements.get(i).length()), null));
							}
						}
					}
				}
			}
			return true;
		} catch (Exception e) {
			System.out.println(e);
			return false;
		}
	}

	public String getObjectif() {
		return objectif;
	}

	public String getFonction() {
		String s = nom + "=";
		for (int i = 0; i < variables.size(); i++) {
			if (coeffs.get(i) < 0)
				s += coeffs.get(i) + variables.get(i).getNom();
			else {
				if (i != 0)
					s += "+" + coeffs.get(i) + variables.get(i).getNom();
				else
					s += coeffs.get(i) + variables.get(i).getNom();
			}
		}
		return s;
	}

	public void setObjectif(String objectif) {
		this.objectif = objectif;
	}
	public Vector<Integer> getCoeffs(){
		return coeffs;
	}
	public Vector<Variable> getVariables(){
		return variables;
	}
	public String getNom(){
		return nom;
	}
}
