2015-09-05 59 views
1

在這個程序中,我被要求創建一個名爲錢包的ArrayList,它將美國硬幣名稱作爲字符串輸入到它。然後我們被要求將每個輸入值分配給它相應的double值。所以當用戶輸入「便士」時,程序應該認識到字符串「便士」是指雙倍0.01。我在Coin課程中通過枚舉完成了這些操作。現在我的任務是在我的Purse類中創建一個新的方法,將double值相加。我已經差不多完成了,但是我已經把代碼放在了AddCoin方法中。由於我需要爲這個任務創建一個新方法,所以我想知道是否有辦法用我現在擁有的方法來完成這個任務。添加ArrayList <String>通過枚舉引用一個雙引號輸入

package purse; 
import java.util.Scanner; 
import java.util.ArrayList; 




/** 
* The Purse program creates an ArrayList called purse that gets printed out, 
    reversed, and transfered into another ArrayList called purse2. 
*  
*    - ArrayList purse 
*    - ArrayList purse2 
*    - Scanner coin - the Scanner that is used to type the contents of ArrayList purse 
*    - Scanner coin2- the Scanner that is used to type the contents of ArrayList purse2 
*    - String input - contains Scanner coin and is used to fill ArrayList purse 
*    - String input2- contains Scanner coin2 and is used to fill ArrayList purse2 
*    - String end - sentinel for ending the process inputting strings into Scanner coin and Scanner coin2 
*  
*/ 
public class Purse 
{ 
    ArrayList<String> purse = new ArrayList<>(); 



    /** 
    * fills ArrayList purse and purse2 with U.S coin names 
    * purse gets printed out and then again is printed in reverse 
    * purse2 is printed 

    */ 






    public void addCoin() 
    { 
     double sum = 0.0; 
     String end = "done"; 

     Scanner coin = new Scanner (System.in); 
     String input = " "; 

     System.out.println("Please put as many coins of U.S currency as you like into the purse, hit the ENTER button after each coin and, type 'done' when finished: "); 

     while (!input.equalsIgnoreCase ("done")) 

     { 
      input = coin.nextLine(); 
      Coin c = new Coin(Coin.Value.valueOf(input)); 
      for(int i =0; i< c.getValue();i++) 
       sum += c.getValue(); 
      System.out.println(sum); 

     if (input.equalsIgnoreCase("penny")||input.equalsIgnoreCase("nickel")||input.equalsIgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(end)) 
     { 
      purse.add(input); 
      purse.remove(end); 

     } 



     else{ 
      System.out.println("Please input a coin of U.S currency."); 
      } 
     } 


    } 

    /** 
    @return ArrayList purse 
    */ 
    public ArrayList<String> printPurseContents() 
    { 
     System.out.println("Contents of the purse: " + purse); 
     return purse; 
    } 

    /** checks whether purse2 has the same coins in the same order as purse 
    * @return 
    * @param purse2 
    */ 



    public boolean sameContents(Purse purse2) 
    { 

     if (purse2.purse.equals(purse)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 

    } 
    /** 
    * checks whether purse2 has the same coins as in purse, disregarding the order 
    * @param purse2 
    * @return 
    */ 

    public boolean sameCoins(Purse purse2) 
    { 

    if(purse2.purse.containsAll(purse)) 
    { 
     return true; 
    }  
    else 
    { 
     return false; 
    } 
    } 


    /** 
    * adds contents of purse into purse2 and clears purse of its contents 
    * @param purse2  
    */ 
    public void transfer(Purse purse2) 
    { 
       purse2.purse.addAll(purse); 
       purse.clear(); 

    System.out.println("The second purse now has: " + purse2.purse); 
    System.out.println("and the first purse has: " + purse); 


    } 



} 

----------硬幣類

package purse; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** private string name; 
* private double value; 
* sum up value method 
* 
* @author Thomas 
*/ 
public class Coin 
{ 
    public static enum Value 
    { 
     penny(0.01), nickel(0.05), dime(0.10), quarter(0.25), done(0); 

     double change; 

     Value(double value) 
     { 
      this.change = value; 
     } 
    } 
    private Value type; 

    public Coin(Value type) 
    { 
     this.type = type; 
    } 
    public double getValue() 
    { 
     return type.change; 
    } 
    public String getName() 
    { 
     return type.name(); 
    } 


    } 

回答

0

應使用的ArrayList<Coin>代替ArrayList<String>,然後添加的總和()方法等:

public double sum() { 
    double sum = 0d; 
    for (Coin c : coins) { 
     sum+=c.getValue(); 
    } 
} 
+0

當我嘗試給我們一個ArrayList 然後我的返回不起作用,我不能將我的字符串輸入添加到我的數組或使用錢包。除去(結束)。我怎麼能解決這個問題,以配合你的意見? – LamasTain

+0

另外,當我把它放到我的程序中時,我被告知要初始化硬幣,程序將它設置爲迭代器硬幣。然後它抱怨說它沒有被初始化,所以它將它設置爲null。哪個不是真的有用。還有什麼可以設置的? – LamasTain