Program not ending correctly
Code :
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Assignment1
{
public static CheckingAccount info;
public static void main(String[] args)
{
String balance, message;
double ibalance;
boolean above = true;
DecimalFormat formatter = new DecimalFormat("#0.00");
int tcode = 1;
double tamount;
balance = JOptionPane.showInputDialog ("Enter your initial balance: ");
ibalance = Double.parseDouble(balance);
info = new CheckingAccount(ibalance);
if (info.getBalance() < 500)
above = false;
while(tcode != 0)
{
tcode = getTransCode();
if (tcode == 1 )
{
tamount = getTransAmt();
info.setBalance(tamount, tcode);
info.setServiceCharge(tcode);
if(info.getBalance() <500 && above ==true)
{
info.setServiceCharge(1.0);
processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
above = false;
}
else if (info.getBalance() <0 && above == false)
{
info.setServiceCharge(2.0);
processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
}
else if (info.getBalance() <0 && above == true)
{
info.setServiceCharge(2.0);
processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
above = false;
}
else
{
processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above);
}
}
else if (tcode ==2)
{
tamount = getTransAmt();
info.setBalance (tamount, tcode);
info.setServiceCharge(tcode);
processDeposit (tcode, info.getBalance(), tamount, info.getServiceCharge());
}
}
if (tcode ==3)
{
message = "End of Transaction" +
"\n" +
"\nTotal balance: $" + formatter.format(info.getBalance()) +
"\nTotal service charge: $" + formatter.format(info.getServiceCharge()) +
"\nFinal Balance: $" + formatter.format(info.EndBalance());
JOptionPane.showMessageDialog (null, message);
}
}
public static int getTransCode()
{
int tcode;
String transcode;
do
{
transcode = JOptionPane.showInputDialog ("Enter your transaction" +
" code:" +
"\n1 For Check" +
"\n2 For Deposit" +
"\n3 To Exit Program" );
tcode = Integer.parseInt(transcode);
}while (tcode >2 || tcode < 0);
return tcode;
}
public static double getTransAmt()
{
double tamount;
String transamount;
transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
tamount = Double.parseDouble(transamount);
return tamount;
}
public static void processCheck(int Transcode, double Balance, double TransAmt, double totalServiceCharge, boolean above)
{
DecimalFormat formatter = new DecimalFormat("#0.00");
String message;
if (Balance >= 500)
{
message = "Transaction: Check in the amount of $" + TransAmt+
"\n" +
"\nCurrent balance: $" + formatter.format(info.getBalance()) +
"\nService Charge: Check --- charge $0.15" +
"\nService Charge: Below $500 --- charge 5.00" +
"\nTotal service charge:" + formatter.format(info.getServiceCharge());
JOptionPane.showMessageDialog (null,message);
}
else if (Balance < 500 && Balance > 0 && above == true)
{
message = "Transaction: Check in the amount of $" + TransAmt+
"\n" +
"\nCurrent balance: $" + formatter.format(info.getBalance()) +
"\nService Charge: Check --- charge $0.15" +
"\nService Charge: Below $500 --- charge 5.00" +
"\nTotal service charge:" + formatter.format(info.getServiceCharge());
JOptionPane.showMessageDialog (null, message);
}
else if (Balance < 500 && Balance > 0 && above == false)
{
message = "Transaction: Check in the amount of $" + TransAmt+
"\n" +
"\nCurrent balance: $" + formatter.format(info.getBalance()) +
"\nService Charge: Check --- charge $0.15" +
"\nTotal service charge:" + formatter.format(info.getServiceCharge());
JOptionPane.showMessageDialog (null, message);
}
else
{
message = "Transaction: Check in the amount of $" + TransAmt+
"\n" +
"\nCurrent balance: $" + formatter.format(info.getBalance()) +
"\nService Charge: Check --- charge $0.15" +
"\nService Charge: Below $0 --- charge 10.00" +
"\nTotal service charge:" + formatter.format(info.getServiceCharge());
JOptionPane.showMessageDialog (null, message);
}
}
public static void processDeposit(int Transcode, double Balance, double TransAmt, double totalServiceCharge)
{
String message;
DecimalFormat formatter = new DecimalFormat("#0.00");
message = "Transaction: Deposit in the amount of $" + TransAmt+
"\n" +
"\nCurrent balance: $" + formatter.format(info.getBalance()) +
"\nService Charge: Deposit --- charge $0.10" +
"\nTotal service charge:" + formatter.format(info.getServiceCharge());
JOptionPane.showMessageDialog (null, message);
}
}
Code :
public class CheckingAccount
{
private double balance;
private double totalServiceCharge = 0;
public CheckingAccount(double number)
{
balance = number;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tcode)
{
if(tcode == 1)
{
balance -= transAmt;
}
else if(tcode == 2)
{
balance += transAmt;
}
}
public double EndBalance()
{
balance = balance - totalServiceCharge;
return balance;
}
public double CurrentBalance()
{
balance = balance - totalServiceCharge;
return balance;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(int tcode)
{
if (tcode ==1)
{
totalServiceCharge = totalServiceCharge + 0.15;
}
else if (tcode ==2)
{
totalServiceCharge = totalServiceCharge + 0.10;
}
}
public void setServiceCharge(double sale)
{
if (sale == 1)
{
totalServiceCharge = totalServiceCharge + 5.00;
}
else if (sale ==2)
{
totalServiceCharge = totalServiceCharge + 10.00;
}
}
}
So as you can see, if the user inputs 3 into tcode, the program is supposed to print the last transaction invoice and end the loop/program.
However, when the user inputs 3, the program does not end, and does not print the last transaction.
Re: Program not ending correctly
Quote:
Originally Posted by
alex067
..However, when the user inputs 3, the program does not end, and does not print the last transaction.
The main() method needs to get a value of 3 from getTransCode() in order to terminate the program the way you have in mind, right?
Then:
Look at the method that gives the transaction code to main():
Code java:
public static int getTransCode()
{
int tcode;
String transcode;
do
{
transcode = JOptionPane.showInputDialog ("Enter your transaction" +
" code:" +
"\n1 For Check" +
"\n2 For Deposit" +
"\n3 To Exit Program" );
tcode = Integer.parseInt(transcode);
} while (tcode > 2 || tcode < 0);
return tcode;
}
What happens when you enter a value of 3? How can it ever leave that loop?
Cheers!
Z
Re: Program not ending correctly
Quote:
Originally Posted by
Zaphod_b
The main() method needs to get a value of 3 from getTransCode() in order to terminate the program the way you have in mind, right?
Then:
Look at the method that gives the transaction code to main():
Code java:
public static int getTransCode()
{
int tcode;
String transcode;
do
{
transcode = JOptionPane.showInputDialog ("Enter your transaction" +
" code:" +
"\n1 For Check" +
"\n2 For Deposit" +
"\n3 To Exit Program" );
tcode = Integer.parseInt(transcode);
} while (tcode > 2 || tcode < 0);
return tcode;
}
What happens when you enter a value of
3? How can it ever leave that loop?
Cheers!
Z
So how should the while statement be constructed in order to make tcode = 3 to input the last of the transaction and end the program?
Re: Program not ending correctly
For what values of tcode do you want to leave the loop? Write them down. Write all of them down.
For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?
Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();
Cheers!
Z
Re: Program not ending correctly
Quote:
Originally Posted by
Zaphod_b
For what values of tcode do you want to leave the loop? Write them down. Write all of them down.
For what values of tcode do you want to stay in the loop? Don't try to write all of them down. They are all of the values except the three values that you wrote, right? Can you think of a way to express these values with a mathematical expression involving ">" and "<" and logical operators?
Now write a logic expression that is true for all values for which you want to stay in the loop. That's what goes into the parentheses at the end of do{}while();
Cheers!
Z
Values of tcode that would end the loop would just be 3
Values for tcode to stay in the loop would be 1,2
So I would do, }while (tcode > 0 || tcode < 3) ? So for every tcode greater than 0 (to make the tcode choice of 1 and 2 valid) and for every tcode less than 3 (to make tcode choice of 1 and 2 valid)
Isnt the argument inside the while loop have to be false in order to return the value? For example, }while (statement), if the statement = false, then the loop would continue, and if the statement = true, the loop would end?
Re: Program not ending correctly
Quote:
Originally Posted by
alex067
Values of tcode that would end the loop would just be 3
That's just wrong.
...
If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)
Quote:
Originally Posted by
alex067
Isnt the argument inside the while loop have to be false in order to return the value?
I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.
Cheers!
Z
Re: Program not ending correctly
Quote:
Originally Posted by
Zaphod_b
That's just wrong.
...
If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)
I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.
Cheers!
Z
So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:
}while (tcode > 0 || tcode < 3);
this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
If this is true, wouldn't any value greater than 3 terminate the program?
Re: Program not ending correctly
Quote:
Originally Posted by
Zaphod_b
That's just wrong.
...
If the value of tcode is 1 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 2 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is 3 do you want to stay in the loop or exit? (Answer: Terminate the loop and return the value of tcode)
If the value of tcode is less than 1 do you want to stay in the loop or exit? (Answer: Stay in the loop and let the user try again)
If the value of tcode is greater than 3 do you want to stay int he loop or exit? (Answer: Stay in the loop and let the user try again)
I think you are getting it. I would put it like this, which is what I think you mean:
If the value of the expression inside the parentheses is true, the loop continues. If the value is false, the loop terminates, and the function returns the value of tcode.
Cheers!
Z
So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:
}while (tcode > 0 || tcode < 3);
this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
If this is true, wouldn't any value greater than 3 terminate the program?
Re: Program not ending correctly
Quote:
Originally Posted by
alex067
}while (tcode > 0 || tcode < 3);
this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
Did you try it? Is that really what the statement means? Try running the statement with -1, 0, 1, 2, 3, 4 and see what happens.
Re: Program not ending correctly
Quote:
Originally Posted by
alex067
So if I wanted tcode 3 to terminate the program, wouldn't my while statement be:
}while (tcode > 0 || tcode < 3);
this statement means, if tcode is greater than 0, and tcode is less than 3, continue the loop?
That code implies the following:
If tcode is greater than 0, continue the loop.
If tcode is less than 3, continue the loop.
For any other range of values, terminate the loop.
That is really, really not what you want, right?
What you want is:
If tcode is less than zero continue the loop.
If tcode is greater than 3 continue the loop.
As jps says: If you get an idea that you think might work, try it. I can't see any point in asking whether might work. Try it!
Try it with each and every one of the values for which you want to terminate the loop: 1, 2, 3
Try it with various values other than these to make sure it stays in the loop and gives the user a chance to try again. Try some smaller numbers. Try some larger numbers.
Cheers!
Z