Assignment 1

 InterestTableGUI.java

packageinteresttable;

importjavafx.application.Application;

importjavafx.event.ActionEvent;

importjavafx.event.EventHandler;

importjavafx.scene.Scene;

importjavafx.scene.control.Button;

importjavafx.scene.control.TextArea;

importjavafx.scene.control.TextField;

importjavafx.scene.layout.StackPane;

importjavafx.stage.Stage;

public class InterestTableGUI {

//static Stage classStage = new Stage();

public static void main(String[] args) {

MyController c = new MyController();

Stage classStage = new Stage();

new Thread() {

@Override

public void run() {

javafx.application.Application.launch(MyController.class);

}

}.start();

try {

c.start(classStage);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} 

Model.java

packageinteresttable;

importjava.text.NumberFormat;

public class Model {

double principal;

double rate;

int year;

public void setPrincipal(double principal) {

this.principal = principal;

}

public void setRate(double rate) {

this.rate = rate;

}

public void setYear(int year) {

this.year = year;

}

public double getPrincipal() {

return principal;

}

public double getRate() {

return rate;

}

publicintgetYear() {

return year;

}

public String simpleIntrest(double principal,doublerate,int year){

String resutl = “”;

int i=1;

for (i=1;i<=year;i++){

doubleinterest_amount;

doublecal_rate;

cal_rate = (principal*(rate/100)*i);

interest_amount = cal_rate + principal;

String formattedValue = NumberFormat.getCurrencyInstance().format(interest_amount);

resutl = resutl+”simpleIntrest of year” +i+”  “+formattedValue+”\n”;

}

returnresutl;

}

public String computeIntrest(double principal,intyear,double rate){

String resutl = “”;

for(int i=1;i<=year;i++) {

doublecal_rate = principal*Math.pow(1+(rate/100),i);

String formattedValue = NumberFormat.getCurrencyInstance()

.format(cal_rate);

resutl = resutl+”ComputedIbtrestos year “+i+”  “+formattedValue+”\n”;

}

returnresutl;

}

public String both(double principal,doublerate,int year){

String result = “”;

result = result+computeIntrest(principal, year, rate)+”\n”;

result = result+simpleIntrest(principal, rate, year);

return result;

}

} 

MyController.java

packageinteresttable;

importjavafx.application.Application;

importjavafx.event.ActionEvent;

importjavafx.event.EventHandler;

importjavafx.stage.Stage;

public class MyController extends Application{

Model model;

View view;

public void setContoll(){

Demo d = new Demo();

view.getSimpleIntrestbtn().setOnAction(d);

 

view.compountIntrestbtn.setOnAction((ActionEvent event) -> {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

view.getTextarea().setText( model.computeIntrest(principal,year,rate));

});

view.bothIntrestbtn.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent event) {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

model.computeIntrest(principal,year,rate);

view.getTextarea().setText( model.both(principal, rate, year));

}

});

}

@Override

public void start(Stage primaryStage) throws Exception {

this.model = new Model();

this.view = new View(primaryStage);

setContoll();

}

class Demo implements EventHandler<ActionEvent>{

@Override

public void handle(ActionEvent event) {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

view.getTextarea().setText( model.simpleIntrest(principal, rate,year));

}

}

} 

View.java

packageinteresttable;

importjava.util.ArrayList;

importjava.util.logging.Level;

importjava.util.logging.Logger;

importjavafx.application.Application;

importjavafx.event.ActionEvent;

importjavafx.event.EventHandler;

importjavafx.scene.control.Button;

importjavafx.scene.control.ComboBox;

importjavafx.scene.control.Label;

importjavafx.scene.control.TextArea;

importjavafx.scene.control.TextField;

importjavafx.scene.layout.AnchorPane;

importjavafx.scene.layout.StackPane;

importjavafx.stage.Stage;

// import sun.plugin.javascript.navig.Anchor;

public class View extends AnchorPane{

Button simpleIntrestbtn;

Button bothIntrestbtn;

Button compountIntrestbtn;

TextAreatextarea;

ComboBox comb;

TextFieldprinciplTextfield;

TextFieldprinciplPercentTextfield;

public View(Stage s){

try {

start(s);

} catch (Exception ex) {

Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);

}

}

publicTextAreagetTextarea() {

returntextarea;

}

