Question: Need help with the following assignment: Deliverables: Pocket.java Coin.java Nickle.java Penny.java Dime.java Quarter.java * are all the same methods just with cooresponding names PocketMain.Java NoSuchCoin.java
Need help with the following assignment:
Deliverables:
Pocket.java
Coin.java
Nickle.java Penny.java Dime.java Quarter.java * are all the same methods just with cooresponding names
PocketMain.Java
NoSuchCoin.java
Here is the code I have so far, it works, but I need to program to continue even after I put in an invalid type or number. I do not know where to put the exception. It is meant to have coins added to it, subtracted from it, the sum of coins added up, number of coins added up and all outputted. Here is my code:
public abstract class Coin
{
private String name;
//takes in coin name as part of constructor and stores it in a private string
public static final String DIME = "DIME";
public static final String NICKLE = "NICKLE";
public static final String PENNY = "PENNY";
public static final String QUARTER = "QUARTER";
//separate string constants for each coin
public String getName()
{
return name;
}
//method that returns coin's name
public void setName(String name)
{
this.name = name;
}
//sets the name for a coin
public Coin()
{
}
//blank class method
public Coin(String name)
{
this.name = name;
}
//coin constructor
public abstract int getValue();
//abstract getValue method
}
///////////////////////////////////////////
// Ramya Raja
// CSCI 1302
// Project 2
// November 10th 2018
///////////////////////////////////////////
public class Dime extends Coin
{
private int value = 1;
//private variable defining value of the coin
public void setValue(int value)
{
this.value = value;
}
public String getName()
{
return super.getName();
}
public void setName(String name)
{
super.setName(name);
}
public Dime()
{
super.setName(Coin.DIME);
}
public Dime(String name)
{
super(name);
}
public int getValue()
{
return value;
}
}
///////////////////////////////////////////
// Ramya Raja
// CSCI 1302
// Project 2
// November 10th 2018
///////////////////////////////////////////
public class Nickle extends Coin
{
private int value = 1;
public String getName()
{
return super.getName();
}
public void setName(String name)
{
super.setName(name);
}
public void setValue(int value)
{
this.value = value;
}
public Nickle()
{
super.setName(Coin.NICKLE);
}
public Nickle(String name)
{
super(name);
}
public int getValue()
{
return value;
}
}
///////////////////////////////////////////
// Ramya Raja
// CSCI 1302
// Project 2
// November 10th 2018
///////////////////////////////////////////
public class NoSuchCoin extends Exception
{
private String stringParameter;
public NoSuchCoin(String stringParameter)
{
super(stringParameter);
System.out.println(stringParameter);
}
}
///////////////////////////////////////////
// Ramya Raja
// CSCI 1302
// Project 2
// November 10th 2018
///////////////////////////////////////////
import java.util.ArrayList;
import java.util.Iterator;
public class Pocket
{
private ArrayList list;
public ArrayList getList()
{
return list;
}
public void setList(ArrayList list)
{
this.list = list;
}
public Pocket()
{
this.list = new ArrayList();
//
}
public void addCoin(Coin coin)
{
this.list.add(coin);
}
public void removeCoin(String type)
{
ArrayList coins = this.list;
Iterator iter = coins.iterator();
while(iter.hasNext())
{
Coin coin = iter.next();
if(coin.getName().equalsIgnoreCase(type))
{
iter.remove();
break;
}
}
this.list = coins;
}
public int countCoins(String type)
{
int count = 0;
for (Coin c : this.list)
{
if(c.getName().equalsIgnoreCase(type))
{
count++;
}
}
return count;
}
public double totalValue(){
double d = 0; //dimes
double n = 0; //nickles
double p = 0;//pennys
double q = 0;//quarters
for(Coin c:this.list)
{
if(c.getName().equalsIgnoreCase(Coin.DIME))
d++;
if(c.getName().equalsIgnoreCase(Coin.NICKLE))
n++;
if(c.getName().equalsIgnoreCase(Coin.PENNY))
p++;
if(c.getName().equalsIgnoreCase(Coin.QUARTER))
q++;
}
double totalValue = (d*.10)+(n*.05)+(p*.01)+(q*.25); // value in cents
return totalValue;
}
}
///////////////////////////////////////////
// Ramya Raja
// CSCI 1302
// Project 2
// November 10th 2018
///////////////////////////////////////////
import java.util.Scanner;
public class PocketMain
{
public static void main (String args[]) throws NoSuchCoin
{
Pocket pocket = new Pocket();
boolean exit = false;
while(!exit)
{
System.out.println("Choose one operation: "
+ "1. Add a coin (must specify the type) "
+ "2. Remove a coin (must specify type) "
+ "3. Display a coin count "
+ "4. Display a total value in the pocket "
+ "5. Exit the program");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter a number representing a type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
int selection = input.nextInt();
switch(selection)
{
case 1:pocket.addCoin( new Dime());
System.out.println("You added a dime to your pocket ");
break;
case 2:pocket.addCoin(new Nickle());
System.out.println("You added a nickle to your pocket ");
break;
case 3:pocket.addCoin(new Penny());
System.out.println("You added a penny to your pocket ");
break;
case 4:pocket.addCoin(new Quarter());
System.out.println("You added a quarter to your pocket ");
break;
default: throw new NoSuchCoin("Sorry, we don't have a coin associated with that number, please try again ");
}
case 2:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =input.nextInt();
switch(selection)
{
case 1:pocket.removeCoin(Coin.DIME);
System.out.println("You removed a dime from your pocket ");
break;
case 2:pocket.removeCoin(Coin.NICKLE);
System.out.println("You removed a nickle from your pocket ");
break;
case 3:pocket.removeCoin(Coin.PENNY);
System.out.println("You removed a penny from your pocket ");
break;
case 4:pocket.removeCoin(Coin.QUARTER);
System.out.println("You removed a quarter from your pocket ");
break;
default:throw new NoSuchCoin("Sorry, we do not have a coin associated with that number");
}
break;
case 3:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =input.nextInt();
switch(selection){
case 1:System.out.println("Dimes:"+pocket.countCoins(Coin.DIME));
break;
case 2:System.out.println("Nickles:"+pocket.countCoins(Coin.NICKLE));
break;
case 3:System.out.println("Pennys:"+pocket.countCoins(Coin.PENNY));
break;
case 4:System.out.println("Quarters:"+pocket.countCoins(Coin.QUARTER));
break;
default:throw new NoSuchCoin("Sorry, that is not a valid input.");
}
break;
case 4:
System.out.println("Total value of the Pocket: $ " + pocket.totalValue() + " ");
break;
case 5:
exit=true;
break;
default : System.out.println("Invalid input exiting program");
exit=true;
}
}
}
}
/////////////////////////////////////////// // Ramya Raja // CSCI 1302 // Project 2 // November 10th 2018 /////////////////////////////////////////// public class Penny extends Coin { private int value; public void setValue(int value) { this.value = value; } public String getName() { return super.getName(); } public void setName(String name) { super.setName(name); } public Penny() { super.setName(Coin.PENNY); } public Penny(String name) { super(name); } public int getValue() { return value; } }
/////////////////////////////////////////// // Ramya Raja // CSCI 1302 // Project 2 // November 10th 2018 ///////////////////////////////////////////
public class Quarter extends Coin{
private int value=1; public String getName() { return super.getName(); }
public void setName(String name) { super.setName(name); }
public void setValue(int value) { this.value = value; }
public Quarter() { super.setName(Coin.QUARTER); } public Quarter(String name) { super(name); } public int getValue() { return value; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
