Question

Money Lab using arraylists in Java Using the Coin class below create a program with the...

Money Lab using arraylists in Java

Using the Coin class below create a program with the following requirements

  • Create an arraylist that holds the money you have in your wallet.

  • You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist.

  • Program must include a loop that asks you to purchase items using the coins in your wallet.

    • When purchasing items, the program will remove coins from the arraylist and then add new coins based on the change that you receive until you are done buying or out of money.

  • Finally the program should total the amount of money that you have left

  • Please include methods as appropriate

Coin Object

public class Coin

{

   private int side;

   private double value;

   private String name;

    public Coin()

   {

       side = 1;

       value = 0.25;

       name = "Quarter";

   }

   public Coin(String iname, double ivalue)

   {

       name = iname;

       value = ivalue;

   }


   public String getName()

   {

       return name;

   }

   public double getValue()

   {

       return value;

   }

   public void setName(String iname)

   {

       name = iname;

   }

   

   public void setValue(double ivalue)

   {

       value = ivalue;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File: MoneyLab.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MoneyLab
{

private List<Coin> Wallet;
public MoneyLab()
{
Wallet = new ArrayList<>();
}

double getTotalWalletValue()
{
double value = 0;
for (int i = 0; i < Wallet.size(); i++)
{
value += Wallet.get(i).getValue();
}
return value;
}

void LoadMoneyInWallet()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter coin name you want to add: ");
String name = scanner.nextLine();
System.out.println("Enter coin value you just added: ");
double value = scanner.nextDouble();
Coin c = new Coin(name, value);
Wallet.add(c);
}

void MakePurchaseComplete(double value)
{
// total amount is sufficient!!
double wallet_value = 0;
int i = 0;
for ( ; i < Wallet.size(); i++)
{
wallet_value += Wallet.get(i).getValue();
if (wallet_value > value)
break;
}
for (int j = 0; j < i+1; j++)
Wallet.remove(0);
// Create a new coin with left over value:
double coin_value = wallet_value - value;
Coin c = new Coin("custom-coin", coin_value);
Wallet.add(c);
}

void PurchaseItem()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the cost of item you want to purchase: ");
double value = scanner.nextDouble();
// make sure the total money is sufficient to buy the item
double total = getTotalWalletValue();
if (value > total)
{
System.out.println("Cannot continue with the purchase, you don't have sufficient balance");
return;
}
MakePurchaseComplete(value);
}

public static void main(String[] args)
{
int input = 1;
MoneyLab wallet = new MoneyLab();
Scanner scanner = new Scanner(System.in);
while (input > 0)
{
System.out.println("Please enter the operation to the wallet :\n1.) Loading the wallet \n2.) Purchase an item.\n0.) Keep the wallet back in the pocket!");
input = scanner.nextInt();
if (input > 2 || input < 0)
System.out.println("Invalid input. Please try again\n");
if (input == 1)
wallet.LoadMoneyInWallet();
if (input == 2)
wallet.PurchaseItem();
}
System.out.println("Thank you. Have a nice day. Net amount in your wallet is " + wallet.getTotalWalletValue());
}

}

File: Coin.java


public class Coin
{
private double value;
private String name;

public Coin(String iname, double ivalue)
{
name = iname;
value = ivalue;
}

public String getName()
{
return name;
}

public double getValue()
{
return value;
}

public void setName(String iname)
{
name = iname;
}

public void setValue(double ivalue)
{
value = ivalue;
}

}

Add a comment
Know the answer?
Add Answer to:
Money Lab using arraylists in Java Using the Coin class below create a program with the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Ive got this started but im really struggling its a c# program, I have to modify...

    Ive got this started but im really struggling its a c# program, I have to modify the class  Purse  to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works. using System; using System.Collections; namespace TestingProject {    /// <summary>    /// A coin with a monetary value.    /// </summary>    public class Coin   {        ///   Constructs a coin.        ///   @param aValue the monetary value of the coin       ...

  • Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin...

    Project 2 Tasks: 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...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of...

    (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of the array is you cannot add or delete elements from the array. The size is fixed and you can only do workarounds. An example is the following: public class Ex_7_5_Prep { public static void main(String[] args) { int[] intArray = { 5, 10, 15, 20 }; printArray(intArray); intArray = addNewElement(intArray, 25); printArray(intArray); } public static int[] addNewElement(int[] originalArray, int newInt) { // Create new...

  • 4. Create a Business class. This class should store an array of Employee objects. Add a...

    4. Create a Business class. This class should store an array of Employee objects. Add a method to add employees to the Business, similar to addFoodItem in the Meal class. Add two more methods for the Business class: getHighestPaid(), which returns the Employee who has been paid the most in total, and getHighestTaxed(), which returns the Employee who has been taxed the most in total. Note: you do not need to handle the case of ties. These are all the...

  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT