Chapter 5 · Real programs

A project of your own

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 20 · Concept

One project, three packages

This lesson is one project: a small inventory program for a shop. It reads the stock from a file and keeps one object per item. It refuses lines that make no sense, and prints a report.

Every piece comes from an earlier lesson. Each exercise leaves a method or two for you to write; the rest is there already.

package app;

import app.model.Item;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path file = Path.of("inventory.txt");
        for (String line : Files.readAllLines(file)) {
            System.out.println(line);
        }
        Item apple = new Item("apple", 12, 30);
        System.out.println(apple);
        System.out.println("Value: " + apple.value());
    }
}
package app;

import app.model.Inventory;
import app.model.Item;

public class Main {
    public static void main(String[] args) {
        Inventory inv = new Inventory();
        inv.add(new Item("apple", 12, 30));
        inv.add(new Item("pear", 3, 45));
        inv.add(new Item("banana", 0, 25));
        try {
            inv.add(new Item("pear", 7, 45));
        } catch (IllegalArgumentException e) {
            System.out.println("Refused: " + e.getMessage());
        }
        for (Item item : inv.lowStock(5)) {
            System.out.println("Low: " + item.name());
        }
        System.out.println("Total value: " + inv.totalValue());
    }
}
package app;

import app.io.BadLineException;
import app.io.LineParser;
import app.model.Item;

public class Main {
    public static void main(String[] args) {
        String[] lines = {
            "apple,12,30", "fig,x,80", "plum,-4,60", "lemon,8"
        };
        for (String line : lines) {
            try {
                Item item = LineParser.parse(line);
                System.out.println("OK: " + item);
            } catch (BadLineException e) {
                System.out.println("Bad: " + e.getMessage());
            }
        }
    }
}
package app;

import app.io.InventoryFile;
import app.model.Inventory;
import java.io.IOException;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path file = Path.of("inventory.txt");
        Inventory inv = InventoryFile.load(file);
        System.out.println("Loaded " + inv.size() + " items");
    }
}
public class Widths {
    public static void main(String[] args) {
        System.out.printf("%-6s|%4d|%n", "fig", 3);
        System.out.printf("%-6s|%4d|%n", "banana", 12);
    }
}
package app;

import app.io.InventoryFile;
import app.model.Inventory;
import java.io.IOException;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path file = Path.of("inventory.txt");
        Inventory inv = InventoryFile.load(file);
        Report.printTable(inv);
        Report.printLowStock(inv, 5);
    }
}
package app;

import app.io.InventoryFile;
import app.model.Inventory;
import java.io.IOException;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path file = Path.of("inventory.txt");
        Inventory inv = InventoryFile.load(file);
        Report.printTable(inv);
        Report.printLowStock(inv, 5);
    }
}
package app;

import app.io.InventoryFile;
import app.model.Inventory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path data = Path.of("inventory.txt");
        Inventory inv = InventoryFile.load(data);
        Path out = Path.of("restock.txt");
        Files.deleteIfExists(out);
        InventoryFile.saveRestock(inv, 5, out);
        if (Files.exists(out)) {
            System.out.print(Files.readString(out));
        } else {
            System.out.println("There is no restock.txt");
        }
    }
}
Terminal
Step 1 of 8The project and its data

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)