Saturday, 14 September 2013

(Java) Looking for advise on the layout and process I have chosen for this task?

(Java) Looking for advise on the layout and process I have chosen for this
task?

I hope this is in the right place, if not my apologies but could someone
point me in a better direction? I've been working on this problem for 3
days now, Only 6 weeks into my first university class for any programming
at all and the first language we're learning is Java.
The problem is as follows:
Exercise Two (Another algorithm with sub modules)
Design an algorithm which will:
-Input the cost of a product and the amount the customer has tendered for
payment. you may assume that the payment is always > cost. You may also
assume that the cost and the amount paid are always in increments of 5
cents.
The algorithm should:
-Calculate the amount of change required.
-Determine the notes and coins to be given to the customer. -Output both
to the user.
I have made multiple attempts at this and scrapped my code multiple times,
I'm not looking for a solution from anyone, I've put enough into this to
hold out for that, but I am after some commentary on whether or not I am
going in right direction or if not a pointer as to where, also any comment
on the comments I've added to the code would be greatly appreciated.
Thanks for even taking the time to read this and a huge thanks to anyone
that can give me a hand. Cheers everyone
import io.*;
public class CoinChange
{
public static void main (String [] args)
{
double tenderValue;
// How do I make tenderValue and tenderValueInCents equal to multiple
variables and then allow these variables to be added into submodule as I
require?
// Do I need to create a submodule just for this? I attempted to make
multiple lines all equalling the variable name but it didnt seem to like
that either.
tenderValue = int (100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05);
tenderValueInCents = int (10000, 5000, 2000, 1000, 500, 200, 100, 50,
20, 10, 5);
change = calcChange(cashPayed, itemCost);
System.out.println(+change +"total change back");
changeInCents = calcChangeInCents(cashPayed, itemCost);
noteFinder (100, 10000); //Is this enough information required to feed
the variable required into this specific submodule?
noteFinderTemp (50, 5000); //Have a feeling I need to add in all the
variables used in the submodules as well?
noteFinderTemp (20,2000);
noteFinderTemp (10,1000);
noteFinderTemp (5,500);
coinFinder (2,200);
coinFinder (1,100);
coinFinder (0.5,50);
coinFinder (0.2,20);
coinFinder (0.1,10);
coinFinder (0.05,5);
}
private static calcChange(double cashPayed, double itemCost, double
change);
{
// I am not actually required to add in the checking to see if the
values are valid lines I just wanted to see if i could but after two
days at this its starting to drive me up the wall and if theres a
lot more that needs to be done for this bit i can take it out and
come back to trying this after getting the program to compile.
cashPayed=ConsoleInput="Enter cash payed in dollars and cents (xx.xx)";
itemCost=ConsoleInput="Enter cost of item xx.xx)";
if cashPayed>itemCost;
change=cashPayed-itemCost;
return change;
else if cashPayed<itemCost;
requiredMoney=itemCost-cashPayed;
System.out.println("Please provide" +requiredMoney +"to complete
transaction.");
system.exit;
else if cashPayed==itemCost;
System.out.println("Thankyou, payment complete.");
system.exit;
}
private static calcChangeInCents(double cashPayed, cashPayedInCents
double itemCost, itemCostInCents double changeInCents);
{
cashPayedInCents=cashPayed*100;
itemCostInCents=itemCost*100;
changeInCents=cashPayedInCents-itemCostInCents;
return changeInCents;
}
private static noteFinder ( double tenderValue, tenderValueInCents,
changeInCents, tenderValueQuantity, tempChangeInCents);
{
tenderValueQuantity=changeInCents/tenderValueInCents;
tempChangeInCents=changeInCents%tenderValue;
if (tenderValueQuantity=0 && tempChangeInCents=0);
system.exit; //is this an adequete way to say if these values are
zero then end program?
else if tenderValueQuantity=>1;
//DO I NEED TO PUT THIS IN? maths.RoundDown(tenderValueQuantity); OR
ANOTHER WAY? LIKE *1000 AND /1000 OR TAKING THE RESULT OF % FROM ORIGINAL?
System.out.println(+tenderValueQuantity +"x $" +tenderValue
+"Notes Change");
else system.out.println("No $" +tendervalue +"Notes required");
if temptChangeInCents=!0;
return tempChangeInCents;
else system.exit;
}
private static noteFinderTemp ( double tenderValue,
tenderValueInCents, changeInCents, tenderValueQuantity,
tempChangeInCents);
{
tenderValueQuantity=tempChangeInCents/tenderValueInCents;
tempChangeInCents=tempChangeInCents%tenderValue;
// This above line^ is the one I am most concerned about, will the
tempChangeInCents override itself in order or will it cause all sorts of
random problems set out like this? If so I'm guessing that I will not be
able to complete this with general submodules subbing in multiple
variables and will have to create a submodule for every tendervalue? This
was my initial thought but hoped there would be a more elegant way to do
it.
if (tenderValueQuantity=0 && tempChangeInCents=0);
system.exit;
else if tenderValueQuantity=>1
System.out.println(+tenderValueQuantity +"x $" +tenderValue
+"Notes change")
else system.out.println("No $" +tendervalue +"Notes required")
if temptChangeInCents=!0 THEN
return tempChangeInCents
else system.exit;
}
private static coinFinder ( double tenderValue, tenderValueInCents,
changeInCents, tenderValueQuantity, tempChangeInCents);
{
tenderValueQuantity=tempChangeInCents/tenderValueInCents;
tempChangeInCents=tempChangeInCents%tenderValue;
if (tenderValueQuantity=0 && tempChangeInCents=0);
system.exit;
else if tenderValueQuantity=>1
System.out.println(+tenderValueQuantity +"x $" +tenderValue
+"Coins change")
else system.out.println("No" +tendervalue +"c coins required")
if temptChangeInCents=!0 THEN
return tempChangeInCents
else system.exit;
}
}'

No comments:

Post a Comment