first commit
This commit is contained in:
17
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.ctxt
Executable file
17
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.ctxt
Executable file
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=height\ width\ depth
|
||||
comment0.target=Brick(int,\ int,\ int)
|
||||
comment0.text=\n\ Create\ a\ Brick\ given\ edge\ lengths\ in\ centimeters.\n\ @param\ height\ The\ brick's\ height.\n\ @param\ width\ The\ brick's\ width.\n\ @param\ depth\ The\ brick's\ depth.\n
|
||||
comment1.params=
|
||||
comment1.target=double\ getSurfaceArea()
|
||||
comment1.text=\n\ @return\ The\ surface\ area\ of\ this\ brick\ in\ square\ centimeters.\n
|
||||
comment2.params=
|
||||
comment2.target=double\ getWeight()
|
||||
comment2.text=\n\ @return\ The\ weight\ of\ this\ brick\ in\ kg.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getVolume()
|
||||
comment3.text=\n\ @return\ The\ volume\ of\ this\ brick\ in\ cubic\ centimeters.\n
|
||||
comment4.params=
|
||||
comment4.target=double\ getHeight()
|
||||
comment4.text=\n\ @return\ The\ height\ of\ this\ brick\ in\ centimeters.\n
|
||||
numComments=5
|
||||
65
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.java
Executable file
65
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Brick.java
Executable file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Brick models a simple brick.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Brick
|
||||
{
|
||||
// Constant.
|
||||
private static final int WEIGHT_PER_CM3 = 2; // weight per cubic cm in grams
|
||||
|
||||
private int height;
|
||||
private int width;
|
||||
private int depth;
|
||||
|
||||
/**
|
||||
* Create a Brick given edge lengths in centimeters.
|
||||
* @param height The brick's height.
|
||||
* @param width The brick's width.
|
||||
* @param depth The brick's depth.
|
||||
*/
|
||||
public Brick(int height, int width, int depth)
|
||||
{
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The surface area of this brick in square centimeters.
|
||||
*/
|
||||
public double getSurfaceArea()
|
||||
{
|
||||
double side1 = width * height;
|
||||
double side2 = width * depth;
|
||||
double side3 = depth * height;
|
||||
double total = (side1 + side1 + side3) * 2;
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The weight of this brick in kg.
|
||||
*/
|
||||
public double getWeight()
|
||||
{
|
||||
return (getVolume() * WEIGHT_PER_CM3) / 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The volume of this brick in cubic centimeters.
|
||||
*/
|
||||
public int getVolume()
|
||||
{
|
||||
return width * height * depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The height of this brick in centimeters.
|
||||
*/
|
||||
public double getHeight()
|
||||
{
|
||||
return height;
|
||||
}
|
||||
}
|
||||
11
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.ctxt
Executable file
11
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.ctxt
Executable file
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=bricksInPlane\ height
|
||||
comment0.target=Pallet(int,\ int)
|
||||
comment0.text=\n\ Create\ a\ pallet\ with\ a\ given\ number\ of\ bricks.\n\ @param\ bricksInPlane\ The\ number\ of\ bricks\ in\ each\ level\ on\ the\ base.\n\ @param\ height\ The\ number\ of\ bricks\ stacked\ on\ top\ of\ each\ other.\n
|
||||
comment1.params=
|
||||
comment1.target=double\ getWeight()
|
||||
comment1.text=\n\ @return\ The\ weight\ of\ the\ pallet\ (in\ kg)\n
|
||||
comment2.params=
|
||||
comment2.target=double\ getHeight()
|
||||
comment2.text=\n\ @return\ The\ height\ of\ this\ stack\ (in\ cm)\n
|
||||
numComments=3
|
||||
45
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.java
Executable file
45
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/Pallet.java
Executable file
@@ -0,0 +1,45 @@
|
||||
|
||||
/**
|
||||
* A pallet is a stack of bricks on a wooden base.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Pallet
|
||||
{
|
||||
private static final double BASE_WEIGHT = 6.5; // in kg
|
||||
private static final double BASE_HEIGHT = 15; // in cm
|
||||
|
||||
private Brick aBrick;
|
||||
private int bricksInPlane;
|
||||
private int height;
|
||||
|
||||
/**
|
||||
* Create a pallet with a given number of bricks.
|
||||
* @param bricksInPlane The number of bricks in each level on the base.
|
||||
* @param height The number of bricks stacked on top of each other.
|
||||
*/
|
||||
public Pallet(int bricksInPlane, int height)
|
||||
{
|
||||
this.bricksInPlane = bricksInPlane;
|
||||
this.height = height;
|
||||
aBrick = new Brick(8, 20, 12);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The weight of the pallet (in kg)
|
||||
*/
|
||||
public double getWeight()
|
||||
{
|
||||
int numberOfBricks = bricksInPlane * height;
|
||||
return aBrick.getWeight() * numberOfBricks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The height of this stack (in cm)
|
||||
*/
|
||||
public double getHeight()
|
||||
{
|
||||
return (aBrick.getHeight() % height) + BASE_HEIGHT;
|
||||
}
|
||||
}
|
||||
19
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/README.TXT
Executable file
19
Semester 1/Programming 1/Java/examples/projects/chapter09/bricks/README.TXT
Executable file
@@ -0,0 +1,19 @@
|
||||
Project: bricks
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
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
|
||||
|
||||
Here is a part of an application for a company producing bricks.
|
||||
Bricks are delivered in pallets (stacks of bricks).
|
||||
|
||||
The Pallet class is supposed to provide methods telling its height
|
||||
and weight.
|
||||
|
||||
There are (at least) four errors in this project. Find them. Fix them.
|
||||
Then discuss how you went about finding the errors. What strategies
|
||||
did you employ?
|
||||
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Pallet
|
||||
dependency1.to=Brick
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=760
|
||||
package.editor.height=435
|
||||
package.editor.width=652
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=570
|
||||
readme.editor.width=846
|
||||
readme.editor.x=34
|
||||
readme.editor.y=23
|
||||
target1.editor.height=708
|
||||
target1.editor.width=893
|
||||
target1.editor.x=50
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=Brick
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=120
|
||||
target1.x=290
|
||||
target1.y=200
|
||||
target2.editor.height=705
|
||||
target2.editor.width=835
|
||||
target2.editor.x=50
|
||||
target2.editor.y=60
|
||||
target2.height=60
|
||||
target2.name=Pallet
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=140
|
||||
target2.y=100
|
||||
@@ -0,0 +1,38 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine\ instance.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ Return\ the\ value\ currently\ displayed\n\ on\ the\ calculator.\n
|
||||
comment10.params=where
|
||||
comment10.target=void\ reportState(java.lang.String)
|
||||
comment10.text=\n\ Print\ the\ values\ of\ this\ object's\ fields.\n\ @param\ where\ Where\ this\ state\ occurs.\n
|
||||
comment11.params=
|
||||
comment11.target=void\ applyPreviousOperator()
|
||||
comment11.text=\n\ An\ operator\ button\ has\ been\ pressed.\n\ Apply\ the\ immediately\ preceding\ operator\ to\n\ calculate\ an\ intermediate\ result.\ This\ will\n\ form\ the\ left\ operand\ of\ the\ new\ operator.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ '+'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ '-'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ Return\ the\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ Return\ the\ author\ of\ this\ engine.\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ Return\ the\ version\ number\ of\ this\ engine.\n
|
||||
numComments=12
|
||||
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* The main part of the calculator doing the calculations.
|
||||
* This version incorporates debugging print statements.
|
||||
* @author Hacker T. Largebrain (version 1.0)
|
||||
* @version 1.1
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// The value in the display.
|
||||
private int displayValue;
|
||||
// The previous operator typed (+ or -).
|
||||
private char previousOperator;
|
||||
// The left operand to previousOperator.
|
||||
private int leftOperand;
|
||||
|
||||
/**
|
||||
* Create a CalcEngine instance.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
displayValue = 0;
|
||||
previousOperator = ' ';
|
||||
leftOperand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value currently displayed
|
||||
* on the calculator.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
System.out.println("numberPressed called with: " +
|
||||
number);
|
||||
displayValue = displayValue * 10 + number;
|
||||
reportState("end of numberPressed");
|
||||
}
|
||||
|
||||
/**
|
||||
* The '+' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
System.out.println("plus called");
|
||||
applyPreviousOperator();
|
||||
previousOperator = '+';
|
||||
displayValue = 0;
|
||||
reportState("end of plus");
|
||||
}
|
||||
|
||||
/**
|
||||
* The '-' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
applyPreviousOperator();
|
||||
previousOperator = '-';
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
System.out.println("equals called");
|
||||
if(previousOperator == '+') {
|
||||
displayValue = leftOperand + displayValue;
|
||||
}
|
||||
else {
|
||||
displayValue = leftOperand - displayValue;
|
||||
}
|
||||
leftOperand = 0;
|
||||
reportState("end of equals");
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
System.out.println("clear called");
|
||||
displayValue = 0;
|
||||
reportState("end of clear");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "Super Calculator";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the author of this engine.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "Hacker T. Largebrain";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the version number of this engine.
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "version 0.2";
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the values of this object's fields.
|
||||
* @param where Where this state occurs.
|
||||
*/
|
||||
public void reportState(String where)
|
||||
{
|
||||
System.out.println("displayValue: " + displayValue +
|
||||
" leftOperand: " + leftOperand +
|
||||
" previousOperator: " + previousOperator +
|
||||
" at " + where);
|
||||
}
|
||||
|
||||
/**
|
||||
* An operator button has been pressed.
|
||||
* Apply the immediately preceding operator to
|
||||
* calculate an intermediate result. This will
|
||||
* form the left operand of the new operator.
|
||||
*/
|
||||
private void applyPreviousOperator()
|
||||
{
|
||||
System.out.println("applyPreviousOperator called");
|
||||
if(previousOperator == '+') {
|
||||
leftOperand += displayValue;
|
||||
}
|
||||
else if(previousOperator == '-') {
|
||||
leftOperand -= displayValue;
|
||||
}
|
||||
else {
|
||||
// There was no preceding operator.
|
||||
leftOperand = displayValue;
|
||||
}
|
||||
reportState("end of applyPreviousOperator");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngineTester()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ CalcEngineTester\n
|
||||
comment1.params=
|
||||
comment1.target=void\ testAll()
|
||||
comment1.text=\n\ Test\ everything.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ testPlus()
|
||||
comment2.text=\n\ Test\ the\ plus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 3+4.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ testMinus()
|
||||
comment3.text=\n\ Test\ the\ minus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 9\ -\ 4.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Test the CalcEngine class.
|
||||
*
|
||||
* @author Hacker T. Largebrain
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CalcEngineTester
|
||||
{
|
||||
// The engine to be tested.
|
||||
private CalcEngine engine;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class CalcEngineTester
|
||||
*/
|
||||
public CalcEngineTester()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test everything.
|
||||
*/
|
||||
public void testAll()
|
||||
{
|
||||
System.out.println("Testing the addition operation.");
|
||||
System.out.println("The result is: " + testPlus());
|
||||
System.out.println("Testing the subtraction operation.");
|
||||
System.out.println("The result is: " + testMinus());
|
||||
System.out.println("All tests passed.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the plus operation of the engine.
|
||||
* @return the result of calculating 3+4.
|
||||
*/
|
||||
public int testPlus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the presses: 3 + 4 =
|
||||
engine.numberPressed(3);
|
||||
engine.plus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 7.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the minus operation of the engine.
|
||||
* @return the result of calculating 9 - 4.
|
||||
*/
|
||||
public int testMinus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the presses: 9 - 4 =
|
||||
engine.numberPressed(9);
|
||||
engine.minus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 5.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
------------------------------------------------------------------------
|
||||
Super Calculator
|
||||
Author: Hacker T. Largebrain
|
||||
------------------------------------------------------------------------
|
||||
|
||||
This program implements a highly efficient calculating engine for
|
||||
a pocket calculator. It includes a complete test rig to demonstrate
|
||||
that the engine works as it should.
|
||||
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=CalcEngineTester
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=700
|
||||
package.editor.height=435
|
||||
package.editor.width=592
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=515
|
||||
readme.editor.width=853
|
||||
readme.editor.x=30
|
||||
readme.editor.y=23
|
||||
target1.editor.height=753
|
||||
target1.editor.width=885
|
||||
target1.editor.x=50
|
||||
target1.editor.y=47
|
||||
target1.height=60
|
||||
target1.name=CalcEngineTester
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=130
|
||||
target1.y=100
|
||||
target2.editor.height=700
|
||||
target2.editor.width=900
|
||||
target2.editor.x=36
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=CalcEngine
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=130
|
||||
target2.x=280
|
||||
target2.y=200
|
||||
@@ -0,0 +1,35 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine\ instance.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ @return\ The\ value\ currently\ displayed\n\ on\ the\ calculator.\n
|
||||
comment10.params=
|
||||
comment10.target=void\ applyPreviousOperator()
|
||||
comment10.text=\n\ An\ operator\ button\ has\ been\ pressed.\n\ Apply\ the\ immediately\ preceding\ operator\ to\n\ calculate\ an\ intermediate\ result.\ This\ will\n\ form\ the\ left\ operand\ of\ the\ new\ operator.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n\ @param\ number\ The\ single\ digit.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ '+'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ '-'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ @return\ The\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ @return\ The\ author\ of\ this\ engine.\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\n
|
||||
numComments=11
|
||||
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* The main part of the calculator performing the
|
||||
* arithmetic logic of the calculations.
|
||||
* @author Hacker T. Largebrain
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// The value in the display.
|
||||
private int displayValue;
|
||||
// The previous operator typed (+ or -).
|
||||
private char previousOperator;
|
||||
// The left operand to previousOperator.
|
||||
private int leftOperand;
|
||||
|
||||
/**
|
||||
* Create a CalcEngine instance.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
displayValue = 0;
|
||||
previousOperator = ' ';
|
||||
leftOperand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value currently displayed
|
||||
* on the calculator.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
* @param number The single digit.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
displayValue = displayValue * 10 + number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '+' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
applyPreviousOperator();
|
||||
previousOperator = '+';
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '-' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
applyPreviousOperator();
|
||||
previousOperator = '-';
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
if(previousOperator == '+') {
|
||||
displayValue = leftOperand + displayValue;
|
||||
}
|
||||
else {
|
||||
displayValue = leftOperand - displayValue;
|
||||
}
|
||||
leftOperand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "Super Calculator";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The author of this engine.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "Hacker T. Largebrain";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The version number of this engine.
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "version 0.2";
|
||||
}
|
||||
|
||||
/**
|
||||
* An operator button has been pressed.
|
||||
* Apply the immediately preceding operator to
|
||||
* calculate an intermediate result. This will
|
||||
* form the left operand of the new operator.
|
||||
*/
|
||||
private void applyPreviousOperator()
|
||||
{
|
||||
if(previousOperator == '+') {
|
||||
leftOperand += displayValue;
|
||||
}
|
||||
else if(previousOperator == '-') {
|
||||
leftOperand -= displayValue;
|
||||
}
|
||||
else {
|
||||
// There was no preceding operator.
|
||||
leftOperand = displayValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngineTester()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ CalcEngineTester\n
|
||||
comment1.params=
|
||||
comment1.target=void\ testAll()
|
||||
comment1.text=\n\ Test\ everything.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ testPlus()
|
||||
comment2.text=\n\ Test\ the\ plus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 3+4.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ testMinus()
|
||||
comment3.text=\n\ Test\ the\ minus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 9\ -\ 4.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Test the CalcEngine class.
|
||||
*
|
||||
* @author Hacker T. Largebrain
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CalcEngineTester
|
||||
{
|
||||
// The engine to be tested.
|
||||
private CalcEngine engine;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class CalcEngineTester
|
||||
*/
|
||||
public CalcEngineTester()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test everything.
|
||||
*/
|
||||
public void testAll()
|
||||
{
|
||||
System.out.println("Testing the addition operation.");
|
||||
System.out.println("The result is: " + testPlus());
|
||||
System.out.println("Testing the subtraction operation.");
|
||||
System.out.println("The result is: " + testMinus());
|
||||
System.out.println("All tests passed.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the plus operation of the engine.
|
||||
* @return the result of calculating 3+4.
|
||||
*/
|
||||
public int testPlus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the key presses: 3 + 4 =
|
||||
engine.numberPressed(3);
|
||||
engine.plus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 7.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the minus operation of the engine.
|
||||
* @return the result of calculating 9 - 4.
|
||||
*/
|
||||
public int testMinus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the presses: 9 - 4 =
|
||||
engine.numberPressed(9);
|
||||
engine.minus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 5.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
------------------------------------------------------------------------
|
||||
Super Calculator
|
||||
Author: Hacker T. Largebrain
|
||||
------------------------------------------------------------------------
|
||||
|
||||
This program implements a highly efficient calculating engine for
|
||||
a pocket calculator. It includes a complete test rig to demonstrate
|
||||
that the engine works as it should.
|
||||
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=CalcEngineTester
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=694
|
||||
package.editor.height=435
|
||||
package.editor.width=586
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=564
|
||||
readme.editor.width=861
|
||||
readme.editor.x=33
|
||||
readme.editor.y=23
|
||||
target1.editor.height=706
|
||||
target1.editor.width=884
|
||||
target1.editor.x=36
|
||||
target1.editor.y=55
|
||||
target1.height=60
|
||||
target1.name=CalcEngineTester
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=140
|
||||
target1.y=80
|
||||
target2.editor.height=722
|
||||
target2.editor.width=904
|
||||
target2.editor.x=47
|
||||
target2.editor.y=30
|
||||
target2.height=60
|
||||
target2.name=CalcEngine
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=130
|
||||
target2.x=300
|
||||
target2.y=180
|
||||
@@ -0,0 +1,41 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ @return\ The\ value\ that\ should\ currently\ be\ displayed\n\ on\ the\ calculator\ display.\n
|
||||
comment10.params=
|
||||
comment10.target=void\ calculateResult()
|
||||
comment10.text=\n\ Combine\ leftOperand,\ lastOperator,\ and\ the\n\ current\ display\ value.\n\ The\ result\ becomes\ both\ the\ leftOperand\ and\n\ the\ new\ display\ value.\n
|
||||
comment11.params=operator
|
||||
comment11.target=void\ applyOperator(char)
|
||||
comment11.text=\n\ Apply\ an\ operator.\n\ @param\ operator\ The\ operator\ to\ apply.\n
|
||||
comment12.params=
|
||||
comment12.target=void\ keySequenceError()
|
||||
comment12.text=\n\ Report\ an\ error\ in\ the\ sequence\ of\ keys\ that\ was\ pressed.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n\ Either\ start\ a\ new\ operand,\ or\ incorporate\ this\ number\ as\n\ the\ least\ significant\ digit\ of\ an\ existing\ one.\n\ @param\ number\ The\ number\ pressed\ on\ the\ calculator.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ 'plus'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ 'minus'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n\ Reset\ everything\ to\ a\ starting\ state.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ @return\ The\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ @return\ The\ author\ of\ this\ engine.\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\n
|
||||
numComments=13
|
||||
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* The main part of the calculator doing the calculations.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// The calculator's state is maintained in three fields:
|
||||
// buildingDisplayValue, haveLeftOperand, and lastOperator.
|
||||
|
||||
// Are we already building a value in the display, or will the
|
||||
// next digit be the first of a new one?
|
||||
private boolean buildingDisplayValue;
|
||||
// Has a left operand already been entered (or calculated)?
|
||||
private boolean haveLeftOperand;
|
||||
// The most recent operator that was entered.
|
||||
private char lastOperator;
|
||||
|
||||
// The current value (to be) shown in the display.
|
||||
private int displayValue;
|
||||
// The value of an existing left operand.
|
||||
private int leftOperand;
|
||||
|
||||
/**
|
||||
* Create a CalcEngine.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value that should currently be displayed
|
||||
* on the calculator display.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
* Either start a new operand, or incorporate this number as
|
||||
* the least significant digit of an existing one.
|
||||
* @param number The number pressed on the calculator.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
if(buildingDisplayValue) {
|
||||
// Incorporate this digit.
|
||||
displayValue = displayValue*10 + number;
|
||||
}
|
||||
else {
|
||||
// Start building a new number.
|
||||
displayValue = number;
|
||||
buildingDisplayValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'plus' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
applyOperator('+');
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'minus' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
applyOperator('-');
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
// This should completes the building of a second operand,
|
||||
// so ensure that we really have a left operand, an operator
|
||||
// and a right operand.
|
||||
if(haveLeftOperand &&
|
||||
lastOperator != '?' &&
|
||||
buildingDisplayValue) {
|
||||
calculateResult();
|
||||
lastOperator = '?';
|
||||
buildingDisplayValue = false;
|
||||
}
|
||||
else {
|
||||
keySequenceError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
* Reset everything to a starting state.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
lastOperator = '?';
|
||||
haveLeftOperand = false;
|
||||
buildingDisplayValue = false;
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "Java Calculator";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The author of this engine.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "David J. Barnes and Michael Kölling";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The version number of this engine.
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "Version 1.0";
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine leftOperand, lastOperator, and the
|
||||
* current display value.
|
||||
* The result becomes both the leftOperand and
|
||||
* the new display value.
|
||||
*/
|
||||
private void calculateResult()
|
||||
{
|
||||
switch(lastOperator) {
|
||||
case '+':
|
||||
displayValue = leftOperand + displayValue;
|
||||
haveLeftOperand = true;
|
||||
leftOperand = displayValue;
|
||||
break;
|
||||
case '-':
|
||||
displayValue = leftOperand - displayValue;
|
||||
haveLeftOperand = true;
|
||||
leftOperand = displayValue;
|
||||
break;
|
||||
default:
|
||||
keySequenceError();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an operator.
|
||||
* @param operator The operator to apply.
|
||||
*/
|
||||
private void applyOperator(char operator)
|
||||
{
|
||||
// If we are not in the process of building a new operand
|
||||
// then it is an error, unless we have just calculated a
|
||||
// result using '='.
|
||||
if(!buildingDisplayValue &&
|
||||
!(haveLeftOperand && lastOperator == '?')) {
|
||||
keySequenceError();
|
||||
return;
|
||||
}
|
||||
|
||||
if(lastOperator != '?') {
|
||||
// First apply the previous operator.
|
||||
calculateResult();
|
||||
}
|
||||
else {
|
||||
// The displayValue now becomes the left operand of this
|
||||
// new operator.
|
||||
haveLeftOperand = true;
|
||||
leftOperand = displayValue;
|
||||
}
|
||||
lastOperator = operator;
|
||||
buildingDisplayValue = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Report an error in the sequence of keys that was pressed.
|
||||
*/
|
||||
private void keySequenceError()
|
||||
{
|
||||
System.out.println("A key sequence error has occurred.");
|
||||
// Reset everything.
|
||||
clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Calculator()
|
||||
comment0.text=\n\ Create\ a\ new\ calculator\ and\ show\ it.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ show()
|
||||
comment1.text=\n\ In\ case\ the\ window\ was\ closed,\ show\ it\ again.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* The main class of a simple calculator. Create one of these and you'll
|
||||
* get the calculator on screen.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Calculator
|
||||
{
|
||||
private CalcEngine engine;
|
||||
private UserInterface gui;
|
||||
|
||||
/**
|
||||
* Create a new calculator and show it.
|
||||
*/
|
||||
public Calculator()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
gui = new UserInterface(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* In case the window was closed, show it again.
|
||||
*/
|
||||
public void show()
|
||||
{
|
||||
gui.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
Project: calculator-full-solution
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
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
|
||||
|
||||
|
||||
This version of the project implements a complete
|
||||
solution to the simple calculator discussed in
|
||||
Chapter 9.
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=engine
|
||||
comment0.target=UserInterface(CalcEngine)
|
||||
comment0.text=\n\ Create\ a\ user\ interface.\n\ @param\ engine\ The\ calculator\ engine.\n
|
||||
comment1.params=visible
|
||||
comment1.target=void\ setVisible(boolean)
|
||||
comment1.text=\n\ Set\ the\ visibility\ of\ the\ interface.\n\ @param\ visible\ true\ if\ the\ interface\ is\ to\ be\ made\ visible,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ makeFrame()
|
||||
comment2.text=\n\ Make\ the\ frame\ for\ the\ user\ interface.\n
|
||||
comment3.params=panel\ buttonText\ action
|
||||
comment3.target=void\ addButton(java.awt.Container,\ java.lang.String,\ UserInterface.ButtonAction)
|
||||
comment3.text=\n\ Add\ a\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ buttonText\ The\ text\ for\ the\ button.\n\ @param\ action\ Action\ to\ be\ taken\ by\ the\ button.\n
|
||||
comment4.params=panel\ digit
|
||||
comment4.target=void\ addNumberButton(java.awt.Container,\ int)
|
||||
comment4.text=\n\ Add\ a\ number\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ digit\ The\ single\ digit\ on\ the\ button.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ redisplay()
|
||||
comment5.text=\n\ Update\ the\ interface\ display\ to\ show\ the\ current\ value\ of\ the\ \n\ calculator.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ showInfo()
|
||||
comment6.text=\n\ Toggle\ the\ info\ display\ in\ the\ calculator's\ status\ area\ between\ the\n\ author\ and\ version\ information.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,144 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
|
||||
/**
|
||||
* A graphical user interface for the calculator. No calculation is being
|
||||
* done here. This class is responsible just for putting up the display on
|
||||
* screen. It then refers to the "CalcEngine" to do all the real work.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class UserInterface
|
||||
{
|
||||
private CalcEngine calc;
|
||||
private boolean showingAuthor;
|
||||
|
||||
private JFrame frame;
|
||||
private JTextField display;
|
||||
private JLabel status;
|
||||
|
||||
/**
|
||||
* Create a user interface.
|
||||
* @param engine The calculator engine.
|
||||
*/
|
||||
public UserInterface(CalcEngine engine)
|
||||
{
|
||||
calc = engine;
|
||||
showingAuthor = true;
|
||||
makeFrame();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility of the interface.
|
||||
* @param visible true if the interface is to be made visible, false otherwise.
|
||||
*/
|
||||
public void setVisible(boolean visible)
|
||||
{
|
||||
frame.setVisible(visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the frame for the user interface.
|
||||
*/
|
||||
private void makeFrame()
|
||||
{
|
||||
frame = new JFrame(calc.getTitle());
|
||||
|
||||
JPanel contentPane = (JPanel)frame.getContentPane();
|
||||
contentPane.setLayout(new BorderLayout(8, 8));
|
||||
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));
|
||||
|
||||
display = new JTextField();
|
||||
contentPane.add(display, BorderLayout.NORTH);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
|
||||
addNumberButton(buttonPanel, 7);
|
||||
addNumberButton(buttonPanel, 8);
|
||||
addNumberButton(buttonPanel, 9);
|
||||
addButton(buttonPanel, "C", () -> calc.clear());
|
||||
|
||||
addNumberButton(buttonPanel, 4);
|
||||
addNumberButton(buttonPanel, 5);
|
||||
addNumberButton(buttonPanel, 6);
|
||||
addButton(buttonPanel, "?", () -> showInfo());
|
||||
|
||||
addNumberButton(buttonPanel, 1);
|
||||
addNumberButton(buttonPanel, 2);
|
||||
addNumberButton(buttonPanel, 3);
|
||||
buttonPanel.add(new JLabel(" "));
|
||||
|
||||
addNumberButton(buttonPanel, 0);
|
||||
addButton(buttonPanel, "+", () -> calc.plus());
|
||||
addButton(buttonPanel, "-", () -> calc.minus());
|
||||
addButton(buttonPanel, "=", () -> calc.equals());
|
||||
|
||||
contentPane.add(buttonPanel, BorderLayout.CENTER);
|
||||
|
||||
status = new JLabel(calc.getAuthor());
|
||||
contentPane.add(status, BorderLayout.SOUTH);
|
||||
|
||||
frame.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param buttonText The text for the button.
|
||||
* @param action Action to be taken by the button.
|
||||
*/
|
||||
private void addButton(Container panel, String buttonText, ButtonAction action)
|
||||
{
|
||||
JButton button = new JButton(buttonText);
|
||||
button.addActionListener(e -> { action.act(); redisplay(); });
|
||||
panel.add(button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a number button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param digit The single digit on the button.
|
||||
*/
|
||||
private void addNumberButton(Container panel, int digit)
|
||||
{
|
||||
addButton(panel, "" + digit, () -> calc.numberPressed(digit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the interface display to show the current value of the
|
||||
* calculator.
|
||||
*/
|
||||
private void redisplay()
|
||||
{
|
||||
display.setText("" + calc.getDisplayValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the info display in the calculator's status area between the
|
||||
* author and version information.
|
||||
*/
|
||||
private void showInfo()
|
||||
{
|
||||
if(showingAuthor)
|
||||
status.setText(calc.getVersion());
|
||||
else
|
||||
status.setText(calc.getAuthor());
|
||||
|
||||
showingAuthor = !showingAuthor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Functional interface for button actions.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
private interface ButtonAction
|
||||
{
|
||||
/**
|
||||
* Act on a button press.
|
||||
*/
|
||||
public void act();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=UserInterface
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Calculator
|
||||
dependency2.to=CalcEngine
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Calculator
|
||||
dependency3.to=UserInterface
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=775
|
||||
package.editor.height=435
|
||||
package.editor.width=667
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=521
|
||||
readme.editor.width=840
|
||||
readme.editor.x=30
|
||||
readme.editor.y=23
|
||||
target1.editor.height=723
|
||||
target1.editor.width=824
|
||||
target1.editor.x=50
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=Calculator
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=140
|
||||
target1.x=110
|
||||
target1.y=80
|
||||
target2.editor.height=664
|
||||
target2.editor.width=848
|
||||
target2.editor.x=37
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=UserInterface
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=140
|
||||
target2.x=280
|
||||
target2.y=170
|
||||
target3.editor.height=732
|
||||
target3.editor.width=681
|
||||
target3.editor.x=103
|
||||
target3.editor.y=39
|
||||
target3.height=60
|
||||
target3.name=CalcEngine
|
||||
target3.naviview.expanded=false
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=140
|
||||
target3.x=450
|
||||
target3.y=260
|
||||
@@ -0,0 +1,32 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ @return\ The\ value\ that\ should\ currently\ be\ displayed\n\ on\ the\ calculator\ display.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n\ Either\ start\ a\ new\ operand,\ or\ incorporate\ this\ number\ as\n\ the\ least\ significant\ digit\ of\ an\ existing\ one.\n\ @param\ number\ The\ number\ pressed\ on\ the\ calculator.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ 'plus'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ 'minus'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ @return\ The\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ @return\ The\ author\ of\ this\ engine.\ This\ string\ is\ displayed\ as\ it\ is,\n\ so\ it\ should\ say\ something\ like\ "Written\ by\ H.\ Simpson".\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\ This\ string\ is\ displayed\ as\ \n\ it\ is,\ so\ it\ should\ say\ something\ like\ "Version\ 1.1".\n
|
||||
numComments=10
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* The main part of the calculator doing the calculations.
|
||||
*
|
||||
* @author: (none yet)
|
||||
* @version 0.1 (incomplete)
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// Put instance variables here.
|
||||
|
||||
/**
|
||||
* Create a CalcEngine.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value that should currently be displayed
|
||||
* on the calculator display.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
* Either start a new operand, or incorporate this number as
|
||||
* the least significant digit of an existing one.
|
||||
* @param number The number pressed on the calculator.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'plus' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'minus' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The author of this engine. This string is displayed as it is,
|
||||
* so it should say something like "Written by H. Simpson".
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The version number of this engine. This string is displayed as
|
||||
* it is, so it should say something like "Version 1.1".
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Calculator()
|
||||
comment0.text=\n\ Create\ a\ new\ calculator\ and\ show\ it.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ show()
|
||||
comment1.text=\n\ In\ case\ the\ window\ was\ closed,\ show\ it\ again.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
/**
|
||||
* The main class of a simple calculator. Create one of these and you'll
|
||||
* get the calculator on screen.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Calculator
|
||||
{
|
||||
private CalcEngine engine;
|
||||
private UserInterface gui;
|
||||
|
||||
/**
|
||||
* Create a new calculator and show it.
|
||||
*/
|
||||
public Calculator()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
gui = new UserInterface(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* In case the window was closed, show it again.
|
||||
*/
|
||||
public void show()
|
||||
{
|
||||
gui.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
Project: calculator-gui
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
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
|
||||
|
||||
|
||||
This program is meant to implement a simple pocket calculator.
|
||||
In its initial version, it is incomplete.
|
||||
|
||||
The Calculator and UserInterface classes can be used as they are, but the
|
||||
CalcEngine, the bit that does the real calculations, has not been implemented.
|
||||
|
||||
It is your task to finish the job. Have fun!
|
||||
|
||||
To start this application (once you have implemented the missing bits) just
|
||||
create an object of class Calculator.
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=engine
|
||||
comment0.target=UserInterface(CalcEngine)
|
||||
comment0.text=\n\ Create\ a\ user\ interface.\n\ @param\ engine\ The\ calculator\ engine.\n
|
||||
comment1.params=visible
|
||||
comment1.target=void\ setVisible(boolean)
|
||||
comment1.text=\n\ Set\ the\ visibility\ of\ the\ interface.\n\ @param\ visible\ true\ if\ the\ interface\ is\ to\ be\ made\ visible,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ makeFrame()
|
||||
comment2.text=\n\ Make\ the\ frame\ for\ the\ user\ interface.\n
|
||||
comment3.params=panel\ buttonText\ action
|
||||
comment3.target=void\ addButton(java.awt.Container,\ java.lang.String,\ UserInterface.ButtonAction)
|
||||
comment3.text=\n\ Add\ a\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ buttonText\ The\ text\ for\ the\ button.\n\ @param\ action\ Action\ to\ be\ taken\ by\ the\ button.\n
|
||||
comment4.params=panel\ digit
|
||||
comment4.target=void\ addNumberButton(java.awt.Container,\ int)
|
||||
comment4.text=\n\ Add\ a\ number\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ digit\ The\ single\ digit\ on\ the\ button.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ redisplay()
|
||||
comment5.text=\n\ Update\ the\ interface\ display\ to\ show\ the\ current\ value\ of\ the\ \n\ calculator.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ showInfo()
|
||||
comment6.text=\n\ Toggle\ the\ info\ display\ in\ the\ calculator's\ status\ area\ between\ the\n\ author\ and\ version\ information.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,144 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
|
||||
/**
|
||||
* A graphical user interface for the calculator. No calculation is being
|
||||
* done here. This class is responsible just for putting up the display on
|
||||
* screen. It then refers to the "CalcEngine" to do all the real work.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class UserInterface
|
||||
{
|
||||
private CalcEngine calc;
|
||||
private boolean showingAuthor;
|
||||
|
||||
private JFrame frame;
|
||||
private JTextField display;
|
||||
private JLabel status;
|
||||
|
||||
/**
|
||||
* Create a user interface.
|
||||
* @param engine The calculator engine.
|
||||
*/
|
||||
public UserInterface(CalcEngine engine)
|
||||
{
|
||||
calc = engine;
|
||||
showingAuthor = true;
|
||||
makeFrame();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility of the interface.
|
||||
* @param visible true if the interface is to be made visible, false otherwise.
|
||||
*/
|
||||
public void setVisible(boolean visible)
|
||||
{
|
||||
frame.setVisible(visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the frame for the user interface.
|
||||
*/
|
||||
private void makeFrame()
|
||||
{
|
||||
frame = new JFrame(calc.getTitle());
|
||||
|
||||
JPanel contentPane = (JPanel)frame.getContentPane();
|
||||
contentPane.setLayout(new BorderLayout(8, 8));
|
||||
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));
|
||||
|
||||
display = new JTextField();
|
||||
contentPane.add(display, BorderLayout.NORTH);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
|
||||
addNumberButton(buttonPanel, 7);
|
||||
addNumberButton(buttonPanel, 8);
|
||||
addNumberButton(buttonPanel, 9);
|
||||
addButton(buttonPanel, "C", () -> calc.clear());
|
||||
|
||||
addNumberButton(buttonPanel, 4);
|
||||
addNumberButton(buttonPanel, 5);
|
||||
addNumberButton(buttonPanel, 6);
|
||||
addButton(buttonPanel, "?", () -> showInfo());
|
||||
|
||||
addNumberButton(buttonPanel, 1);
|
||||
addNumberButton(buttonPanel, 2);
|
||||
addNumberButton(buttonPanel, 3);
|
||||
buttonPanel.add(new JLabel(" "));
|
||||
|
||||
addNumberButton(buttonPanel, 0);
|
||||
addButton(buttonPanel, "+", () -> calc.plus());
|
||||
addButton(buttonPanel, "-", () -> calc.minus());
|
||||
addButton(buttonPanel, "=", () -> calc.equals());
|
||||
|
||||
contentPane.add(buttonPanel, BorderLayout.CENTER);
|
||||
|
||||
status = new JLabel(calc.getAuthor());
|
||||
contentPane.add(status, BorderLayout.SOUTH);
|
||||
|
||||
frame.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param buttonText The text for the button.
|
||||
* @param action Action to be taken by the button.
|
||||
*/
|
||||
private void addButton(Container panel, String buttonText, ButtonAction action)
|
||||
{
|
||||
JButton button = new JButton(buttonText);
|
||||
button.addActionListener(e -> { action.act(); redisplay(); });
|
||||
panel.add(button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a number button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param digit The single digit on the button.
|
||||
*/
|
||||
private void addNumberButton(Container panel, int digit)
|
||||
{
|
||||
addButton(panel, "" + digit, () -> calc.numberPressed(digit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the interface display to show the current value of the
|
||||
* calculator.
|
||||
*/
|
||||
private void redisplay()
|
||||
{
|
||||
display.setText("" + calc.getDisplayValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the info display in the calculator's status area between the
|
||||
* author and version information.
|
||||
*/
|
||||
private void showInfo()
|
||||
{
|
||||
if(showingAuthor)
|
||||
status.setText(calc.getVersion());
|
||||
else
|
||||
status.setText(calc.getAuthor());
|
||||
|
||||
showingAuthor = !showingAuthor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Functional interface for button actions.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
private interface ButtonAction
|
||||
{
|
||||
/**
|
||||
* Act on a button press.
|
||||
*/
|
||||
public void act();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=UserInterface
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Calculator
|
||||
dependency2.to=CalcEngine
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Calculator
|
||||
dependency3.to=UserInterface
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=760
|
||||
package.editor.height=435
|
||||
package.editor.width=652
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=602
|
||||
readme.editor.width=814
|
||||
readme.editor.x=41
|
||||
readme.editor.y=23
|
||||
target1.editor.height=735
|
||||
target1.editor.width=843
|
||||
target1.editor.x=50
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=Calculator
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=80
|
||||
target1.y=70
|
||||
target2.editor.height=696
|
||||
target2.editor.width=822
|
||||
target2.editor.x=99
|
||||
target2.editor.y=49
|
||||
target2.height=60
|
||||
target2.name=UserInterface
|
||||
target2.naviview.expanded=false
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=240
|
||||
target2.y=150
|
||||
target3.editor.height=721
|
||||
target3.editor.width=870
|
||||
target3.editor.x=50
|
||||
target3.editor.y=60
|
||||
target3.height=60
|
||||
target3.name=CalcEngine
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=120
|
||||
target3.x=430
|
||||
target3.y=230
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=author\ text\ rating
|
||||
comment0.target=Comment(java.lang.String,\ java.lang.String,\ int)
|
||||
comment0.text=\n\ Create\ a\ comment\ with\ all\ necessary\ details.\ The\ initial\ vote\ balance\ is\ 0.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ upvote()
|
||||
comment1.text=\n\ Indicate\ that\ this\ comment\ is\ useful\ ('upvote').\ This\ is\ used\ when\ a\ reader\ clicks\n\ the\ 'Yes'\ button\ after\ the\ "Was\ this\ comment\ helpful?"\ quesion.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ downvote()
|
||||
comment2.text=\n\ Indicate\ that\ this\ comment\ is\ not\ useful\ ('downvote').\ This\ is\ used\ when\ a\ reader\n\ clicks\ the\ 'No'\ button\ after\ the\ "Was\ this\ comment\ helpful?"\ quesion.\n
|
||||
comment3.params=
|
||||
comment3.target=java.lang.String\ getAuthor()
|
||||
comment3.text=\n\ Return\ the\ author\ of\ this\ comment.\n
|
||||
comment4.params=
|
||||
comment4.target=int\ getRating()
|
||||
comment4.text=\n\ Return\ the\ rating\ of\ this\ comment.\n
|
||||
comment5.params=
|
||||
comment5.target=int\ getVoteCount()
|
||||
comment5.text=\n\ Return\ the\ vote\ count\ (balance\ of\ up\ and\ down-votes).\n
|
||||
comment6.params=
|
||||
comment6.target=java.lang.String\ getFullDetails()
|
||||
comment6.text=\n\ Return\ the\ full\ text\ and\ details\ of\ the\ comment,\ including\ \n\ the\ comment\ text,\ author\ and\ rating.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,87 @@
|
||||
|
||||
/**
|
||||
* This class represents a comment left for a sales item on an online sales site.
|
||||
* A comment consists of a comment text, a rating, and an author name. Other users
|
||||
* can indicate whether the comment was useful or not (called 'upvoting' or
|
||||
* 'downvoting'). The balance between upvotes and downvotes is stored with comments.
|
||||
* A negative vote balance means that the comment received more downvotes than upvotes.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Comment
|
||||
{
|
||||
private String author;
|
||||
private String text;
|
||||
private int rating;
|
||||
private int votes;
|
||||
|
||||
/**
|
||||
* Create a comment with all necessary details. The initial vote balance is 0.
|
||||
*/
|
||||
public Comment(String author, String text, int rating)
|
||||
{
|
||||
this.author = author;
|
||||
this.text = text;
|
||||
this.rating = rating;
|
||||
votes = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that this comment is useful ('upvote'). This is used when a reader clicks
|
||||
* the 'Yes' button after the "Was this comment helpful?" quesion.
|
||||
*/
|
||||
public void upvote()
|
||||
{
|
||||
votes++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that this comment is not useful ('downvote'). This is used when a reader
|
||||
* clicks the 'No' button after the "Was this comment helpful?" quesion.
|
||||
*/
|
||||
public void downvote()
|
||||
{
|
||||
votes--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the author of this comment.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rating of this comment.
|
||||
*/
|
||||
public int getRating()
|
||||
{
|
||||
return rating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the vote count (balance of up and down-votes).
|
||||
*/
|
||||
public int getVoteCount()
|
||||
{
|
||||
return votes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the full text and details of the comment, including
|
||||
* the comment text, author and rating.
|
||||
*/
|
||||
public String getFullDetails()
|
||||
{
|
||||
String details = "Rating: " + "*****".substring(0,rating) + " "
|
||||
+ "By: " + author + "\n"
|
||||
+ " " + text + "\n";
|
||||
// Note: 'votes' is currently included for testing purposes only. In the final
|
||||
// application, this will nt be shown. Instead, the vote count will be used to
|
||||
// select and order the comments on screen.
|
||||
details += "(Voted as helpful: " + votes + ")\n";
|
||||
return details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
Project: online-shop-junit
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
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
|
||||
|
||||
This project implements a small part of an online sales system (such as Amazon.com).
|
||||
The current project is concerned only with customer comments for sales items. It contains
|
||||
code to create, store, show and manipulate customer comments.
|
||||
|
||||
The purpose of this project is to introduce regression testing using JUnit. It includes
|
||||
a test class that demonstrates some of the testing functionality. Testing is further
|
||||
discussed in the book.
|
||||
|
||||
NOTE: There are several serious errors to be found in these classes.
|
||||
@@ -0,0 +1,41 @@
|
||||
#BlueJ class context
|
||||
comment0.params=name\ price
|
||||
comment0.target=SalesItem(java.lang.String,\ int)
|
||||
comment0.text=\n\ Create\ a\ new\ sales\ item.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ getName()
|
||||
comment1.text=\n\ Return\ the\ name\ of\ this\ item.\n
|
||||
comment10.params=rating
|
||||
comment10.target=boolean\ ratingInvalid(int)
|
||||
comment10.text=\n\ Check\ whether\ the\ given\ rating\ is\ invalid.\ Return\ true\ if\ it\ is\ invalid.\n\ Valid\ ratings\ are\ in\ the\ range\ [1..5].\n
|
||||
comment11.params=author
|
||||
comment11.target=Comment\ findCommentByAuthor(java.lang.String)
|
||||
comment11.text=\n\ Find\ the\ comment\ by\ the\ author\ with\ the\ given\ name.\n\ \n\ @return\ The\ comment\ if\ it\ exists;\ null\ if\ it\ doesn't.\n
|
||||
comment12.params=price
|
||||
comment12.target=java.lang.String\ priceString(int)
|
||||
comment12.text=\n\ For\ a\ price\ given\ as\ an\ int,\ return\ a\ readable\ String\ representing\ the\ same\ price.\n\ The\ price\ is\ given\ in\ whole\ cents.\ For\ example\ for\ price\=\=12345,\ the\ following\ String\n\ is\ returned\:\ $123.45\n
|
||||
comment2.params=
|
||||
comment2.target=int\ getPrice()
|
||||
comment2.text=\n\ Return\ the\ price\ of\ this\ item.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getNumberOfComments()
|
||||
comment3.text=\n\ Return\ the\ number\ of\ customer\ comments\ for\ this\ item.\n
|
||||
comment4.params=author\ text\ rating
|
||||
comment4.target=boolean\ addComment(java.lang.String,\ java.lang.String,\ int)
|
||||
comment4.text=\n\ Add\ a\ comment\ to\ the\ comment\ list\ of\ this\ sales\ item.\ Return\ true\ if\ successful;\n\ false\ if\ the\ comment\ was\ rejected.\n\ \n\ The\ comment\ will\ be\ rejected\ if\ the\ same\ author\ has\ already\ left\ a\ comment,\ or\n\ if\ the\ rating\ is\ invalid.\ Valid\ ratings\ are\ numbers\ between\ 1\ and\ 5\ (inclusive).\n
|
||||
comment5.params=index
|
||||
comment5.target=void\ removeComment(int)
|
||||
comment5.text=\n\ Remove\ the\ comment\ stored\ at\ the\ index\ given.\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment6.params=index
|
||||
comment6.target=void\ upvoteComment(int)
|
||||
comment6.text=\n\ Upvote\ the\ comment\ at\ 'index'.\ That\ is\:\ count\ this\ comment\ as\ more\ helpful.\n\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment7.params=index
|
||||
comment7.target=void\ downvoteComment(int)
|
||||
comment7.text=\n\ Downvote\ the\ comment\ at\ 'index'.\ That\ is\:\ count\ this\ comment\ as\ less\ helpful.\n\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment8.params=
|
||||
comment8.target=void\ showInfo()
|
||||
comment8.text=\n\ Show\ all\ comments\ on\ screen.\ (Currently,\ for\ testing\ purposes\:\ print\ to\ the\ terminal.\n\ Modify\ later\ for\ web\ display.)\n
|
||||
comment9.params=
|
||||
comment9.target=Comment\ findMostHelpfulComment()
|
||||
comment9.text=\n\ Return\ the\ most\ helpful\ comment.\ The\ most\ useful\ comment\ is\ the\ one\ with\ the\ highest\ vote\n\ balance.\ If\ there\ are\ multiple\ comments\ with\ equal\ highest\ balance,\ return\ any\ one\ of\n\ them.\n
|
||||
numComments=13
|
||||
@@ -0,0 +1,184 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* The class represents sales items on an online e-commerce site (such as Amazon.com).
|
||||
* SalesItem objects store all information relevant to this item, including description,
|
||||
* price, customer comments, etc.
|
||||
*
|
||||
* NOTE: The current version is incomplete! Currently, only code dealing with customer
|
||||
* comments is here.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 0.1 (2016.02.29)
|
||||
*/
|
||||
public class SalesItem
|
||||
{
|
||||
private String name;
|
||||
private int price; // in cents
|
||||
private ArrayList<Comment> comments;
|
||||
|
||||
/**
|
||||
* Create a new sales item.
|
||||
*/
|
||||
public SalesItem(String name, int price)
|
||||
{
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
comments = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of this item.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the price of this item.
|
||||
*/
|
||||
public int getPrice()
|
||||
{
|
||||
return price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of customer comments for this item.
|
||||
*/
|
||||
public int getNumberOfComments()
|
||||
{
|
||||
return comments.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a comment to the comment list of this sales item. Return true if successful;
|
||||
* false if the comment was rejected.
|
||||
*
|
||||
* The comment will be rejected if the same author has already left a comment, or
|
||||
* if the rating is invalid. Valid ratings are numbers between 1 and 5 (inclusive).
|
||||
*/
|
||||
public boolean addComment(String author, String text, int rating)
|
||||
{
|
||||
if(ratingInvalid(rating)) { // reject invalid ratings
|
||||
return false;
|
||||
}
|
||||
|
||||
if(findCommentByAuthor(author) != null) { // reject mutiple comments by same author
|
||||
return false;
|
||||
}
|
||||
|
||||
comments.add(new Comment(author, text, rating));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the comment stored at the index given. If the index is invalid, do nothing.
|
||||
*/
|
||||
public void removeComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upvote the comment at 'index'. That is: count this comment as more helpful.
|
||||
* If the index is invalid, do nothing.
|
||||
*/
|
||||
public void upvoteComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.get(index).upvote();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downvote the comment at 'index'. That is: count this comment as less helpful.
|
||||
* If the index is invalid, do nothing.
|
||||
*/
|
||||
public void downvoteComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.get(index).downvote();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all comments on screen. (Currently, for testing purposes: print to the terminal.
|
||||
* Modify later for web display.)
|
||||
*/
|
||||
public void showInfo()
|
||||
{
|
||||
System.out.println("*** " + name + " ***");
|
||||
System.out.println("Price: " + priceString(price));
|
||||
System.out.println();
|
||||
System.out.println("Customer comments:");
|
||||
for(Comment comment : comments) {
|
||||
System.out.println("-------------------------------------------");
|
||||
System.out.println(comment.getFullDetails());
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("===========================================");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the most helpful comment. The most useful comment is the one with the highest vote
|
||||
* balance. If there are multiple comments with equal highest balance, return any one of
|
||||
* them.
|
||||
*/
|
||||
public Comment findMostHelpfulComment()
|
||||
{
|
||||
Iterator<Comment> it = comments.iterator();
|
||||
Comment best = it.next();
|
||||
while(it.hasNext()) {
|
||||
Comment current = it.next();
|
||||
if(current.getVoteCount() > best.getVoteCount()) {
|
||||
best = current;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given rating is invalid. Return true if it is invalid.
|
||||
* Valid ratings are in the range [1..5].
|
||||
*/
|
||||
private boolean ratingInvalid(int rating)
|
||||
{
|
||||
return rating < 0 || rating > 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the comment by the author with the given name.
|
||||
*
|
||||
* @return The comment if it exists; null if it doesn't.
|
||||
*/
|
||||
private Comment findCommentByAuthor(String author)
|
||||
{
|
||||
for(Comment comment : comments) {
|
||||
if(comment.getAuthor().equals(author)) {
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* For a price given as an int, return a readable String representing the same price.
|
||||
* The price is given in whole cents. For example for price==12345, the following String
|
||||
* is returned: $123.45
|
||||
*/
|
||||
private String priceString(int price)
|
||||
{
|
||||
int dollars = price / 100;
|
||||
int cents = price - (dollars*100);
|
||||
if(cents <= 9) {
|
||||
return "$" + dollars + ".0" + cents; // include zero padding if necessary
|
||||
}
|
||||
else {
|
||||
return "$" + dollars + "." + cents;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=SalesItemTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ SalesItemTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testAddComment()
|
||||
comment3.text=\n\ Test\ that\ a\ comment\ can\ be\ added,\ and\ that\ the\ comment\ count\ is\ correct\ afterwards.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ testIllegalRating()
|
||||
comment4.text=\n\ Test\ that\ a\ comment\ using\ an\ illegal\ rating\ value\ is\ rejected.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ testInit()
|
||||
comment5.text=\n\ Test\ that\ a\ sales\ item\ is\ correctly\ initialised\ (name\ and\ price).\n
|
||||
comment6.params=
|
||||
comment6.target=void\ addComment()
|
||||
numComments=7
|
||||
@@ -0,0 +1,82 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class SalesItemTest.
|
||||
*
|
||||
* @author mik
|
||||
* @version 0.1
|
||||
*/
|
||||
public class SalesItemTest
|
||||
{
|
||||
/**
|
||||
* Default constructor for test class SalesItemTest
|
||||
*/
|
||||
public SalesItemTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a comment can be added, and that the comment count is correct afterwards.
|
||||
*/
|
||||
@Test
|
||||
public void testAddComment()
|
||||
{
|
||||
SalesItem salesIte1 = new SalesItem("Brain surgery for Dummies", 21998);
|
||||
assertEquals(true, salesIte1.addComment("James Duckling", "This book is great. I perform brain surgery every week now.", 4));
|
||||
assertEquals(1, salesIte1.getNumberOfComments());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a comment using an illegal rating value is rejected.
|
||||
*/
|
||||
@Test
|
||||
public void testIllegalRating()
|
||||
{
|
||||
SalesItem salesIte1 = new SalesItem("Java For Complete Idiots, Vol 2", 19900);
|
||||
assertEquals(false, salesIte1.addComment("Joshua Black", "Not worth the money. The font is too small.", -5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a sales item is correctly initialised (name and price).
|
||||
*/
|
||||
@Test
|
||||
public void testInit()
|
||||
{
|
||||
SalesItem salesIte1 = new SalesItem("test name", 1000);
|
||||
assertEquals("test name", salesIte1.getName());
|
||||
assertEquals(1000, salesIte1.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addComment()
|
||||
{
|
||||
SalesItem salesIte1 = new SalesItem("Brain Surgery for Dummies.", 9899);
|
||||
assertEquals(true, salesIte1.addComment("Fred", "Great - I perform brain surgery every week now!", 4));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=SalesItem
|
||||
dependency1.to=Comment
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=670
|
||||
package.editor.height=371
|
||||
package.editor.width=562
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=700
|
||||
readme.editor.width=900
|
||||
readme.editor.x=50
|
||||
readme.editor.y=45
|
||||
target1.editor.height=700
|
||||
target1.editor.width=921
|
||||
target1.editor.x=315
|
||||
target1.editor.y=47
|
||||
target1.height=60
|
||||
target1.name=Comment
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=290
|
||||
target1.y=210
|
||||
target2.editor.height=700
|
||||
target2.editor.width=900
|
||||
target2.editor.x=100
|
||||
target2.editor.y=53
|
||||
target2.height=50
|
||||
target2.name=SalesItemTest
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=UnitTestTargetJunit4
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=160
|
||||
target2.y=70
|
||||
target3.association=SalesItemTest
|
||||
target3.editor.height=757
|
||||
target3.editor.width=967
|
||||
target3.editor.x=187
|
||||
target3.editor.y=43
|
||||
target3.height=60
|
||||
target3.name=SalesItem
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=110
|
||||
target3.x=130
|
||||
target3.y=100
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=author\ text\ rating
|
||||
comment0.target=Comment(java.lang.String,\ java.lang.String,\ int)
|
||||
comment0.text=\n\ Create\ a\ comment\ with\ all\ necessary\ details.\ The\ initial\ vote\ balance\ is\ 0.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ upvote()
|
||||
comment1.text=\n\ Indicate\ that\ this\ comment\ is\ useful\ ('upvote').\ This\ is\ used\ when\ a\ reader\ clicks\n\ the\ 'Yes'\ button\ after\ the\ "Was\ this\ comment\ helpful?"\ quesion.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ downvote()
|
||||
comment2.text=\n\ Indicate\ that\ this\ comment\ is\ not\ useful\ ('downvote').\ This\ is\ used\ when\ a\ reader\n\ clicks\ the\ 'No'\ button\ after\ the\ "Was\ this\ comment\ helpful?"\ quesion.\n
|
||||
comment3.params=
|
||||
comment3.target=java.lang.String\ getAuthor()
|
||||
comment3.text=\n\ Return\ the\ author\ of\ this\ comment.\n
|
||||
comment4.params=
|
||||
comment4.target=int\ getRating()
|
||||
comment4.text=\n\ Return\ the\ rating\ of\ this\ comment.\n
|
||||
comment5.params=
|
||||
comment5.target=int\ getVoteCount()
|
||||
comment5.text=\n\ Return\ the\ vote\ count\ (balance\ of\ up\ and\ down-votes).\n
|
||||
comment6.params=
|
||||
comment6.target=java.lang.String\ getFullDetails()
|
||||
comment6.text=\n\ Return\ the\ full\ text\ and\ details\ of\ the\ comment,\ including\ \n\ the\ comment\ text,\ author\ and\ rating.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,87 @@
|
||||
|
||||
/**
|
||||
* This class represents a comment left for a sales item on an online sales site.
|
||||
* A comment consists of a comment text, a rating, and an author name. Other users
|
||||
* can indicate whether the comment was useful or not (called 'upvoting' or
|
||||
* 'downvoting'). The balance between upvotes and downvotes is stored with comments.
|
||||
* A negative vote balance means that the comment received more downvotes than upvotes.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Comment
|
||||
{
|
||||
private String author;
|
||||
private String text;
|
||||
private int rating;
|
||||
private int votes;
|
||||
|
||||
/**
|
||||
* Create a comment with all necessary details. The initial vote balance is 0.
|
||||
*/
|
||||
public Comment(String author, String text, int rating)
|
||||
{
|
||||
this.author = author;
|
||||
this.text = text;
|
||||
this.rating = rating;
|
||||
votes = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that this comment is useful ('upvote'). This is used when a reader clicks
|
||||
* the 'Yes' button after the "Was this comment helpful?" quesion.
|
||||
*/
|
||||
public void upvote()
|
||||
{
|
||||
votes++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that this comment is not useful ('downvote'). This is used when a reader
|
||||
* clicks the 'No' button after the "Was this comment helpful?" quesion.
|
||||
*/
|
||||
public void downvote()
|
||||
{
|
||||
votes--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the author of this comment.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rating of this comment.
|
||||
*/
|
||||
public int getRating()
|
||||
{
|
||||
return rating;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the vote count (balance of up and down-votes).
|
||||
*/
|
||||
public int getVoteCount()
|
||||
{
|
||||
return votes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the full text and details of the comment, including
|
||||
* the comment text, author and rating.
|
||||
*/
|
||||
public String getFullDetails()
|
||||
{
|
||||
String details = "Rating: " + "*****".substring(0,rating) + " "
|
||||
+ "By: " + author + "\n"
|
||||
+ " " + text + "\n";
|
||||
// Note: 'votes' is currently included for testing purposes only. In the final
|
||||
// application, this will nt be shown. Instead, the vote count will be used to
|
||||
// select and order the comments on screen.
|
||||
details += "(Voted as helpful: " + votes + ")\n";
|
||||
return details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
Project: online-shop
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
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
|
||||
|
||||
This project implements a small part of an online sales system (such as Amazon.com).
|
||||
The current project is concerned only with customer comments for sales items. It contains
|
||||
code to create, store, show and manipulate customer comments.
|
||||
|
||||
NOTE: There are several serious errors to be found in these classes.
|
||||
@@ -0,0 +1,41 @@
|
||||
#BlueJ class context
|
||||
comment0.params=name\ price
|
||||
comment0.target=SalesItem(java.lang.String,\ int)
|
||||
comment0.text=\n\ Create\ a\ new\ sales\ item.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ getName()
|
||||
comment1.text=\n\ Return\ the\ name\ of\ this\ item.\n
|
||||
comment10.params=rating
|
||||
comment10.target=boolean\ ratingInvalid(int)
|
||||
comment10.text=\n\ Check\ whether\ the\ given\ rating\ is\ invalid.\ Return\ true\ if\ it\ is\ invalid.\n\ Valid\ ratings\ are\ in\ the\ range\ [1..5].\n
|
||||
comment11.params=author
|
||||
comment11.target=Comment\ findCommentByAuthor(java.lang.String)
|
||||
comment11.text=\n\ Find\ the\ comment\ by\ the\ author\ with\ the\ given\ name.\n\ \n\ @return\ The\ comment\ if\ it\ exists;\ null\ if\ it\ doesn't.\n
|
||||
comment12.params=price
|
||||
comment12.target=java.lang.String\ priceString(int)
|
||||
comment12.text=\n\ For\ a\ price\ given\ as\ an\ int,\ return\ a\ readable\ String\ representing\ the\ same\ price.\n\ The\ price\ is\ given\ in\ whole\ cents.\ For\ example\ for\ price\=\=12345,\ the\ following\ String\n\ is\ returned\:\ $123.45\n
|
||||
comment2.params=
|
||||
comment2.target=int\ getPrice()
|
||||
comment2.text=\n\ Return\ the\ price\ of\ this\ item.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getNumberOfComments()
|
||||
comment3.text=\n\ Return\ the\ number\ of\ customer\ comments\ for\ this\ item.\n
|
||||
comment4.params=author\ text\ rating
|
||||
comment4.target=boolean\ addComment(java.lang.String,\ java.lang.String,\ int)
|
||||
comment4.text=\n\ Add\ a\ comment\ to\ the\ comment\ list\ of\ this\ sales\ item.\ Return\ true\ if\ successful;\n\ false\ if\ the\ comment\ was\ rejected.\n\ \n\ The\ comment\ will\ be\ rejected\ if\ the\ same\ author\ has\ already\ left\ a\ comment,\ or\n\ if\ the\ rating\ is\ invalid.\ Valid\ ratings\ are\ numbers\ between\ 1\ and\ 5\ (inclusive).\n
|
||||
comment5.params=index
|
||||
comment5.target=void\ removeComment(int)
|
||||
comment5.text=\n\ Remove\ the\ comment\ stored\ at\ the\ index\ given.\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment6.params=index
|
||||
comment6.target=void\ upvoteComment(int)
|
||||
comment6.text=\n\ Upvote\ the\ comment\ at\ 'index'.\ That\ is\:\ count\ this\ comment\ as\ more\ helpful.\n\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment7.params=index
|
||||
comment7.target=void\ downvoteComment(int)
|
||||
comment7.text=\n\ Downvote\ the\ comment\ at\ 'index'.\ That\ is\:\ count\ this\ comment\ as\ less\ helpful.\n\ If\ the\ index\ is\ invalid,\ do\ nothing.\n
|
||||
comment8.params=
|
||||
comment8.target=void\ showInfo()
|
||||
comment8.text=\n\ Show\ all\ comments\ on\ screen.\ (Currently,\ for\ testing\ purposes\:\ print\ to\ the\ terminal.\n\ Modify\ later\ for\ web\ display.)\n
|
||||
comment9.params=
|
||||
comment9.target=Comment\ findMostHelpfulComment()
|
||||
comment9.text=\n\ Return\ the\ most\ helpful\ comment.\ The\ most\ useful\ comment\ is\ the\ one\ with\ the\ highest\ vote\n\ balance.\ If\ there\ are\ multiple\ comments\ with\ equal\ highest\ balance,\ return\ any\ one\ of\n\ them.\n
|
||||
numComments=13
|
||||
@@ -0,0 +1,184 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* The class represents sales items on an online e-commerce site (such as Amazon.com).
|
||||
* SalesItem objects store all information relevant to this item, including description,
|
||||
* price, customer comments, etc.
|
||||
*
|
||||
* NOTE: The current version is incomplete! Currently, only code dealing with customer
|
||||
* comments is here.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 0.1 (2016.02.29)
|
||||
*/
|
||||
public class SalesItem
|
||||
{
|
||||
private String name;
|
||||
private int price; // in cents
|
||||
private ArrayList<Comment> comments;
|
||||
|
||||
/**
|
||||
* Create a new sales item.
|
||||
*/
|
||||
public SalesItem(String name, int price)
|
||||
{
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
comments = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of this item.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the price of this item.
|
||||
*/
|
||||
public int getPrice()
|
||||
{
|
||||
return price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of customer comments for this item.
|
||||
*/
|
||||
public int getNumberOfComments()
|
||||
{
|
||||
return comments.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a comment to the comment list of this sales item. Return true if successful;
|
||||
* false if the comment was rejected.
|
||||
*
|
||||
* The comment will be rejected if the same author has already left a comment, or
|
||||
* if the rating is invalid. Valid ratings are numbers between 1 and 5 (inclusive).
|
||||
*/
|
||||
public boolean addComment(String author, String text, int rating)
|
||||
{
|
||||
if(ratingInvalid(rating)) { // reject invalid ratings
|
||||
return false;
|
||||
}
|
||||
|
||||
if(findCommentByAuthor(author) != null) { // reject mutiple comments by same author
|
||||
return false;
|
||||
}
|
||||
|
||||
comments.add(new Comment(author, text, rating));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the comment stored at the index given. If the index is invalid, do nothing.
|
||||
*/
|
||||
public void removeComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upvote the comment at 'index'. That is: count this comment as more helpful.
|
||||
* If the index is invalid, do nothing.
|
||||
*/
|
||||
public void upvoteComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.get(index).upvote();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downvote the comment at 'index'. That is: count this comment as less helpful.
|
||||
* If the index is invalid, do nothing.
|
||||
*/
|
||||
public void downvoteComment(int index)
|
||||
{
|
||||
if(index >=0 && index < comments.size()) { // if index is valid
|
||||
comments.get(index).downvote();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all comments on screen. (Currently, for testing purposes: print to the terminal.
|
||||
* Modify later for web display.)
|
||||
*/
|
||||
public void showInfo()
|
||||
{
|
||||
System.out.println("*** " + name + " ***");
|
||||
System.out.println("Price: " + priceString(price));
|
||||
System.out.println();
|
||||
System.out.println("Customer comments:");
|
||||
for(Comment comment : comments) {
|
||||
System.out.println("-------------------------------------------");
|
||||
System.out.println(comment.getFullDetails());
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("===========================================");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the most helpful comment. The most useful comment is the one with the highest vote
|
||||
* balance. If there are multiple comments with equal highest balance, return any one of
|
||||
* them.
|
||||
*/
|
||||
public Comment findMostHelpfulComment()
|
||||
{
|
||||
Iterator<Comment> it = comments.iterator();
|
||||
Comment best = it.next();
|
||||
while(it.hasNext()) {
|
||||
Comment current = it.next();
|
||||
if(current.getVoteCount() > best.getVoteCount()) {
|
||||
best = current;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given rating is invalid. Return true if it is invalid.
|
||||
* Valid ratings are in the range [1..5].
|
||||
*/
|
||||
private boolean ratingInvalid(int rating)
|
||||
{
|
||||
return rating < 0 || rating > 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the comment by the author with the given name.
|
||||
*
|
||||
* @return The comment if it exists; null if it doesn't.
|
||||
*/
|
||||
private Comment findCommentByAuthor(String author)
|
||||
{
|
||||
for(Comment comment : comments) {
|
||||
if(comment.getAuthor().equals(author)) {
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* For a price given as an int, return a readable String representing the same price.
|
||||
* The price is given in whole cents. For example for price==12345, the following String
|
||||
* is returned: $123.45
|
||||
*/
|
||||
private String priceString(int price)
|
||||
{
|
||||
int dollars = price / 100;
|
||||
int cents = price - (dollars*100);
|
||||
if(cents <= 9) {
|
||||
return "$" + dollars + ".0" + cents; // include zero padding if necessary
|
||||
}
|
||||
else {
|
||||
return "$" + dollars + "." + cents;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=SalesItem
|
||||
dependency1.to=Comment
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=753
|
||||
package.editor.height=431
|
||||
package.editor.width=645
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
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=921
|
||||
target1.editor.x=62
|
||||
target1.editor.y=59
|
||||
target1.height=60
|
||||
target1.name=Comment
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=290
|
||||
target1.y=210
|
||||
target2.editor.height=752
|
||||
target2.editor.width=937
|
||||
target2.editor.x=53
|
||||
target2.editor.y=49
|
||||
target2.height=60
|
||||
target2.name=SalesItem
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=100
|
||||
target2.x=160
|
||||
target2.y=110
|
||||
Reference in New Issue
Block a user