Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
131 changes: 131 additions & 0 deletions BetterTicketMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* 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 2011.07.31
*/
public class BetterTicketMachine
{
// 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;
private int saving;
private int discount;
private int mean;
private int count;
private int budget;



/**
* Create a machine that issues tickets of the given price.
*/
public BetterTicketMachine(int ticketCost)
{
price = ticketCost;
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: " +
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()
{
int amountLeftToPay = price - balance;


if(amountLeftToPay < 0) {
// 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.");

}
saving = price * discount;
if(count > 0){
mean = total / count;
}
budget = 1000;
if(price > budget){
System.out.println("Too expensive " + budget);
}
else{
System.out.println("Just right");
}
}

public int emptyMachine(){
int resetTotal = total;
total= 0;
return resetTotal;
}



/**
* Return the money in the balance.
* The balance is cleared.
*/
public int refundBalance()
{
int amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
}
45 changes: 43 additions & 2 deletions TicketMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ public class TicketMachine
private int balance;
// The total amount of money collected by this machine.
private int total;

private int status;

private int score;

/**
* 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 ticketCost)
public TicketMachine()
{
price = ticketCost;
price = 1000;
balance = 0;
total = 0;
}
Expand Down Expand Up @@ -75,4 +79,41 @@ public void printTicket()
// Clear the balance.
balance = 0;
}
public int getTotal()
{
return total;
}
public void setPrice(int ticketCost)
{
this.price = ticketCost;
}
/**
* Increase score by the given number of points.
*/
public void increase(int points){
this.score = points + score;
}

/**
* Reduce price by the given amount.
*/
public void discount(int amount){
this.price = price - amount;
}

public void prompt(){
System.out.println("Please insert the correct amount of money");
}
public void showPrice(){
System.out.println("The price of a ticket is " + price +" cents.");
}
public void empty(){
total=0;
}
public TicketMachine(int price){

}



}
25 changes: 16 additions & 9 deletions bluej.pkg
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#BlueJ package file
editor.fx.0.height=722
editor.fx.0.width=800
editor.fx.0.x=709
editor.fx.0.y=113
editor.fx.0.x=622
editor.fx.0.y=23
objectbench.height=101
objectbench.width=461
objectbench.width=776
package.divider.horizontal=0.6
package.divider.vertical=0.8007380073800738
package.editor.height=427
package.editor.width=674
package.editor.x=1067
package.editor.y=119
package.editor.x=35
package.editor.y=217
package.frame.height=600
package.frame.width=800
package.numDependencies=0
package.numTargets=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
Expand All @@ -24,9 +24,16 @@ readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=TicketMachine
target1.name=BetterTicketMachine
target1.showInterface=false
target1.type=ClassTarget
target1.width=120
target1.x=80
target1.width=150
target1.x=240
target1.y=50
target2.height=50
target2.name=TicketMachine
target2.showInterface=false
target2.type=ClassTarget
target2.width=120
target2.x=80
target2.y=50
25 changes: 16 additions & 9 deletions package.bluej
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#BlueJ package file
editor.fx.0.height=722
editor.fx.0.width=800
editor.fx.0.x=709
editor.fx.0.y=113
editor.fx.0.x=622
editor.fx.0.y=23
objectbench.height=101
objectbench.width=461
objectbench.width=776
package.divider.horizontal=0.6
package.divider.vertical=0.8007380073800738
package.editor.height=427
package.editor.width=674
package.editor.x=1067
package.editor.y=119
package.editor.x=35
package.editor.y=217
package.frame.height=600
package.frame.width=800
package.numDependencies=0
package.numTargets=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
Expand All @@ -24,9 +24,16 @@ readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=TicketMachine
target1.name=BetterTicketMachine
target1.showInterface=false
target1.type=ClassTarget
target1.width=120
target1.x=80
target1.width=150
target1.x=240
target1.y=50
target2.height=50
target2.name=TicketMachine
target2.showInterface=false
target2.type=ClassTarget
target2.width=120
target2.x=80
target2.y=50