Create a Coin.java class that includes the following: Takes in a coin name as pa
ID: 3805715 • Letter: C
Question
Create a Coin.java class that includes the following:
Takes in a coin name as part of the constructor and stores it in a private string
Has a method that returns the coins name
Has an abstract getvalue method
Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:
A constructor that passes the coins name to the parent
A private variable that defines the value of the coin
A method that returns the coins name via the parent class
And any other methods that should be required.
Create a Pocket class that includes the following:
An ArrayList of type coin to manage coins.(You MUST use an ARRAYLIST)
A method that allows a coin to be added
A method that allows a coin to be removed
A method that returns a count of the coins in your arraylist by coin type.
A method that returns a total value of all the coins in your arraylist
Create a NoSuchCoin class that will act as your custom exception (see pg.551 in book).
Create a PocketMain class that includes the following :
Creates an object of type pocket to be used below
A main method that prompts the user with a menu of options to include:
Add a coin (must specify type)
Remove a coin (must specify type)
Display a coin count
Display a total value in the pocket
An ability to exit the program
This method must handle poor user input somehow using your custom exception
Explanation / Answer
/*
* i hope most of the code meets your requirement ,please feel free to comment for any
* doubts or change request in code ,Thank You and all the best :-)
*/
package com;
public abstract class Coin {
private String name;
public static final String DIME="DIME";
public static final String NICKLE="NICKLE";
public static final String PENNY="PENNY";
public static final String QUARTER="QUARTER";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Coin() {
}
public Coin(String name) {
this.name=name;
}
public abstract int getValue();
}
//****************************************************************************************************
package com;
public class Dime extends Coin{
private int value=1;
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);
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
//*************************************************************************************************************
package com;
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);
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
//*******************************************************************************************************
package com;
public class NoSuchCoin extends Exception{
public NoSuchCoin() {
System.out.println("No such class exists");
}
}
//*****************************************************************************************************
package com;
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);
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
//*******************************************************************************************************
package com;
import java.util.ArrayList;
import java.util.Iterator;
public class Pocket {
private ArrayList<Coin> list;
public ArrayList<Coin> getList() {
return list;
}
public void setList(ArrayList<Coin> list) {
this.list = list;
}
public Pocket() {
this.list=new ArrayList<Coin>();
// TODO Auto-generated constructor stub
}
public void addCoin(Coin coin){
this.list.add(coin);
}
public void removeCoin(String type){
ArrayList<Coin> coins = this.list;
Iterator<Coin> iter = coins.iterator();
while (iter.hasNext()) {
Coin coin = iter.next();
if (coin.getName().equalsIgnoreCase(type))
{
iter.remove();
break;
}
}
this.list=coins;
}
//public int[] countCoins()
public int countCoins(String type)
{
int count=0;
for(Coin c:this.list)
{
if(c.getName().equalsIgnoreCase(type))
{
count++;
}
}
return count;
// int d=0; //dimes
// int n=0; //nickles
// int p=0;//pennys
// int 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++;
//
// }
// int [] coins = {d,n,p,q};
// return coins;
}
public float totalValue(){
float d=0; //dimes
float n=0; //nickles
float p=0;//pennys
float 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++;
}
float totalValue = (d*10)+(n*5)+(p)+(q*25); // value in cents
return totalValue;
}
}
//****************************************************************************************************
package com;
import java.util.Scanner;
public class PocketMain {
public static void main(String args[]) throws NoSuchCoin
{
Pocket pocket = new Pocket();
boolean exit =false;
while(!exit)
{
/*
* 1.Add a coin (must specify type)
2.Remove a coin (must specify type)
3.Display a coin count
4.Display a total value in the pocket
5.An ability to exit the program
*/
System.out.println("Choose one operation: "
+ "1.Add a coin (must specify 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 sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
int selection =sc.nextInt();
switch(selection){
case 1:pocket.addCoin( new Dime());
System.out.println("dime added to pocket successfully");
break;
case 2:pocket.addCoin(new Nickle());
System.out.println("Nickle added to pocket successfully");
break;
case 3:pocket.addCoin(new Penny());
System.out.println("Penny added to pocket successfully");
break;
case 4:pocket.addCoin(new Quarter());
System.out.println("Quarter added to pocket successfully");
break;
default:throw new NoSuchCoin();
}
break;
case 2:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =sc.nextInt();
switch(selection){
case 1:pocket.removeCoin(Coin.DIME);
System.out.println("Dime removed from pocket successfully");
break;
case 2:pocket.removeCoin(Coin.NICKLE);
System.out.println("Nickle removed from pocket successfully");
break;
case 3:pocket.removeCoin(Coin.PENNY);
System.out.println("Penny removed from pocket successfully");
break;
case 4:pocket.removeCoin(Coin.QUARTER);
System.out.println("Quarter removed from pocket successfully");
break;
default:throw new NoSuchCoin();
}
break;
case 3:
System.out.println("Enter type of coin "
+ "1.Dime "
+ "2.Nickle "
+ "3.Penny "
+ "4.Quarter");
selection =sc.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();
}
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;
}
}
}
}
//*************************************************************************************************************
package com;
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);
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
//***********************************************************************************************