first commit
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
Project: better-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
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 2.
|
||||
|
||||
Purpose of project: To illustrate conditional statements.
|
||||
How to start this project: Create one or more TicketMachine objects.
|
||||
|
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=cost
|
||||
comment0.target=TicketMachine(int)
|
||||
comment0.text=\n\ Create\ a\ machine\ that\ issues\ tickets\ of\ the\ given\ price.\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\ Check\ that\ the\ amount\ is\ sensible.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ printTicket()
|
||||
comment4.text=\n\ Print\ a\ ticket\ if\ enough\ money\ has\ been\ inserted,\ and\n\ reduce\ the\ current\ balance\ by\ the\ ticket\ price.\ Print\n\ an\ error\ message\ if\ more\ money\ is\ required.\n
|
||||
comment5.params=
|
||||
comment5.target=int\ refundBalance()
|
||||
comment5.text=\n\ Return\ the\ money\ in\ the\ balance.\n\ The\ balance\ is\ cleared.\n
|
||||
numComments=6
|
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* TicketMachine models a ticket machine that issues
|
||||
* flat-fare tickets.
|
||||
* The price of a ticket is specified via the constructor.
|
||||
* Instances will check to ensure that a user only enters
|
||||
* sensible amounts of money, and will only print a ticket
|
||||
* if enough money has been input.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
* Check that the amount is sensible.
|
||||
*/
|
||||
public void insertMoney(int amount)
|
||||
{
|
||||
if(amount > 0) {
|
||||
balance = balance + amount;
|
||||
}
|
||||
else {
|
||||
System.out.println("Use a positive amount rather than: " +
|
||||
amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
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 price.
|
||||
balance = balance - price;
|
||||
}
|
||||
else {
|
||||
System.out.println("You must insert at least: " +
|
||||
(price - balance) + " more cents.");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the money in the balance.
|
||||
* The balance is cleared.
|
||||
*/
|
||||
public int refundBalance()
|
||||
{
|
||||
int amountToRefund;
|
||||
amountToRefund = balance;
|
||||
balance = 0;
|
||||
return amountToRefund;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
#BlueJ package file
|
||||
objectbench.height=89
|
||||
objectbench.width=692
|
||||
package.editor.height=344
|
||||
package.editor.width=584
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=0
|
||||
package.numTargets=1
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=609
|
||||
readme.editor.width=798
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=661
|
||||
target1.editor.width=780
|
||||
target1.editor.x=53
|
||||
target1.editor.y=34
|
||||
target1.height=60
|
||||
target1.name=TicketMachine
|
||||
target1.naviview.expanded=false
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=120
|
||||
target1.x=190
|
||||
target1.y=110
|
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* A class that maintains information on a book.
|
||||
* This might form part of a larger application such
|
||||
* as a library system, for instance.
|
||||
*
|
||||
* @author (Insert your name here.)
|
||||
* @version (Insert today's date here.)
|
||||
*/
|
||||
class Book
|
||||
{
|
||||
// The fields.
|
||||
private String author;
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* Set the author and title fields when this object
|
||||
* is constructed.
|
||||
*/
|
||||
public Book(String bookAuthor, String bookTitle)
|
||||
{
|
||||
author = bookAuthor;
|
||||
title = bookTitle;
|
||||
}
|
||||
|
||||
// Add the methods here ...
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
Project: book-exercise
|
||||
Authors: To be set.
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 2.
|
||||
|
||||
Purpose of project: This project forms the basis of a set of exercises
|
||||
that can be found in Chapter 2.
|
||||
How to start this project: Create one or more Book instances.
|
||||
|
||||
USER INSTRUCTIONS: Complete the exercises in Chapter 2.
|
@@ -0,0 +1,29 @@
|
||||
#BlueJ package file
|
||||
objectbench.height=100
|
||||
objectbench.width=363
|
||||
package.editor.height=400
|
||||
package.editor.width=560
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=0
|
||||
package.numTargets=1
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=700
|
||||
readme.editor.width=900
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=70
|
||||
target1.editor.y=50
|
||||
target1.height=60
|
||||
target1.name=Book
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=110
|
||||
target1.x=150
|
||||
target1.y=120
|
@@ -0,0 +1,14 @@
|
||||
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
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
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.
|
@@ -0,0 +1,17 @@
|
||||
#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
|
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 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 2016.02.29
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
#BlueJ package file
|
||||
objectbench.height=89
|
||||
objectbench.width=706
|
||||
package.editor.height=327
|
||||
package.editor.width=598
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
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=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=619
|
||||
target1.editor.width=777
|
||||
target1.editor.x=69
|
||||
target1.editor.y=25
|
||||
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
|
Reference in New Issue
Block a user