Search code examples
javacurrencybitcointracker

Why isn't my live result updating in main?


I'm making a currency tracker to track the live value of bitcoin.

the class "bitcoinlive" runs Correctly if I run it in its own main method, but it won't work when I make an instance of the file. I need it to print the live value you of bitcoin.

I try to print out the variable "a53" but I don't know if I'm doing it right. Here are the list of imports for the bitcoinlive class because it kept giving me an error message and wouldn't allow this to be apart of the code when posting this.

public static void main(String[] args) {
    Dates d = new Dates();
    String s = d.getDate();
    System.out.println("Date is" + s);
    W3 mywallet = new W3();
    Bitcoinlive mybitcoinlive = new Bitcoinlive();
    L3 myledger = new L3();
    Scanner myscanner = new Scanner(System.in);
    double buy = 0.0;
    int choice = 0;
    double bitcoin = 4000;
    double USD = 20000;

    while (choice != 5) {
        System.out.println("Welcome! Enter a command. \n"
                + "Enter 1) Buy Bitcoin \n"
                + "Enter 2) Sell Bitcoin  \n"
                + "Enter 3) Print Balance \n"
                + "Enter 4) Print History \n"
                + "ENTER 5) Exit Program\n");
        choice = myscanner.nextInt();

        if (choice == 1) {
            System.out.println("How many? ");
            buy = myscanner.nextDouble();
            mywallet.add(buy);
            bitcoin = bitcoin * buy;
            USD = USD - bitcoin;

            myledger.save(s);

            System.out.println("you have bought:" + mywallet.numcoins);
            System.out.println(USD);
            System.out.println(mybitcoinlive.a53);
            bitcoin = 4000;

        } else if (choice == 2 && USD >= bitcoin) {
            System.out.println("How many?");

            buy = myscanner.nextDouble();
            mywallet.subtract(buy);
            System.out.println("you have sold:" + mywallet.numcoins);
            USD = USD + bitcoin;
            System.out.println(USD);
            bitcoin = 4000;
            myledger.save(s);

        } else if (choice == 3) {
            System.out.println("Balance:" + mywallet.numcoins);

        } else if (choice == 4) {
            System.out.println("Transaction history:  ");

            System.out.println("you have made" + myledger.getsize() + "transactions"
                    + d.getDate());

        } else if (choice == 5) {
            // exit
            break;

        } else if (choice == 7) {
            System.out.println(mybitcoinlive.price);
        }

    }

    System.out.println("Bye");

}

this is my separate class

public class Bitcoinlive {



    Double a53=0.0;
    double price;     

    Double get() {

        try {
            String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
            Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
            String a13 = docblocktradescoincapeth13.toString();
            int a23 = a13.indexOf("price_usd") + 13;
            int a33 = a13.indexOf("price_btc") - 4;
            String a43 = a13.substring(a23, a33);
            a53 = Double.parseDouble(a43);
        } catch (Exception e) {
            System.out.println("Error accessing bitcoin values");
        }

        return a53;
    }
}

Solution

  • Your class Bitcoinlive stores the price in a field called a53. You can update this field by calling get(). However, it looks like you're never calling get() - you're just calling the field:

    System.out.println(mybitcoinlive.a53);
    

    Try replacing that line with:

    System.out.println(mybitcoinlive.get());
    

    Or refresh it first:

    mybitcoinlive.get();
    System.out.println(mybitcoinlive.a53);