public void setTextarea(TextAreatextarea) {

this.textarea = textarea;

}

publicComboBoxgetComb() {

return comb;

}

public void setComb(ComboBox comb) {

this.comb = comb;

}

publicTextFieldgetPrinciplTextfield() {

returnprinciplTextfield;

}

public void setPrinciplTextfield(TextFieldprinciplTextfield) {

this.principlTextfield = principlTextfield;

}

publicTextFieldgetPrinciplPercentTextfield() {

returnprinciplPercentTextfield;

}

public void setPrinciplPercentTextfield(TextFieldprinciplPercentTextfield) {

this.principlPercentTextfield = principlPercentTextfield;

}

public Button getBothIntrestbtn() {

returnbothIntrestbtn;

}

public void setBothIntrestbtn(Button bothIntrestbtn) {

this.bothIntrestbtn = bothIntrestbtn;

}

public Button getCompountIntrestbtn() {

returncompountIntrestbtn;

}

public void setCompountIntrestbtn(Button compountIntrestbtn) {

this.compountIntrestbtn = compountIntrestbtn;

}

public Button getSimpleIntrestbtn() {

returnsimpleIntrestbtn;

}

public void setSimpleIntrestbtn(Button simpleIntrestbtn) {

this.simpleIntrestbtn = simpleIntrestbtn;

}

public void start(Stage primaryStage) throws Exception {

Model m = new Model();

simpleIntrestbtn = new Button();

comb = new ComboBox();

comb.setTranslateX(433.0);

comb.setTranslateY(284.0);

ArrayList<String> years = new ArrayList<String>();

for(int i=1;i<26;i++)

years.add(“”+i);

comb.getItems().addAll(years);

comb.setValue(“1”);

textarea = new TextArea();

textarea.setTranslateX(55.0);

textarea.setTranslateY(20.0);

textarea.setPrefHeight(200.0);

textarea.setPrefWidth(335.0);

principlTextfield= new TextField();

principlTextfield.setTranslateX(42.0);

principlTextfield.setTranslateY(284.0);

principlPercentTextfield= new TextField();

principlPercentTextfield.setTranslateX(242.0);

principlPercentTextfield.setTranslateY(284.0);

//        String ratevalue=principlTextfield.getText();

//        Double rate= Double.parseDouble(ratevalue);

//        m.setPrincipal(rate);

Label principlLabel= new Label();

principlLabel.setText(“Principal :”);

principlLabel.setTranslateX(60.0);

principlLabel.setTranslateY(243.0);

Label  rateLabel= new Label();

rateLabel.setText(“Rate(Persentage):”);

rateLabel.setTranslateX(268.0);

rateLabel.setTranslateY(243.0);

Label  noOfYrearsLabel= new Label();

noOfYrearsLabel.setText(“No Of Years :”);

noOfYrearsLabel.setTranslateX(463.0);

noOfYrearsLabel.setTranslateY(243.0);

simpleIntrestbtn.setText(“Simple Intrestbtn”);

simpleIntrestbtn.setTranslateX(56.0);

simpleIntrestbtn.setTranslateY(329.0);

compountIntrestbtn = new Button();

compountIntrestbtn.setText(“CompountIntrestbtn”);

compountIntrestbtn.setTranslateX(256.0);

compountIntrestbtn.setTranslateY(329.0);

bothIntrestbtn = new Button();

bothIntrestbtn.setText(“Both Intrest”);

bothIntrestbtn.setTranslateX(451.0);

bothIntrestbtn.setTranslateY(329.0);

AnchorPane root = new AnchorPane();

//  StackPane root = new StackPane();

root.prefHeight(400.0);

root.prefWidth(600.0);

root.getChildren().add(bothIntrestbtn);

root.getChildren().add(compountIntrestbtn);

root.getChildren().add(simpleIntrestbtn);

root.getChildren().add( principlLabel);

root.getChildren().add( rateLabel);

root.getChildren().add( noOfYrearsLabel);

root.getChildren().add( principlTextfield);

root.getChildren().add( principlPercentTextfield);

root.getChildren().add( textarea);

root.getChildren().add(comb);

//root.getChildren().add( textarea);

javafx.scene.Scene scene = new javafx.scene.Scene(root, 600, 400);

primaryStage.setTitle(“Interest Table”);

primaryStage.setScene(scene);

primaryStage.show();

}

}

