Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Receipt Formatter | Mastering String.format()
Formatting & Parsing in Java

bookChallenge: Receipt Formatter

In this challenge, you will apply your knowledge of String.format() to create a professional-looking receipt. Your goal is to implement a method called formatReceipt that takes three lists: item names, their quantities, and their prices. The method should return a formatted string that represents a receipt, with each item's name, quantity, and price displayed in neatly aligned columns. Prices must always display two decimal places for clarity. This exercise will help you master column alignment and numeric formatting, which are essential skills for producing readable console output in Java.

Tehtävä

Swipe to start coding

Implement a method to generate a formatted receipt using String.format(). The method must align each column (item name, quantity, price) and display prices with exactly two decimal places.

  • Each item, quantity, and price must appear on its own line, formatted in columns.
  • The item name column should be left-aligned and wide enough for the longest item.
  • The quantity column should be right-aligned.
  • The price column should be right-aligned and always display two digits after the decimal point.
  • The receipt must include a header row and a separator row before the item rows.
  • The method must return the formatted receipt as a single string.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you show me an example of the expected receipt output?

What should happen if the lists have different lengths?

Should the receipt include a total price at the end?

close

bookChallenge: Receipt Formatter

Pyyhkäise näyttääksesi valikon

In this challenge, you will apply your knowledge of String.format() to create a professional-looking receipt. Your goal is to implement a method called formatReceipt that takes three lists: item names, their quantities, and their prices. The method should return a formatted string that represents a receipt, with each item's name, quantity, and price displayed in neatly aligned columns. Prices must always display two decimal places for clarity. This exercise will help you master column alignment and numeric formatting, which are essential skills for producing readable console output in Java.

Tehtävä

Swipe to start coding

Implement a method to generate a formatted receipt using String.format(). The method must align each column (item name, quantity, price) and display prices with exactly two decimal places.

  • Each item, quantity, and price must appear on its own line, formatted in columns.
  • The item name column should be left-aligned and wide enough for the longest item.
  • The quantity column should be right-aligned.
  • The price column should be right-aligned and always display two digits after the decimal point.
  • The receipt must include a header row and a separator row before the item rows.
  • The method must return the formatted receipt as a single string.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6
single

single

some-alt