๐ป Unit 1 Project: Digital Receipt
Overview
๐ฅ PROJECT SETUP & SUBMISSION INSTRUCTIONS
- Go to the
CS2 Unit 1 Project
assignment on Blackbaud and follow the provided GitHub Classroom link.๐ Clicking the link generates a private repository for your project with the appropriate starter code. Note that projects are stored within the BWL-CS Organization, so you cannot access it from the โYour Repositoriesโ page!
- Open the repository in a Codespace whenever you spend time working on the program, in class or at home.
โ ๏ธ Always remember to
commit changes
after every coding session! - When your project is complete, submit the link to your repository in the
CS2 Unit 1 Project
assignment on Blackbaud.
๐๏ธ Whenever we shop or dine out, we often spend money and receive a receipt in return. This receipt serves as proof of our purchase. The businessโs register uses specialized software that prints the receipt in a specific format, containing certain predetermined details.
PART A: Basic Receipt
- Open your IDE and create a new file called
Receipt1.java
. - Copy and paste (or retype) the following starter code into your new file:
public class Receipt1
{
public static void main(String [] args)
{
System.out.println("**************************************");
System.out.println("* *");
System.out.println("* High School Snack Bar *");
System.out.println("* *");
System.out.println("* Drink ..........$1.50 *");
System.out.println("* Candy ..........$1.25 *");
System.out.println("* Hot Dog ........$2.75 *");
System.out.println("* Hamburger ......$3.50 *");
System.out.println("* *");
System.out.println("**************************************");
}
}
- Run the program.
- If any errors occur, fix them so the program compiles and runs successfully.
- Save the file โ you will reuse this code in the next activity.
๐ก HINTS:
-
A syntax error is a mistake in the program where the rules of the programming language are not followed. These errors are detected by the compiler.
-
A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. These errors are detected by testing the program with specific data to see if it produces the expected outcome.
-
A run-time error is a mistake in the program that occurs during the execution of a program and typically causes the program to terminate abnormally.
-
An exception is a type of run-time error that occurs as a result of an unexpected error that was not detected by the compiler. It interrupts the normal flow of the programโs execution.
PART B: Enhanced Receipt
- In your IDE, create a new file called
Receipt2.java
. - Open the code from
Receipt1.java
and copy it into your new file. - Change the class name to
Receipt2
so it matches the file name. - Create a variable to store your high schoolโs name.
- Create separate variables to store the prices of each snack bar item.
- Choose the most appropriate data type for each variable.
- Replace the literal values in
System.out.println
with string concatenation using your variables. - If your high school name is too long for one line, abbreviate it or split it onto two lines.
- Run the program and debug if needed.
- Make sure the receipt displays correctly with your variables.
๐ก HINTS:
-
The variable for high school name should be of type
String
because it will contain letters, and the variables for the cost of a drink, candy, hot dog, and hamburger should be of typedouble
because each of them will contain a real number value. -
The syntax for declaring a variable is
dataType variableName = initialValue ;
. -
To correctly concatenate the variable name
highSchoolName
with the literal โSnack Barโ in the print statement, the syntax should behighSchoolNameSystem.out.printIn("* " + highSchoolName + " Snack Bar *");
-
To correctly concatenate a variable name
itemName
with a real numberitemCost
in the print statement, the syntax should beSystem.out.printIn("* " + itemName + ".............$" + itemCost +" *");
PART C: Adding Random
- Create a new file called
Receipt3.java
. - Copy the code from
Receipt2.java
into your new file. - Change the class name to
Receipt3
. -
Add variables for:
- Order number
- Number of drinks ordered
- Number of candies ordered
- Number of hot dogs ordered
- Number of hamburgers ordered
- Tax rate
- Subtotal
- Total tax
- Total
-
Use random numbers to:
- Assign each item a quantity between 0 and 2 (inclusive).
- Assign an order number between 1 and 100 (inclusive).
-
Update the receipt printout to include:
- Order number
- Quantity, name, and cost for each item
- Total cost for the order
- Run and debug the program.
๐ก HINTS:
-
The variables for tax rate, subtotal, total tax, and total should be of type
double
because each will contain a real number value. The variables for order number and the number of drinks, candies, hot dogs, and hamburgers should be of typeint
because each will contain a whole number. -
To generate the order number, which should be a random number between 1 and 100, inclusive, the
Math.random()
method should be used. The general form of generating a random number between low and high is(int)(Math.random() * (high - low + 1) + low)
. For the order number example, the code should be(int)(Math.random() * 100 + 1)
-
If the variable
numDrinks
contains the value that is randomly generated and the variabledrinkCost
contains the value of the cost per drink, then to calculate the total cost for the drinks, you would use the expressionnumDrinks * drinkCost
. -
The subtotal can be found by adding each of the item totals. The value for the tax can be found by multiplying the subtotal and tax rate. The order total can be found by adding the subtotal and the tax.
-
The escape sequence for adding a new line to an output is
"\n"
. The escape sequence for adding a tab to an output is"\t"
.
PART D: Interactive Receipt
- Create a new file called
Receipt4.java
. - Copy the code from
Receipt3.java
into your new file. - Change the class name to
Receipt4
. - Import the Scanner library at the top of the file:
import java.util.Scanner;
- Create a
Scanner
object for keyboard input:Scanner input = new Scanner(System.in);
-
Prompt the user to enter:
- Number of drinks
- Number of candies
- Number of hot dogs
- Number of hamburgers
- Store these values in the appropriate variables.
- Prompt the user to enter the full high school name.
- Use String methods to create initials from the first letters of each word (e.g., โBirch Wathen Lenoxโ โ โBWLโ).
- Replace the high school name on the receipt with the generated initials.
- Calculate the orderโs total cost using the userโs input values.
- Print the updated receipt.
- Run and debug the program.
๐ก HINTS:
-
The placement of the statement
import java.util.Scanner;
must be before thepublic class Receipt4
header. -
The placement of the statement
Scanner input = new Scanner(System.in);
should be after the headerpublic static void main(String[] args)
. -
If the variable
nameOfSchool
contains the four-word name of the high school, the String method substring can be used to extract the first letter. This would yield the statementfirstLetter = nameOfSchool.substring(0, 1);
. -
To find the position of the first space in the high schoolโs name, the String method
indexOf
can be used. This would yield the statementint position = nameOfSchool.indexOf(" ");
. -
Once the position of the space is located, the String method
substring
can be used to get the remaining words. This would yield the statementremainingWords = nameOfSchool.substring(position+1);
.
PART E: Design a Receipt
- Choose a business type for your custom receipt (e.g., store, cafรฉ, personal business).
-
Write down details about the business:
- What it sells
- Information shown on the receipt (store name, location, date, item descriptions, prices, quantities, etc.)
- Decide how many items your receipt will include.
- Draw your receipt on paper to plan the layout.
- Identify all the variables needed to store your data.
-
Assign data to the variables:
- Some set with literal values
- Some set from user input
-
Add creativity by including extra features:
- Generate a receipt number using part of the business name plus a random number.
- Apply a random โsurpriseโ discount (1%โ10%).
- Use String and Math methods from the Java Quick Reference.
- Write your Java code in a file named
Receipt5.java
. - Test the program to ensure it displays correctly and works as intended.
๐ก HINTS:
- Because of the way decimal numbers are stored, the values of a double variable value will print many decimal places. One way to display only two decimal places would be to use the statement
value = ((int)(value * 100)/100.0);
.
Acknowledgement
Content on this page is adapted from CollegeBoard.