dimanche 31 juillet 2016

Java- if and else issue

while i'm working on my first desktop application and finished design the scene for "adding" feature which allows the user to adding new words and its meaning and other stuff like description and more the point here is all of this is a text fields and return Strings so if a user want to add a word without adding description so the description area will return (null), so is i wanted to display the word on the other scene after saving it ill need to make a type for every word like this

if(addobj.audio == null && addobj.image == null && addobj.des != null)
                {addobj.type = "a"; addobj.createFile(); addobj.aadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.audio == null && addobj.des == null && addobj.image == null)
                {addobj.type = "b"; addobj.createFile(); addobj.badd(); addobj.close(); stage.setScene(msc);}

                if(addobj.image == null && addobj.audio != null && addobj.des != null)
                {addobj.type = "c"; addobj.createFile(); addobj.cadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.audio == null && addobj.des != null && addobj.image != null)
                {addobj.type = "d"; addobj.createFile(); addobj.dadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.des == null && addobj.audio != null && addobj.image != null)
                {addobj.type = "e"; addobj.createFile(); addobj.eadd(); addobj.close(); stage.setScene(msc);}

and as you can see the last if is commented and that because every word i have insert was carrying the type "s" that should appear with only the word that have all the features so i commented it to see the result of the others and once i clicked save i get no response so here is the hole code main class

package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

    Add addobj = new Add();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        //Components

        //Layouts
        VBox settly, openly, addly, mly = new VBox();
        settly = new VBox();
        openly = new VBox();
        addly = new VBox();

        //Scenes
        Scene settsc,addsc, vsc, msc = new Scene(mly, 600, 400);
        settsc = new Scene(settly, 600, 400);
        addsc = new Scene(addly, 600, 400);
        vsc = new Scene(openly, 600, 400);

        //labels
        Label well = new Label();

        //Buttons
        Button settingsb, addb, openb = new Button("Open the vocabulary");
        settingsb = new Button("Settings");
        addb = new Button("Add new word");

        //Buttons Settings
        addb.setOnAction(e -> stage.setScene(addsc));
        settingsb.setOnAction(e -> stage.setScene(settsc));
        openb.setOnAction(e -> stage.setScene(vsc));



        //Label Settings
        well.setText("Vocabulary Builder");
        //Layouts Settings
        mly.getChildren().addAll(well, openb, addb, settingsb);
        mly.setPadding(new Insets(20, 20, 20, 20));

        //Adding word interface
        TextField tfa, tfi, tfw, tfm = new TextField();
        tfw = new TextField();
        tfa = new TextField();
        tfi = new TextField();
        tfw.setPromptText("Type The New Word Here");
        tfm.setPromptText("Type The Meaning Here");
        tfa.setPromptText("Paste the link of the audio file");
        tfi.setPromptText("Paste the link of the image");

        TextArea tad= new TextArea();
        tad.setPromptText("Type The Description Here");

        Button sbtn = new Button("Save");
        Button cbtn = new Button("Cancel");

            //Saving And Canceling presses
            sbtn.setOnAction(e -> {
                addobj.word = tfw.getText();
                addobj.mean = tfm.getText();
                addobj.des = tad.getText();
                addobj.audio = tfa.getText();
                addobj.image = tfi.getText();

                if (addobj.word == null) 
                    tfw.setText("Please add a word");

                if (addobj.mean == null) 
                    tfm.setText("Please add the meaning");

                if(addobj.audio == null && addobj.image == null && addobj.des != null)
                {addobj.type = "a"; addobj.createFile(); addobj.aadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.audio == null && addobj.des == null && addobj.image == null)
                {addobj.type = "b"; addobj.createFile(); addobj.badd(); addobj.close(); stage.setScene(msc);}

                if(addobj.image == null && addobj.audio != null && addobj.des != null)
                {addobj.type = "c"; addobj.createFile(); addobj.cadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.audio == null && addobj.des != null && addobj.image != null)
                {addobj.type = "d"; addobj.createFile(); addobj.dadd(); addobj.close(); stage.setScene(msc);}

                if(addobj.des == null && addobj.audio != null && addobj.image != null)
                {addobj.type = "e"; addobj.createFile(); addobj.eadd(); addobj.close(); stage.setScene(msc);}

                //if(!(addobj.audio == null && addobj.des == null && addobj.image == null))
                //{addobj.type = "s"; addobj.createFile(); addobj.sadd(); addobj.close(); stage.setScene(msc);}

            });

            cbtn.setOnAction(e -> stage.setScene(msc));


        addly.getChildren().addAll(tfw, tfm, tad, tfa, tfi, cbtn, sbtn);

        //Adding Components

        //finishing
        stage.setTitle("Vocabulary Builder");
        stage.setScene(msc);
        stage.show();
    }
}

the Add class

package application;

import java.util.*;
import java.io.*;
import java.lang.*;

public class Add {

    private Formatter x;

    //Strings
    String type,word,mean,des,audio,image;


    public void createFile(){
    try
    {
        x = new Formatter("Vocabulary.txt");
    }
    catch(Exception e)
    {
        System.out.println("let this apper in a box later");
    }
    }

    public void aadd()
    {
        x.format("%s %s %s ", type, word, mean, des);
    }

    public void badd()
    {
        x.format("%s %s", type, word, mean);
    }

    public void cadd()
    {
        x.format("%s %s %s %s", type, word, mean, des, audio);
    }

    public void sadd()
    {
        x.format("%s %s %s %s %s", type, word, mean, des, audio, image);
    }

    public void dadd()
    {
        x.format("%s %s %s %s", type, word, mean, des, image);
    }

    public void eadd()
    {
        x.format("%s %s %s %s", type, word, mean, audio, image);
    }

    public void close()
    {
        x.close();
    }


    /*
     * a include word, mean and description
     * b include word and mean 
     * c include word, mean, description and audio 
     * s include word, mean, description, audio and image
     * d include word, mean, description and image
     * e include word, mean, audio and image
     */

PS i'm using JavaFX for the interface

Aucun commentaire:

Enregistrer un commentaire