Simple Java Program (CoinLister) using JGRASP

Words
801
Reading
4 min
Listen
Play
8y


Please be advised that below you will see the codes using only JGRASP.

import java.awt.*; 
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class CoinPortfolioGUI extends JFrame
{

private JTextField jtfCoinName = new JTextField();
private JTextField jtfAbbrv = new JTextField();
private JTextField jtfCoinPrice = new JTextField("0");
private JTextField jtfCoinAmount = new JTextField("0");
private JTextField jtfTotal = new JTextField("0");

JButton add, delete, another;

JTextArea AreaList = new JTextArea("Coinname Abbreviation Price Amount Bought
Total Investment ", 20, 10);

private JButton jbtComputeTotal = new JButton(" Compute Total ");

CoinClass ccoin;
int counter = 0;
ArrayList clist = new ArrayList(20);

addHandler aHandler;
deleteHandler dHandler;
anotherHandler anHandler;

//GUI POSITIONS & RESTRICTIONS//
public CoinPortfolioGUI()
{
add = new JButton(" List ");
delete = new JButton(" Delist ");
another = new JButton(" Add New Coin ");

aHandler = new addHandler();
add.addActionListener(aHandler);

dHandler = new deleteHandler();
delete.addActionListener(dHandler);

anHandler = new anotherHandler();
another.addActionListener(anHandler);

JPanel p1 = new JPanel(new GridLayout(10, 50));
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));

p1.add(new JLabel("Coin Name"));
p1.add(jtfCoinName);

p1.add(new JLabel("Abbreviation"));
p1.add(jtfAbbrv);

p1.add(new JLabel("Price"));
p1.add(jtfCoinPrice);

p1.add(new JLabel("Amount Bought"));
p1.add(jtfCoinAmount);

p1.add(new JLabel("Total"));
p1.add(jtfTotal);


p2.add(jbtComputeTotal);
p2.add(another);
p2.add(delete);
p2.add(add);


p3.add(AreaList);


add(p1, BorderLayout.EAST);
add(p2, BorderLayout.SOUTH);
add(p3, BorderLayout.CENTER);

jbtComputeTotal.addActionListener(new ButtonListener());

toggleDisable(true);
toggleButtons(false);  
jtfTotal.setEditable(false);
AreaList.setEditable(false);   
}    

public void toggleDisable(boolean a)
{
jtfCoinName.setEditable(a);
jtfAbbrv.setEditable(a);
jtfCoinAmount.setEditable(a);
jtfCoinPrice.setEditable(a);

}
public void toggleButtons(boolean a)
{
another.setEnabled(a);
add.setEnabled(!a);
jbtComputeTotal.setEnabled(!a);
}
//CLEARING DEFAULT AFTER COIN IS LISTED (SET TO BLANK AND ZEROS)
public void clearTextField()
{
jtfCoinName.setText("");
jtfAbbrv.setText("");
jtfCoinPrice.setText("0");
jtfCoinAmount.setText("0");
jtfTotal.setText("0");
}

private class anotherHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        clearTextField();
        toggleDisable(true);
        toggleButtons(false);
    }
}

private class addHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"Coin listed");
toggleButtons(true);

        String cname = new String(jtfCoinName.getText()).toString();
        String abrv = new String(jtfAbbrv.getText()).toString();
        double price = Double.parseDouble(jtfCoinPrice.getText());
        double amount = Double.parseDouble(jtfCoinAmount.getText());
        double total = Double.parseDouble(jtfTotal.getText());

        ccoin = new CoinClass(cname, abrv, price, amount, total);
        jtfTotal.setText(String.format("%.2f", ccoin.getAmount()));


        clist.add(counter, ccoin.toString());
        counter++;
        toggleButtons(true);
        toggleDisable(false);
        up();    
    }   

}

public void up() {
AreaList.setText("Coinname Abbreviation Price Amount Bought Total Investment ");
for(int i=0; i<clist.size(); i++) {
String s = clist.get(i).toString();
AreaList.append(s);
}
}

