【发布时间】:2015-09-05 13:52:48
【问题描述】:
在这个程序中,我需要创建一个名为 purse 的 ArrayList,它将美国硬币名称作为字符串输入其中。然后我们被要求将每个输入值分配给它对应的双精度值。因此,当用户键入“penny”时,程序应该识别出字符串“penny”指的是双倍 0.01。我在 Coin 课程中通过枚举完成了这一点。现在我的任务是在我的 Purse 类中创建一个将双精度值相加的新方法。我几乎已经完成了,但我已将代码执行任务放在 AddCoin 方法中。因为我需要为此任务创建一个新方法,所以我想知道是否有办法用我现在拥有的方法来完成它。
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();
}
}
【问题讨论】: