Question

This needs to be done in Java. Please use basic Java coding at an intro level....

This needs to be done in Java. Please use basic Java coding at an intro level. THANK YOU FOR YOUR HELP! I WILL LEAVE A GOOD RATING!

  1. Design and implement MoneyTest and Money. Use the test-first approach, as we discussed, and document each method before implementing.
    Money
    - dollars : int
    - cents : int
    + Money (int, int)
    + getDollars()
    + getCents()
    + add (Money) : Money
    + subtract (Money) : Money
    + toString() : String
    + equals (Money) : boolean
    + compareTo (Money) : int
    • You may assume for now that negative money amounts are allowed.
    • You may assume different instance variables (e.g. internally, you may just keep track of total cents), as long as the methods behave as specified. Here is a partial solution based on our demo and discussion in class: MoneyTest.java and Money.java; missing are: second test for testComplexAdd (see bottom of MoneyTest.java), tests and implementations of subtract and compareTo.
    • The compareTo method compares two Money objects: the one that invokes the method and the one received as parameter, and returns
      • 0 if the two Money amounts are the same,
      • -1 if this Money object represents an amount less than that received as parameter, or
      • 1 if this Money object represents an amount greater than that received as parameter.
  2. Design and implement AccountTest and Account. Use the test-first approach, as we discussed, and document each method before implementing.
    Account
    - name : String
    - id : String
    - balance : Money
    + Account (String, String, Money)
    + deposit (Money)
    + withdraw (Money)
    + transfer (Account, Money)
    + toString() : String
    + equals (Account) : boolean
0 0
Add a comment Improve this question Transcribed image text
Answer #1


As per the problem statement I have solve the problem. Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


//Money.java
public class Money
{
    private long totalCents;

    //constructor money with attributed bollars and cents
    public Money(int dollars, int cents)
    {
        this.totalCents = dollars * 100L + cents;
    }

    //Constructor where initialise the attributes cents
    public Money(long cents)
    {
        this.totalCents = cents;
    }

    //getter method for dollars
    public int getDollars()
    {
        return (int) this.totalCents / 100;
    }

    //getter method for get cents
    public int getCents()
    {
        return (int) this.totalCents % 100;
    }

    //method to add money
    public Money add (Money money)
    {
        return new Money (this.totalCents + money.totalCents);
    }

    //method to substract money
    public Money subtract (Money money)
    {
        return new Money (this.totalCents - money.totalCents);
    }

    //to string method to get dollars
    public String toString()
    {
        String result = "$" + this.getDollars() + ".";

        if (this.getCents() < 10) {
            result += "0";
        }

        result += this.getCents();
        return result;
    }

    //method equals to compare the two mone attributes whether equal or not
    public boolean equals (Money other)
    {
        return (this.totalCents == other.totalCents);
    }

