Chapter 12
<< Chapter 11 | HomeworkTrailIndex | Chapter 13 >>
Object-Oriented Design

Discover classes
Nouns are possible classes
Invoice Address LineItem Product Description Price Quantity Total Amount Due
Analyze Classes
Invoice Address LineItem // Records the product and the quantity Product Description // Field of the Product class Price // Field of the Product class Quantity // Not an attribute of a Product Total // Computed – not stored anywhere Amount Due // Computed – not stored anywhere
Classes after a process of elimination
Invoice Address LineItem Product
CRC Cards for printing Invoice

Invoice needs to be populated with products and quantities




UML Diagram

InvoicePrinter.java
/**
This program demonstrates the invoice classes by printing
a sample invoice.
*/
public class InvoicePrinter
{
public static void main(String[] args)
{
Address samsAddress
= new Address("Sam's Small Appliances",
"100 Main Street", "Anytown", "CA", "98765");
Invoice samsInvoice = new Invoice(samsAddress);
samsInvoice.add(new Product("Toaster", 29.95), 3);
samsInvoice.add(new Product("Hair dryer", 24.95), 1);
samsInvoice.add(new Product("Car vacuum", 19.99), 2);
System.out.println(samsInvoice.format());
}
}
How to format the "tabs"
String r = " I N V O I C E\n\n"
+ billingAddress.format()
+ String.format("\n\n%-30s%8s%5s%8s\n",
"Description", "Price", "Qty", "Total");
Review Exercises
Programming Exercises
FreeResponse #1 from Trees, pages 242-244. A used car lot contains cars of various makes and models.
Implement the Car class which should contain info about the car's year, make, mileage, and price. Write the CarLot methods addCar, printCarsInLot, and findMatchingCars
Car.java
import java.text.DecimalFormat;
public class Car
{
//Your code here.
//Include fields, constructors and at least accessor methods for your fields
// Remember that all Constructors should initialize all fields
public String toString()
{
DecimalFormat dollar= new DecimalFormat("$00,000.00");
DecimalFormat odometer = new DecimalFormat("000,000.0");
return make+"\t "+year+"\t "+odometer.format(mileage)+"\t "+dollar.format(price);
}
}
CarLot.java
import java.util.ArrayList;
public class CarLot
{
private static final int MAX_CARS = 100;
private Car[] lot;
private static int numberOfCars = 0;
/**
* Constructs a CarLot (array of Cars)
*/
public CarLot()
{
lot=new Car[MAX_CARS];
}
/**
* Adds a car to car lot.
* Precondition: numberOfCars < MAX_CARS
* @param aNewCar a car to be added to this car lot
*/
public void addCar(Car aNewCar)
{
// Your code here
}
/**
* Prints the info about the cars in the car lot
* (print the index and the car in that spot)
*/
public void printCarsInLot()
{
//Your code here
}
/**
* Fills the ArrayList with cars in the car lot that
* have the same year and make as the parameters
* passes to the method
* @param year the year to search for
* @param make the make of car to search for
*/
public ArrayList<Car> findMatchingCars(int year, String make)
{
//Your code here
return null;
}
/**
* Prints the year, make, mileage, and price of each
* car in the car lot that has the same year and make
* as the method parameters
* @param year the year to search for
* @param make the make of car to search for
*
*/
public void printMatchingCars(int year, String make)
{
//Your code here
}
/**
* Removes Car from lot
*/
public void removeCar(int i)
{
if(lot[i]!=null)
numberOfCars--;
lot[i]=null;
}
}
CarLotTester.java
public class CarLotTester {
/**
* Tests the CarLot class
*/
public static void main(String[] args) {
System.out.println("Stage 1 Testing: Make 3 Cars and Print Lot");
// make a lot and add a few cars
CarLot usedCarLot = new CarLot();
usedCarLot.addCar(new Car("Toyota",2008,230000.2,11300.0));
usedCarLot.addCar(new Car("Toyota",2008,120000.2,14300.0));
usedCarLot.addCar(new Car("Ford",2008,80000.2,10700.0));
//print the lot
usedCarLot.printCarsInLot();
System.out.println("Stage 2 Testing: Print 2008 Toyotas");
usedCarLot.printMatchingCars(2008, "Toyota");
System.out.println("Expected 2 Toyotas");
System.out.println("Stage 3 Testing: Fill the lot with cars");
//fill the lot
for (int i=0;i<100-3;i++)
usedCarLot.addCar(randCar());
usedCarLot.printCarsInLot();
System.out.println("Expected no runtime errors");
System.out.println("Stage 4 Testing: Attempt to add one more car");
//attempt to add a car when lot is full
usedCarLot.addCar(new Car("Rambler", 1962, 999999.8,10.0));
System.out.println("Expected no runtime errors");
System.out.println("Stage 5 Testing: Remove car at index 97");
//sell a car
usedCarLot.removeCar(97);
usedCarLot.printCarsInLot();
System.out.println("Expected index 97 to be skipped");
System.out.println("Stage 6 Testing: Adding a car to see if it finds the empty space");
//attempt to add a car (does addCar find the spot?)
usedCarLot.addCar(new Car("Packard", 1941, 54828.1, 34000.01));
usedCarLot.printCarsInLot();
System.out.println("Expected a Packard at index 97");
}
public static int randInt(int min, int max)
{
return min+(int)(Math.random()*(max-min+1));
}
public static double randDouble(double min, double max)
{
return min+Math.random()*(max-min+1.0);
}
public static Car randCar()
{
String[] make={"Toyota","Ford","Chev","Honda","BMW","Fiat"};
return new Car(make[randInt(0,make.length-1)],
randInt(1989,2010),
randDouble(40000.0,300000.0),
randDouble(4000.0,30000.0));
}
}
