question uploaded
naive-ticket-machine/package.bluej
#BlueJ package file objectbench.height=76 objectbench.width=785 package.editor.height=340 package.editor.width=687 package.editor.x=39 package.editor.y=65 package.numDependencies=0 package.numTargets=1 package.showExtends=true package.showUses=true project.charset=UTF-8 readme.editor.height=611 readme.editor.width=835 readme.editor.x=0 readme.editor.y=22 target1.editor.height=673 target1.editor.width=815 target1.editor.x=480 target1.editor.y=59 target1.height=60 target1.name=TicketMachine target1.naviview.expanded=true target1.showInterface=false target1.type=ClassTarget target1.typeParameters= target1.width=120 target1.x=170 target1.y=100
naive-ticket-machine/README.TXT
Project: naive-ticket-machine Authors: David Barnes and Michael Kölling This project is part of the material for the book Objects First with Java - A Practical Introduction using BlueJ Fifth edition David J. Barnes and Michael Kölling Pearson Education, 2012 It is discussed in chapter 2. Purpose of project: To illustrate the basics of fields, constructors, and methods. How to start this project: Create one or more TicketMachine objects.
naive-ticket-machine/TicketMachine.ctxt
#BlueJ class context comment0.params=cost comment0.target=TicketMachine(int) comment0.text=\n\ Create\ a\ machine\ that\ issues\ tickets\ of\ the\ given\ price.\n\ Note\ that\ the\ price\ must\ be\ greater\ than\ zero,\ and\ there\n\ are\ no\ checks\ to\ ensure\ this.\n comment1.params= comment1.target=int\ getPrice() comment1.text=\n\ Return\ the\ price\ of\ a\ ticket.\n comment2.params= comment2.target=int\ getBalance() comment2.text=\n\ Return\ the\ amount\ of\ money\ already\ inserted\ for\ the\n\ next\ ticket.\n comment3.params=amount comment3.target=void\ insertMoney(int) comment3.text=\n\ Receive\ an\ amount\ of\ money\ from\ a\ customer.\n comment4.params= comment4.target=void\ printTicket() comment4.text=\n\ Print\ a\ ticket.\n\ Update\ the\ total\ collected\ and\n\ reduce\ the\ balance\ to\ zero.\n numComments=5
naive-ticket-machine/TicketMachine.java
naive-ticket-machine/TicketMachine.java
/**
* TicketMachine models a naive ticket machine that issues
* flat-fare tickets.
* The price of a ticket is specified via the constructor.
* It is a naive machine in the sense that it trusts its users
* to insert enough money before trying to print a ticket.
* It also assumes that users enter sensible amounts.
*
*
@author
David J. Barnes and Michael Kölling
*
@version
2011.07.31
*/
public
class
TicketMachine
{
// The price of a ticket from this machine.
private
int
price
;
// The amount of money entered by a customer so far.
private
int
balance
;
// The total amount of money collected by this machine.
private
int
total
;
/**
* Create a machine that issues tickets of the given price.
* Note that the price must be greater than zero, and there
* are no checks to ensure this.
*/
public
TicketMachine
(
int
cost
)
{
price
=
cost
;
balance
=
0
;
total
=
0
;
}
/**
* Return the price of a ticket.
*/
public
int
getPrice
()
{
return
price
;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*/
public
int
getBalance
()
{
return
balance
;
}
/**
* Receive an amount of money from a customer.
*/
public
void
insertMoney
(
int
amount
)
{
balance
=
balance
+
amount
;
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public
void
printTicket
()
{
// 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 balance.
total
=
total
+
balance
;
// Clear the balance.
balance
=
0
;
}
}