    //method to compare the value of two money
    public int compareTo (Money other)
    {
        if (this.totalCents == other.totalCents)
        {
            return 0;
        }
        else if (this.totalCents < other.totalCents)
        {
            return -1;
        }
        else if (this.totalCents > other.totalCents)
        {
            return 1;
        }

        return 99;
    }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//MoneyTest.java

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class MoneyTest
{
    private Money amount;

    // constructor for test class MoneyTest
    public MoneyTest()
    {
    }

    @Before
    public void setUp() {
        amount = new Money(0, 0);
    }

    @After
    public void tearDown() {
        amount = null;
    }

    // Test of Money objects.
    @Test
    public void testCreate()
    {
        assertEquals("Error in testCreate", 0, amount.getDollars());
        assertEquals("Error in testCreate", 0, amount.getCents());

        Money amount2 = new Money (4, 50);

        assertEquals("Error in testCreate", 4, amount2.getDollars());
        assertEquals("Error in testCreate", 50, amount2.getCents());

        Money amount3 = new Money (-4, -50);

        assertEquals("Error in testCreate", -4, amount3.getDollars());
        assertEquals("Error in testCreate", -50, amount3.getCents());

    }

    // Test of Money object to String.
    @Test
    public void testToString()
    {
        // First test
        Money amount = new Money (7, 55);
        String actual = amount.toString();
        String expected = "$7.55";
        assertTrue("Error in testToString", actual.equals(expected));

        // Second test
        Money amount2 = new Money (7, 5);
        String actual2 = amount2.toString();
        String expected2 = "$7.05";
        assertTrue("Error in testToString", actual2.equals(expected2));
    }

    // Test equality of money values
    @Test
    public void testEquality()
    {
        Money myCash = new Money (3, 75);
        Money yourCash = new Money (3, 75);

        assertTrue ("Error in testEquality", myCash.equals(yourCash));

        Money myAmount = new Money (50, 0);
        Money yourAmount = new Money (50, 0);

        assertTrue ("Error in testEquality", myAmount.equals(yourAmount));
    }

    // Test inequality of money values
    @Test
    public void testInequality()
    {
        Money myCash = new Money (3, 75);
        Money difftDollarsSameCents = new Money (4, 75);
        Money difftDollarsDifftCents = new Money (4, 80);
        Money sameDollarsDifftCents = new Money (3, 80);

        assertFalse ("Error in testInequality", myCash.equals(difftDollarsSameCents));
        assertFalse ("Error in testInequality", myCash.equals(difftDollarsDifftCents));
        assertFalse ("Error in testInequality", myCash.equals(sameDollarsDifftCents));
    }

    // Test addition of money values
    @Test
    public void testSimpleAdd()
    {
        Money amount2 = new Money (2, 30);
        Money amount3 = new Money (3, 50);

        Money actualAmount = amount2.add (amount3);
        Money expectedAmount = new Money (5, 80);

        assertTrue ("Error in testSimpleAdd", actualAmount.equals(expectedAmount));
       }

    // Test complex addition of two money values
    @Test
    public void testComplexAdd()
    {

        Money myCash = new Money (3, 50);
        Money yourCash = new Money (4, 50);

        Money expectedAmount = new Money (8, 0);

        Money actualAmount = myCash.add(yourCash);


        assertTrue ("Error in testComplexAdd", actualAmount.equals(expectedAmount));


        Money myCash2 = new Money (4, 60);
        Money yourCash2 = new Money (5, 80);

        Money expectedAmount2 = new Money (10, 40);

        Money actualAmount2 = myCash2.add(yourCash2);

        assertTrue ("Error in testComplexAdd", actualAmount2.equals(expectedAmount2));

    }

    @Test
    public void testSubtract()
    {
        Money cash1 = new Money (4, 50);
        Money cash2 = new Money (3, 50);

        Money expectedAmount = new Money (1, 0);

        Money actualAmount = cash1.subtract(cash2);

        assertTrue ("Error in testSubtract", actualAmount.equals(expectedAmount));

        Money cash3 = new Money (4, 70);
        Money cash4 = new Money (4, 50);

        Money expectedAmount2 = new Money (0, 20);

        Money actualAmount2 = cash3.subtract(cash4);

        assertTrue ("Error in testSubtract", actualAmount2.equals(expectedAmount2));
    }

    @Test
    public void testCompare()
    {
        Money testCash = new Money (3, 50);
        Money cash1 = new Money (3, 50);
        Money cash2 = new Money (4, 50);
        Money cash3 = new Money (2, 50);


        assertEquals ("Error in testCompare", 0, testCash.compareTo(cash1));

        assertEquals ("Error in testCompare", -1, testCash.compareTo(cash2));

        assertEquals ("Error in testCompare", 1, testCash.compareTo(cash3));
    }

}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Account.java
public class Account
{
    private String name;
    private String id;
    private Money balance = new Money(0, 0);


    //constructor for Account wiht attributes like name , id and balance
    public Account(String nameInput, String idInput, Money balanceInput)
    {
        this.name = nameInput;
        this.id = idInput;
        this.balance = balanceInput;
    }

    public Account(Account other)
    {
        this.name = other.name;
        this.id = other.id;
        this.balance = other.balance;
    }

    //deposit method will add money to taccount
    public void deposit(Money other)
    {
        if(other.compareTo(new Money (0, 0)) == 0 || other.compareTo(new Money (0, 0)) == 1)
        {
            this.balance = this.balance.add(other);
        }
    }

    //withdraw will withdraw the money from account
    public void withdraw(Money other)
    {
        if(other.compareTo(this.balance) == 0 || other.compareTo(this.balance) == -1)
        {
            this.balance = this.balance.subtract(other);
        }
    }