private class deleteHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(!clist.isEmpty()) {
String str = JOptionPane.showInputDialog("Enter the index to delist.");
int i = Integer.parseInt(str);
if( i >= clist.size())
JOptionPane.showMessageDialog(null, "Coin not found.");
else {
for(int n=0; n<clist.size(); n++) {
if(i==n){
String s = clist.get(n).toString();
clist.remove(s);
counter--;
}
}
}
}
else
JOptionPane.showMessageDialog(null, "The portfolio is Empty.");
up();
}
}

private class ButtonListener implements ActionListener
{
//TOTAL INPUT FORMULA (PRICE * AMOUNT = TOTAL INVESTMENT)//
public void actionPerformed(ActionEvent e )
{
double pri = Double.parseDouble(jtfCoinPrice.getText());
double tot = Double.parseDouble(jtfCoinAmount.getText());
double inv = Double.parseDouble(jtfTotal.getText());

    inv = pri * tot;
    String result = new Double(inv).toString();
    jtfTotal.setText(result);
    String cname = new String(jtfCoinName.getText()).toString();
    String abrv = new String(jtfAbbrv.getText()).toString();

    double price = Double.parseDouble(jtfCoinPrice.getText());
    double amount = Double.parseDouble(jtfCoinAmount.getText());
    double total = Double.parseDouble(jtfTotal.getText());
    //CALLING CoinClass.java//
    CoinClass ccoin = new CoinClass(cname, abrv, price, amount, total);

    toggleButtons(false);
    toggleDisable(true);
}

}
//RUN COINPORTFOLIOGUI
public static void main(String[] args)
{
CoinPortfolioGUI frame = new CoinPortfolioGUI();
frame.setTitle("Coin Portfolio");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
}
}

You may not able to run this without the CoinClass.java

public class CoinClass
{
        //DECLARATION OF VARIABLES//
        private String cname;
        private String abrv;
        private double price;   
        private double amount;
        private double total;
        //CONSTRUCTORS//
        public CoinClass(String cname, String abrv, double amount, double price, double total)
        {
            this.cname = cname;
        this.abrv = abrv;
        this.amount = amount;
        this.price = price;
        this.total = total;
    }
    //GETTER COINNAME//
    public String getCname()
    {
        return cname;
    }
    //SETTER COINNAME//
    public void setCname(String cname)
    {
        this.cname = cname;
    }
    //GETTER ABRV//
    public String getAbrv()
    {
        return abrv;
    }
    //SETTER ABRV//
    public void setAbrv(String abrv)
    {
        this.abrv = abrv;
    }
    //GETTER AMOUNT//
    public double getAmount()
    {
        return amount;
    }
    //SET AMOUNT OF COINS FOR PORTFOLIO//
    public void setAmount(double amount)
    {
        this.amount = amount;
    }
    //GETTER PRICE OF THE COIN//
    public double getPrice()
    {
        return price;
    }
    //SET PRICE OF THE COINS FOR PORTFOLIO//
    public void setPrice(double price)
    {
        this.price = price;
    }
    //GETTER TOTAL AMOUNT OF INVESTMENT (PRICE * AMOUNT)//
    public double getTotal()
    {
        return total;
    }
    //SET TOTAL AMOUNT OF INVESTMENT (PRICE * AMOUNT)//
    public void setTotal(double total)
    {
        this.total = total;
    }
public String toString()
 {
      return "\n"+cname+"                     "+abrv+"                       "+amount+" USD                        "+price+" Coins/Tokens                   "+total+ " USD";
 }

}

//DONE PUBLIC CLASS CASHIER

Here is a Sample Run, There are a little bit of errors because I only used JGRASP as my compiler. No Drag and drops.

alt

Running Smoothly using JGRASP

alt

Compute Total Action Performed

alt

Sample (3) Listed Coin @ index with JText Disabled after Performed

Delisting a coin is simple, we'll have to use the 0-20 number indexes. The program still have some Flaws, the purpose of making this program is to understand the basics of GUI, by this - I was able to debug it manually. Thanks to Jgrasp lol.

Posted from my blog with SteemPress : http://chuckunlimited.net/index.php/2018/10/25/simple-java-program-coinlister-using-jgrasp/
![steempress.png](
SteemPress Moderator | [Discord Server](https://discord.gg/WZ42psQ)

Simple Java Program (CoinLister) using JGRASP | Ecency