Solution 

InterestTableGUI.java

packageinteresttable;

importjavafx.application.Application;

public class InterestTableGUI {

public static void main(String[] args) {

Application.launch(MyController.class, args);

}

} 

Model.java

packageinteresttable;

importjava.text.NumberFormat;

public class Model {

double principal;

double rate;

int year;

public void setPrincipal(double principal) {

this.principal = principal;

}

public void setRate(double rate) {

this.rate = rate;

}

public void setYear(int year) {

this.year = year;

}

public double getPrincipal() {

return principal;

}

public double getRate() {

return rate;

}

publicintgetYear() {

return year;

}

public String simpleIntrest(double principal,doublerate,int year){

String resutl = “”;

int i=1;

for (i=1;i<=year;i++){

doubleinterest_amount;

doublecal_rate;

cal_rate = (principal*(rate/100)*i);

interest_amount = cal_rate + principal;

String formattedValue = NumberFormat.getCurrencyInstance().format(interest_amount);

resutl = resutl+”simpleIntrest of year” +i+”  “+formattedValue+”\n”;

}

returnresutl;

}

public String computeIntrest(double principal,intyear,double rate){

String resutl = “”;

for(int i=1;i<=year;i++) {

doublecal_rate = principal*Math.pow(1+(rate/100),i);

String formattedValue = NumberFormat.getCurrencyInstance()

.format(cal_rate);

resutl = resutl+”ComputedIbtrestos year “+i+”  “+formattedValue+”\n”;

}

returnresutl;

}

public String both(double principal,doublerate,int year){

String result = “”;

result = result+computeIntrest(principal, year, rate)+”\n”;

result = result+simpleIntrest(principal, rate, year);

return result;

}

} 

MyController.java

packageinteresttable;

importjavafx.application.Application;

importjavafx.event.ActionEvent;

importjavafx.event.EventHandler;

importjavafx.stage.Stage;

public class MyController extends Application{

Model model;

View view;

public void setContoll(){

Demo d = new Demo();

view.getSimpleIntrestbtn().setOnAction(d);

view.compountIntrestbtn.setOnAction((ActionEvent event) -> {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

view.getTextarea().setText( model.computeIntrest(principal,year,rate));

});

view.bothIntrestbtn.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent event) {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

model.computeIntrest(principal,year,rate);

view.getTextarea().setText( model.both(principal, rate, year));

}

});

}

@Override

public void start(Stage primaryStage) throws Exception {

this.model = new Model();

this.view = new View(primaryStage);

setContoll();

}

class Demo implements EventHandler<ActionEvent>{

@Override

public void handle(ActionEvent event) {

String principalvalue=view.principlTextfield.getText();

Double principal= Double.parseDouble(principalvalue);

model.setPrincipal(principal);

String ratevalue=view.principlPercentTextfield.getText();

Double rate= Double.parseDouble(ratevalue);

model.setPrincipal(rate);

String yearvalue=view.getComb().getSelectionModel().getSelectedItem().toString();

int year= Integer.parseInt(yearvalue);

model.setPrincipal(year);

view.getTextarea().setText( model.simpleIntrest(principal, rate,year));

}

}

} 

View.java

packageinteresttable;

importjava.util.ArrayList;

importjava.util.logging.Level;

importjava.util.logging.Logger;

importjavafx.application.Application;

importjavafx.event.ActionEvent;

importjavafx.event.EventHandler;

importjavafx.scene.control.Button;

importjavafx.scene.control.ComboBox;

importjavafx.scene.control.Label;

importjavafx.scene.control.TextArea;

importjavafx.scene.control.TextField;

importjavafx.scene.layout.AnchorPane;

importjavafx.scene.layout.StackPane;

importjavafx.stage.Stage;

// import sun.plugin.javascript.navig.Anchor;

public class View extends AnchorPane{

Button simpleIntrestbtn;

Button bothIntrestbtn;

Button compountIntrestbtn;

TextAreatextarea;

ComboBox comb;

TextFieldprinciplTextfield;

TextFieldprinciplPercentTextfield;

public View(Stage s){

try {

start(s);

} catch (Exception ex) {

Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);

}

}

publicTextAreagetTextarea() {

returntextarea;

}

public void setTextarea(TextAreatextarea) {

this.textarea = textarea;

}