    //method to transfer the money
    public void transfer(Account other, Money cash)
    {
        this.balance = this.balance.add(cash);
        other.balance = other.balance.subtract(cash);
    }

    //to string method
    public String toString()
    {
        return "" + this.name + ", " + this.id + ", " + this.balance.toString();
    }

    //method to return the ID
    public String returnID()
    {
        return new String(id);
    }

    //boolean equal method to check if attributed of two accounts are equal or not.
    public boolean equals(Account other)
    {
        if(this.name.equals(other.name) && this.id.equals(other.id) && this.balance.equals(other.balance))
        {
            return true;
        }

        return false;
    }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//AccountTest.java
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//Test class for account
public class AccountTest
{
    private Account account;

    //default constructor for account test
    public AccountTest()
    {
    }

    @Before
    public void setUp()
    {
        account = new Account("Name", "001", new Money (0, 0));
    }

    @After
    public void tearDown()
    {
        account = null;
    }


    // test if money deposited into the account or not
    @Test
    public void testDeposit()
    {
        Money depositCash = new Money (5, 0);


        Account expectedResult = new Account("Name", "001", depositCash);
        account.deposit(depositCash);

        assertTrue ("Error in testDeposit", account.equals(expectedResult));
    }

    @Test
    public void testWithdraw()
    {
        Money withdrawCash = new Money (5, 0);
        Account account2 = new Account("Name", "001", new Money (10, 0));


        Account expectedResult = new Account("Name", "001", new Money (5, 0));
        account2.withdraw(withdrawCash);

        assertTrue ("Error in testWithdraw", account2.equals(expectedResult));
    }

    @Test
    public void testTransfer()
    {
        Money transferCash = new Money (5, 0);
        Account account2 = new Account("Name", "001", new Money (12, 0));

        account.transfer(account2, transferCash);


        Account expectedResult1 = new Account("Name", "001", new Money (5, 0));
        Account expectedResult2 = new Account("Name", "001", new Money (7, 0));

        assertTrue ("Error in testTransfer", account.equals(expectedResult1));
        assertTrue ("Error in testTransfer", account2.equals(expectedResult2));
    }

    @Test
    public void testToString()
    {
        String expectedResult = "Name, 001, $0.00";
        String actualResult = account.toString();


        assertTrue ("Error in testToString", expectedResult.equals(actualResult));
    }

    @Test
    public void testEquals()
    {
        Account account2 = new Account("Name", "001", new Money (0, 0));


        assertTrue ("Error in testEquals", account.equals(account2));

        Account account3 = new Account("Nam", "001", new Money (0, 0));
        Account account4 = new Account("Name", "00", new Money (0, 0));
        Account account5 = new Account("Name", "001", new Money (10, 0));


        assertFalse ("Error in testEquals", account.equals(account3));
        assertFalse ("Error in testEquals", account.equals(account4));
        assertFalse ("Error in testEquals", account.equals(account5));
    }

   
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program Output:



Add a comment
Know the answer?
Add Answer to:
This needs to be done in Java. Please use basic Java coding at an intro level....
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
  • in java please Purpose: To practice designing a class that can be used in many applications....

    in java please Purpose: To practice designing a class that can be used in many applications. To write and implement a thorough test plan. You are NOT to use any of the Java API date classes (Gregorian calendar, Date…) except for the constructor method to set the date fields to the current data. Write a class to hold data and perform operations on dates. Ie: January 2, 2019   or 3/15/2018 You must include a toString( ), an equals( ) and...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file:...

    help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file: UWECPerson.java uWECPerson.java UWECPerson uwecld: int firstName: String lastName : String +UWECPerson(uwecld: int, firstName : String, lastName: String) +getUwecid): int setUwecld(uwecld: int): void +getFirstName): String +setFirstName(firstName: String): void getLastName): String setLastName(lastName: String): void +toString): String +equals(other: Object): boolean The constructor, accessors, and mutators behave as expected. The equals method returns true only if the parameter is a UWECPerson and all instance variables are equal. The...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • Write in java and the class need to use give in the end copy it is...

    Write in java and the class need to use give in the end copy it is fine. Problem Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end). The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In...

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