• 0

Help


Question

Hello,

Could anybody help me with this university portfolio question please?

6.3 project better-ticket-machine:

Rewrite the printTicket method so that it declares a local variable, amountLeftToPay. This should then be initialized to contain the difference between price and balance.

Rewrite the test in the conditional statement to check the value of amountLeftToPay. If its value is less than or equal to zero, a ticket should be printed, otherwise an error message should be printed stating the amount still required. Test your version to ensure that it behaves correctly. (exercise 2.57 from Barnes and Kolling 4th edition)

Ive added the bit in bold already...... but dont know how to do the rest of the question..... im not looking for the answer to be posted but any help you could give me to help guide me there would be very gratefully recieved.....

/**

* Print a ticket if enough money has been inserted, and

* reduce the current balance by the ticket price. Print

* an error message if more money is required.

*/

public void printTicket()

{

int amountLeftToPay;

if(balance >= price) {

// Simulate the printing of a ticket.

System.out.println("##################");

System.out.println("# The BlueJ Line");

System.out.println("# Ticket");

System.out.println("# " + price + " cents.");

System.out.println("##################");

System.out.println();

// Update the total collected with the price.

total = total + price;

// Reduce the balance by the prince.

balance = balance - price;

}

else {

System.out.println("You must insert at least: " +

(price - balance) + " more cents.");

}

}

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

int amountLeftToPay;

'This should then be initialized to contain the difference between price and balance.'

amountLeftToPay = price - balance;

the next part asks,

Rewrite the test in the conditional statement to check the value of amountLeftToPay. If its value is less than or equal to zero, a ticket should be printed, otherwise an error message should be printed stating the amount still required.

you currently have

if(balance >= price) {...}

else{...}

it wants

if(amountLeftToPlay <=0){print ticket}

else{print amountLeftToPay}

pretty straight forward, This is teaching you how to read specifications properly.

Link to comment
Share on other sites

  • 0

public void printTicket()
{

double amountLeftToPay;
double price ;// some value fixed already
double balance;


// add your custom code for displaying the price and allowing user to input the amount given

if(balance == price)
{
//print ticket
}//if(balance == price)
else
{
amountLeftToPay = total - price;
if(amountLeftToPay &lt; = 0)
{
//print ticket
}//amountLeftToPay
else
//print amountLeftToPay
}
}//if(balance == price)


}

Hope helps

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.