publicComboBoxgetComb() {

return comb;

}

public void setComb(ComboBox comb) {

this.comb = comb;

}

publicTextFieldgetPrinciplTextfield() {

returnprinciplTextfield;

}

public void setPrinciplTextfield(TextFieldprinciplTextfield) {

this.principlTextfield = principlTextfield;

}

publicTextFieldgetPrinciplPercentTextfield() {

returnprinciplPercentTextfield;

}

public void setPrinciplPercentTextfield(TextFieldprinciplPercentTextfield) {

this.principlPercentTextfield = principlPercentTextfield;

}

public Button getBothIntrestbtn() {

returnbothIntrestbtn;

}

public void setBothIntrestbtn(Button bothIntrestbtn) {

this.bothIntrestbtn = bothIntrestbtn;

}

public Button getCompountIntrestbtn() {

returncompountIntrestbtn;

}

public void setCompountIntrestbtn(Button compountIntrestbtn) {

this.compountIntrestbtn = compountIntrestbtn;

}

public Button getSimpleIntrestbtn() {

returnsimpleIntrestbtn;

}

public void setSimpleIntrestbtn(Button simpleIntrestbtn) {

this.simpleIntrestbtn = simpleIntrestbtn;

}

public void start(Stage primaryStage) throws Exception {

Model m = new Model();

simpleIntrestbtn = new Button();

comb = new ComboBox();

comb.setTranslateX(433.0);

comb.setTranslateY(284.0);

ArrayList<String> years = new ArrayList<String>();

for(int i=1;i<26;i++)

years.add(“”+i);

comb.getItems().addAll(years);

comb.setValue(“1”);

textarea = new TextArea();

textarea.setTranslateX(55.0);

textarea.setTranslateY(20.0);

textarea.setPrefHeight(200.0);

textarea.setPrefWidth(335.0);

principlTextfield= new TextField();

principlTextfield.setTranslateX(42.0);

principlTextfield.setTranslateY(284.0);

principlPercentTextfield= new TextField();

principlPercentTextfield.setTranslateX(242.0);

principlPercentTextfield.setTranslateY(284.0);

//        String ratevalue=principlTextfield.getText();

//        Double rate= Double.parseDouble(ratevalue);

//        m.setPrincipal(rate);

Label principlLabel= new Label();

principlLabel.setText(“Principal :”);

principlLabel.setTranslateX(60.0);

principlLabel.setTranslateY(243.0);

Label  rateLabel= new Label();

rateLabel.setText(“Rate(Persentage):”);

rateLabel.setTranslateX(268.0);

rateLabel.setTranslateY(243.0);

Label  noOfYrearsLabel= new Label();

noOfYrearsLabel.setText(“No Of Years :”);

noOfYrearsLabel.setTranslateX(463.0);

noOfYrearsLabel.setTranslateY(243.0);

simpleIntrestbtn.setText(“Simple Intrestbtn”);

simpleIntrestbtn.setTranslateX(56.0);

simpleIntrestbtn.setTranslateY(329.0);

compountIntrestbtn = new Button();

compountIntrestbtn.setText(“CompountIntrestbtn”);

compountIntrestbtn.setTranslateX(256.0);

compountIntrestbtn.setTranslateY(329.0);

bothIntrestbtn = new Button();

bothIntrestbtn.setText(“Both Intrest”);

bothIntrestbtn.setTranslateX(451.0);

bothIntrestbtn.setTranslateY(329.0);

AnchorPane root = new AnchorPane();

//  StackPane root = new StackPane();

root.prefHeight(400.0);

root.prefWidth(600.0);

root.getChildren().add(bothIntrestbtn);

root.getChildren().add(compountIntrestbtn);

root.getChildren().add(simpleIntrestbtn);

root.getChildren().add( principlLabel);

root.getChildren().add( rateLabel);

root.getChildren().add( noOfYrearsLabel);

root.getChildren().add( principlTextfield);

root.getChildren().add( principlPercentTextfield);

root.getChildren().add( textarea);

root.getChildren().add(comb);

//root.getChildren().add( textarea);

javafx.scene.Scene scene = new javafx.scene.Scene(root, 600, 400);

primaryStage.setTitle(“Interest Table”);

primaryStage.setScene(scene);

primaryStage.show();

}

}