Assignment No 1 C#: Creating your own class and calling it from another class Du
ID: 3721962 • Letter: A
Question
Assignment No 1 C#: Creating your own class and calling it from another class Due Date: March 2nd 2018 Submission: printout source properly identified and comments of your paragraphs upload your C# assignment properly identified (yourname+ass#) via moodle) Problem Create a Money class that has as data member’s dollars and cents. Include IncrementMoney and Decrement.Money instance methods. Include constructors that enable the Money class to be instantiated with a single value representing the full dollar/cent amount as well as a constructor that enables you to create an instance of the class by sending in two separate integer values for representing the dollar and cent amounts. Also include an instance method that returns as a string the number of dollars, quarters, nickels, dimes, and pennies. Override the ToString ( ) method to return the monetary amount formatted with currency symbols. Create a second class to test your Money class.
SAMPLE OF THE RUN FOR TWO AMOUNT
First line should display “Welcome to the money exchange by << your name >>
Original Amount: Dollars: 5 Cents: 36 Enter decrement amount. Enter full dollar and cent amount: 3.e0 Original Amount: $5.36 Decrement Test: Amount to Subtract: 3.e0 New Dollar Amount: 2.ee New Cent Amount: .36 New Amount: $2.36 Press any key to see the next test.. .. . . Second Test: Next Money Value: $4.e2 Enter Increment amount. Enter full dollar and cent amount: 5e Second Test: Original Amount: $4.02 Increment Test: Amount to Add: 5e.ee New Amount: $54.02 Press any key to see the next test......Explanation / Answer
Hi,
Below is the C# program for all the four test.Please run the program and let me know in case of any questions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Welcome to the money exchange by ABC");
Console.WriteLine();
Console.WriteLine("First Test");
Console.WriteLine("Enter Original Amount in Dollars and Cents: ");
double amount = Convert.ToDouble(Console.ReadLine());
Money money = new Money(amount);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Enter Decrement Amount.");
Console.WriteLine("Enter full dollar and cent amount: ");
double decAmount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
Console.WriteLine();
Console.Write("Original Amount: ");
Console.WriteLine(money.ToString());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Decrement Test:");
Console.Write("Amount to Subtract: ");
Console.WriteLine(decAmount);
Money decMoney = money.DecrementMoney(decAmount);
Console.Write("New Dollar amount: ");
Console.WriteLine(decMoney.getDollars());
Console.Write("New Cent amount: ");
Console.WriteLine(decMoney.getCents());
Console.Write("New Amount: ");
Console.WriteLine(decMoney.ToString());
Console.WriteLine("Press Any Key to See Next Test..");
Console.Read();
//------First Test Ends Here ------------
//------------Second Test Starts ------------
Console.WriteLine("Second Test");
Console.Write("Next Money Value: ");
Console.WriteLine(decMoney.ToString());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Enter Incement Amount.");
Console.WriteLine("Enter full dollar and cent amount: ");
double incAmount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
Console.WriteLine();
Console.Write("Original Amount: ");
Console.WriteLine(decMoney.ToString());
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Increment Test:");
Console.Write("Amount to Increment: ");
Console.WriteLine(incAmount);
Money incMoney = decMoney.IncrementMoney(incAmount);
Console.Write("New Dollar amount: ");
Console.WriteLine(incMoney.getDollars());
Console.Write("New Cent amount: ");
Console.WriteLine(incMoney.getCents());
Console.Write("New Amount: ");
Console.WriteLine(incMoney.ToString());
Console.WriteLine("Press Any Key to See Next Test..");
Console.Read();
//---------------Second Test Ends Here ----------
//-----------------Third Test Starts-------------
Console.WriteLine("Third Test - input dollar and cent as separate value: ");
Console.Write("Enter Dollars: ");
int dollar = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Cents: ");
int cent = Convert.ToInt32(Console.ReadLine());
money = new Money(dollar,cent);
Console.WriteLine();
Console.Write("Full Dollar Amount: ");
Console.WriteLine(money.ToString());
Console.WriteLine("Press Any Key to See Next Test..");
Console.Read();
//-----------Third Test ENds Here--------------
//------------Fourth Test Starts---------------
Console.WriteLine("Last Test - input as a single value");
Console.Write("Enter full dollar and cent amount: ");
amount = Convert.ToDouble(Console.ReadLine());
money = new Money(amount);
Console.Write("Full Dollar Amount: ");
Console.WriteLine(money.ToString());
}
}
class Money
{
private int dollars;
private int cents;
public Money(int dollars, int cents)
{
this.dollars = dollars;
this.cents = cents;
}
public Money(double value)
{
dollars = (int) value;
double fractionalPart = value - dollars;
cents = (int)(fractionalPart*100);
}
public int getDollars()
{
return dollars;
}
public int getCents()
{
return cents;
}
public Money IncrementMoney(double value)
{
double input = this.dollars + this.cents/100;
double result = input + value;
return new Money(result);
}
public Money DecrementMoney(double value)
{
double input = this.dollars + this.cents/100;
double result = input - value;
return new Money(result);
}
public string PrintDollars()
{
return "You have " + this.dollars + " Dollars, " + this.cents/25 + " Quarters, " + this.cents/5 + " Nickels, " + this.cents/10 + " Dimes and " + this.cents + " Pennies.";
}
public string ToString()
{
return "$"+this.dollars+"."+this.cents;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.