如何修复我的代码,使其在购买后返回更新的值

时间:2019-04-09 17:58:37

标签: arraylist

这是我的硬币对象,在驱动程序代码的数组列表中使用。

public class Coin
{
    private int side;
    private double value;
    private String name;

     public Coin()
    {

        side = 1;
        value = 0.25;
        name = "Quarter";
    }

    public Coin(String iname, double ivalue)
    {
        name = iname;
        value = ivalue;
        flip();
    }


    public String getName()
    {
        return name;
    }

    public double getValue()
    {
        return value;
    }

    public void setName(String iname)
    {
        name = iname;
    }

    public void setValue(double ivalue)
    {
        value = ivalue;
    }

    public String flip()
    {
        side = (int)(Math.random()* 2);
        if (side == 0)
           return "Heads";
        else 
           return "Tails";
     }
    public String toString()
    {   String outside = "";
        if (side == 0)
          outside = "Heads";
        else 
          outside = "Tails";

        return "Coin name " + name + "Coin value: " + value + "  " + outside;
      }
}

这是我的coindriver类。这是我无法返回更新的钱包arraylist的值的问题。

import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

public class CoinDriver
{
    private List<Coin> Wallet;
    public  CoinDriver()
    {
        Wallet = new ArrayList<>();
    }

    public static void main(String[] args)
    {
        int choice = 0;
        CoinDriver Wallet = new CoinDriver();
        Scanner scanner = new Scanner(System.in);
        while (choice != 2)
        {
            System.out.println("1 Purchase an item 2 Calculate money in wallet and end program");
            choice = scanner.nextInt();
            Wallet.InputCoin();
            if (choice == 1)
                Wallet.PurchaseItem();
            System.out.println("Net amount in your wallet is " + Wallet.WalletValue());
        }
    }

    void InputCoin()
    {
        Coin Penny = new Coin ("Penny",0.01);
        Coin Dime = new Coin ("Dime", 0.10);
        Coin Dollar = new Coin ("Dollar",1.00);
        Coin Twenty = new Coin ("Twenty", 20.00);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Twenty);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Twenty);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Twenty);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Twenty);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Twenty);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
        Wallet.add(Penny);
        Wallet.add(Dime);
        Wallet.add(Dollar);
    }

    double WalletValue()
    {
        double sum = 0;
        for(int x = 1; x < Wallet.size(); x++)
        {
            sum += Wallet.get(x).getValue();
        }
        return sum;
    }

    void PurchaseItem()
    {
        Coin Penny = new Coin ("Penny",0.01);
        Coin Dime = new Coin ("Dime", 0.10);
        Coin Dollar = new Coin ("Dollar",1.00);
        Coin Twenty = new Coin ("Twenty", 20.00);

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the cost of item you want to purchase: ");
        double price = scanner.nextDouble();
        double total = WalletValue();
        int round = (int) Math.round(price);
        int calctwenty = round/20;
        int calcdollar = round - calctwenty*20;
        double calcdime = price%1;
        double rounddime = (Math.round(calcdime))/10;
        double calcpenny = (price*10)%1;
        double roundpenny = (Math.round(calcpenny));

        if (price > total)
        {
            System.out.println("Insufficient money, cannot purchase");
            return;
        }

        if (calctwenty > 0)
        {
            for (int i = 0; i < calctwenty; i++)
            {
                Wallet.remove(Twenty);
            }
        }
        if (calcdollar > 0)
        {
            for (int j = 0; j < calcdollar; j++)
            {
                Wallet.remove(Dollar);
            }
        }
        if (rounddime > 0)
        {
            for( int y = 0; y < rounddime; y++)
            {
                Wallet.remove(Dime);
            }
        }
        if (rounddime > 0)
        {
            for ( int p = 0; p < roundpenny; p++)
            {
                Wallet.remove(Penny);
            }
        }
        WalletValue();
    }

}

我希望代码在购买并删除对象后返回更新的值。但是实际上,代码仍返回初始钱包值。请帮助谢谢

0 个答案:

没有答案