好的,这个程序的目标是将字符串值(如便士,角钱,四分之一和镍)输入到类Purse中的ArrayList钱包中。然后,一旦Purse通过PurseMain类执行,那么Coin类应该获取添加到ArrayList钱包中的硬币名称的字符串值并将它们的美元值加在一起。我的教授告诉我们在名为Coin的第三类中创建硬币值,并在PurseMain中创建另一个ArrayList。但实际上我的主要问题是,如何将在Coin类中创建的对象调用到PurseMain中的ArrayList中?任何帮助,将不胜感激。感谢。
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()
{
String penny = "penny";
String nickel = "nickel";
String dime = "dime";
String quarter = "quarter";
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());
if (input.equalsIgnoreCase("penny")||input.equalsIgnoreCase("nickel")||input.equals IgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(en d))
{
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);
}
}
---------- PurseMain
package purse;
import java.util.ArrayList;
/*
* 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.
*/
/**legalcoin = new ArrayList<Coin>; <-----
* legalcoin.add( new Coin ("Penny", .01); <--- Professor said to do
*
* @author
* 9/01/2015
* Lab I
*/
public class PurseMain
{
private ArrayList<Coin> legalCoin = new ArrayList<>();
legalCoin.add (new Coin ("Penny",0.01));
public static void main(String[] args)
{
Purse purse1 = new Purse();
purse1.addCoin();
purse1.printPurseContents();
Purse purse2 = new Purse();
purse2.addCoin();
purse2.printPurseContents();
System.out.println("Both of the purses have the same contents and the contents is in the same order: " + purse2.sameContents(purse1));
System.out.println("Both of the purses have the same contents: " +purse2.sameCoins(purse1));
}
}
----------硬币
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
*/
public class Coin
{
private String Penny = "penny";
private String Nickel = "nickel";
private String Dime = "dime";
private String Quarter = "quarter";
private double penny = 0.01;
private double nickel = 0.05;
private double dime = 0.10;
private double quarter = 0.25;
public void SumUpValue()
{
}
}
答案 0 :(得分:0)
你需要在你的钱包里放一个新的硬币袋(要求明智)。所以,有一个硬币袋,里面有硬币清单。因为钱包里有硬币袋,所以它是你钱包的财产。
public class Coin{
private String type;
private String currencyType;
//more fields and getters and setters
}
public class Purse{
ArrayList<String> purse = new ArrayList<>();
List<Coins> coinPouch = new ArrayList<>();//contains coin objects
.
.
.
.
private void addCoins(String coinType){
Coin coin = new Coin();
coin.setType(coinType);
coinPouch.add(coin);
}
}
您修改的添加硬币的代码如下:
if (input.equalsIgnoreCase("penny")|| input.equalsIgnoreCase("nickel") ||input.equalsIgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(end)){
addCoins(input);//Adds coins to the list of coins objects and the purse now has the coin pouch :D
purse.remove(end);
}
else{
System.out.println("Please input a coin of U.S currency.");
}