๐Ÿ’ป Unit 1 Project: Digital Receipt

Overview

๐Ÿ“ฅ PROJECT SETUP & SUBMISSION INSTRUCTIONS
  1. 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!

  2. 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!

  3. 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

  1. Open your IDE and create a new file called Receipt1.java.
  2. 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("**************************************");
   }
}
  1. Run the program.
  2. If any errors occur, fix them so the program compiles and runs successfully.
  3. 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

  1. In your IDE, create a new file called Receipt2.java.
  2. Open the code from Receipt1.java and copy it into your new file.
  3. Change the class name to Receipt2 so it matches the file name.
  4. Create a variable to store your high schoolโ€™s name.
  5. Create separate variables to store the prices of each snack bar item.
  6. Choose the most appropriate data type for each variable.
  7. Replace the literal values in System.out.println with string concatenation using your variables.
  8. If your high school name is too long for one line, abbreviate it or split it onto two lines.
  9. Run the program and debug if needed.
  10. 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 type double 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 be highSchoolNameSystem.out.printIn("* " + highSchoolName + " Snack Bar *");

  • To correctly concatenate a variable name itemName with a real number itemCost in the print statement, the syntax should be System.out.printIn("* " + itemName + ".............$" + itemCost +" *");

PART C: Adding Random

  1. Create a new file called Receipt3.java.
  2. Copy the code from Receipt2.java into your new file.
  3. Change the class name to Receipt3.
  4. 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
  5. Use random numbers to:

    • Assign each item a quantity between 0 and 2 (inclusive).
    • Assign an order number between 1 and 100 (inclusive).
  6. Update the receipt printout to include:

    • Order number
    • Quantity, name, and cost for each item
    • Total cost for the order
  7. 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 type int 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 variable drinkCost contains the value of the cost per drink, then to calculate the total cost for the drinks, you would use the expression numDrinks * 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

  1. Create a new file called Receipt4.java.
  2. Copy the code from Receipt3.java into your new file.
  3. Change the class name to Receipt4.
  4. Import the Scanner library at the top of the file: import java.util.Scanner;
  5. Create a Scanner object for keyboard input: Scanner input = new Scanner(System.in);
  6. Prompt the user to enter:

    • Number of drinks
    • Number of candies
    • Number of hot dogs
    • Number of hamburgers
  7. Store these values in the appropriate variables.
  8. Prompt the user to enter the full high school name.
  9. Use String methods to create initials from the first letters of each word (e.g., โ€œBirch Wathen Lenoxโ€ โ†’ โ€œBWLโ€).
  10. Replace the high school name on the receipt with the generated initials.
  11. Calculate the orderโ€™s total cost using the userโ€™s input values.
  12. Print the updated receipt.
  13. Run and debug the program.
๐Ÿ’ก HINTS:
  • The placement of the statement import java.util.Scanner; must be before the public class Receipt4 header.

  • The placement of the statement Scanner input = new Scanner(System.in); should be after the header public 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 statement firstLetter = 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 statement int 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 statement remainingWords = nameOfSchool.substring(position+1);.

PART E: Design a Receipt

  1. Choose a business type for your custom receipt (e.g., store, cafรฉ, personal business).
  2. Write down details about the business:

    • What it sells
    • Information shown on the receipt (store name, location, date, item descriptions, prices, quantities, etc.)
  3. Decide how many items your receipt will include.
  4. Draw your receipt on paper to plan the layout.
  5. Identify all the variables needed to store your data.
  6. Assign data to the variables:

    • Some set with literal values
    • Some set from user input
  7. 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.
  8. Write your Java code in a file named Receipt5.java.
  9. 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.