Reputation: 11
For this code, I create an array list where a user is prompted to enter a coin of type String into a 'purse' which is then added to the array list. I have a method that returns the array list, printing out the coins in the list. I created several other methods that act upon the array list in several ways. The main issue that I am having is creating a class called Coin, in which I want to assign a value to each coin entered in the array list 'coins'. This would allow me to create a method to add up the values of each coin in the 'coins' array or a method to spend a coin from the 'coins' array. I thought about adding a public void setType(String entered) method in the Coins class to assign each coin entered a value, but I am not entirely sure how to go about it. Once it is made, however, I know that I want to create a list: List coinPocket = new ArrayList<>(); and a private method that adds the values from the Coin class to the array list:ArrayList coins = new ArrayList();. I was wondering if anyone knew how I would go about this. Here is all of my code so far:
public class Coin
{
private String type;
private String currencyType;
private double penny = 0.01;
private double quarter = 0.25;
private double dime = 0.10;
private double nickle = 0.05;
public void setType(String entered)
{
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
/**
*
* A class that contains methods and variables for adding and rearranging coins in a purse. An array list of coins; two variables, TERMINATE to stop adding coins and coinEntered to represent a coin in the purse; and a Scanner for user input are used.
*/
public class Purse
{
ArrayList<String> coins = new ArrayList<String>();
List<Coin> coinPocket = new ArrayList<>();
Scanner in = new Scanner(System.in);
private final String TERMINATE = "Q";
private String coinEntered = " ";
private void addCoins(String coinType)
{
Coin cash = new Coin();
cash.setType(coinType);
coinPocket.add(cash);
}
/**
* A method that allows the user to add coins to their purse and second purse, assuming it is American currency. If it is not American currency, the user is asked to enter the correct currency. The user will be prompted to press Q to quit adding coins once they have added them all.
* @param coinName
*
*/
public void addCoin(String coinName)
{
System.out.println("Which coin would you like to add? PENNY, NICKLE, DIME, or QUARTER? Press Q to stop adding coins.");
while (!coinEntered.equals(TERMINATE))
{
coinEntered = in.nextLine();
if (coinEntered.equals("PENNY") || coinEntered.equals("NICKLE") || coinEntered.equals("DIME") || coinEntered.equals("QUARTER") || coinEntered.equals(TERMINATE))
{
coins.add(coinEntered);
coins.remove(TERMINATE);
}
else
{
System.out.println("Sorry! You can only add American currency to the purse. Please add American currency.");
}
}
}
/**
* Prints out the purse and its contents after adding coins.
* @return
* Purse with added coins.
*/
public ArrayList<String> printPurseContents()
{
return coins;
}
/**
* Reverses the order of the coins in a purse, then prints out the contents of the purse in reverse order.
* @return
* Reverse order of array of coins.
*/
public ArrayList<String> reverse()
{
Collections.reverse(coins);
return coins;
}
/**
* A method that allows the user to transfer coins in a purse to a different purse. The coins transfered from the original purse will leave that purse empty.
* @param otherPurse
*
*/
public void transfer(Purse otherPurse)
{
coins.addAll(otherPurse.coins);
otherPurse.coins.clear();
}
/**
* Method that checks whether one purse has the same coins in the same order as another purse.
* @param otherPurse
* @return Prints true if the contents of each purse have the same coins and the same order of coins, or false if the contents of each purse do not have the same coins and the same order of coins.
*/
public boolean sameContents(Purse otherPurse)
{
if(otherPurse.coins.equals(coins))
{
System.out.println("It is true that each purse has the same coins in the same order.");
return true;
}
else
{
System.out.println("It is false that each purse has the same coins in the same order.");
return false;
}
}
/**
* Method that checks whether one purse has the same coins as another purse regardless of the order of coins.
* @param otherPurse
* @return Prints true if each purse has the same coins as another purse regardless of order, or prints false if each purse does not have the same coins as another purse regardless of order.
*/
public boolean sameCoins(Purse otherPurse)
{
if(otherPurse.coins.containsAll(coins))
{
System.out.println("It is true that each purse has the same coins regardless of order.");
return true;
}
else
{
System.out.println("It is false that each purse has the same coins regardless of order.");
return false;
}
}
public void addTotalCoins()
{
}
public void spendCoin()
{
}
}
public class PurseMain
{
public static void main(String[] args)
{
Purse johnnysPurse = new Purse();
Purse otherPurse = new Purse();
johnnysPurse.addCoin(null);
otherPurse.addCoin(null);
System.out.println("Purse " + johnnysPurse.printPurseContents());
System.out.println("Purse " + otherPurse.printPurseContents());
System.out.println();
System.out.println(otherPurse.sameContents(johnnysPurse));
System.out.println();
System.out.println(otherPurse.sameCoins(johnnysPurse));
System.out.println();
}
}
Upvotes: 1
Views: 892
Reputation: 9559
I would use an enum, bt discourage use of double, use integer instead.
Here's my Coin class:
public class Coin {
public static enum Type {
QUARTER(25),
DIME(10),
NICKEL(5),
PENNY(1);
int value;
Type(int value) {
this.value = value;
}
}
private Type type;
public Coin(Type type) {
this.type = type;
}
public int getValue() {
return type.value;
}
public String getName() {
return type.name();
}
}
A new coin may be created like this:
String coinEntered = //get string from user
Coin coin = new Coin(Coin.Type.valueOf(coinEntered));
Note that an java.lang.IllegalArgumentException
will be thrown if the entered string does not match any coin type. You may want to catch this exception to generate a warning message to the user.
Upvotes: 1
Reputation: 88707
If I understand your question correctly, you want to ask the user to enter strings like "PENNY", "DIME" etc. and create a Coin
out of this.
To do this you could use an enum with properties, e.g.:
enum CoinType {
PENNY(0.01),
NICKLE(0.05)
DIME(0.1),
QUARTER(0.25);
private final double value;
private CoinType( double v ) {
value = v;
}
public double getValue() {
return value;
}
}
Then ask the user for either a number (provide a selection list) or the name. Assuming you ask for the name you could then do the following:
CoinType type = CoinType.valueOf( coinEntered );
Catch the IllegalArgumentException
in case the user entered a wrong name.
Upvotes: 1