Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 10: Working With Classes

Most Java applications are made up of multiple classes. As long as the class is in the same package then it can be referenced from any other class in the same package.

The next three (3) figures show the classes that make up a storefront inventory report. The Storefront object is a "super" class. The GiftShop is a Storefront object and the catalog is stored in an ArrayList of Item objects.

package buffalostate.edu.sp18.your-last-name;
  
import java.util.LinkedList;
import java.util.Collections;
  
public class StoreFront {

  private LinkedList catalog = new LinkedList();
  
  public void addItem (String id, String name, String price, String quant) {
    Item it = new Item(id, name, price, quant);
    catalog.add(it);
  }

  public Item getItem(int i) {
    return (Item) catalog.get(i);
  }

  public int getSize() {
    return catalog.size();
  }

  public void sort() {
    Collections.sort(catalog);
  }
}
Figure 10-1 Storefront.java Code
package buffalostate.edu.sp18.your-last-name;
  
public class GiftShop {

  public static void main(String[] args) {
    Storefront store = new Storefront();
    store.addItem("C01", "MUG", "9.99", "150");
    store.addItem("C02", "LG MUG", "12.99", "82");
    store.addItem("C03", "MOUSEPAD", "10.49", "800");
    store.addItem("D01", "T SHIRT", "16.99", "90");
    store.sort();

    for (int i = 0; i < store.getSize(); i++) {
      Item show = (Item) store.getItem(i);
      System.out.println("\nItem ID: " + show.getId() +
      "\nName: " + show.getName() +
      "\nRetail Price: $" + show.getRetail() +
      "\nPrice: $" + show.getPrice() +
      "\nQuantity: " + show.getQuantity());
    }
  }
}
Figure 10-2 GiftShop.java Code
package buffalostate.edu.sp18.your-last-name;
  
public class Item implements Comparable {

  private String id;
  private String name;
  private double retail;
  private int quantity;
  private double price;

  Item(String idIn, String nameIn, String retailIn, String qIn) {
    id = idIn;
    name = nameIn;
    retail = Double.parseDouble(retailIn);
    quantity = Integer.parseInt(qIn);
    if (quantity > 400) {
      price = retail * .5D;
    } else if (quantity > 200) {
      price = retail * .6D;
    } else {
      price = retail * .7D;
    }
    price = Math.floor( price * 100 + .5 ) / 100;
  }

  public int compareTo(Object obj) {
    Item temp = (Item) obj;
    if (this.price < temp.price) {
      return -1;
    } else if (this.price > temp.price) {
      return -1;
    }
    return 0;
  }

  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public double getRetail() {
    return retail;
  }

  public int getQuantity() {
    return quantity;
  }

  public double getPrice() {
    return price;
  }
}
Figure 10-3 Item.java Code
Item ID: D01
Name: T SHIRT
Retail Price: $16.99
Price: $11.89
Quantity: 90

Item ID: C03
Name: MOUSEPAD
Retail Price: $10.49
Price: $5.25
Quantity: 800

Item ID: C02
Name: LG MUG
Retail Price: $12.99
Price: $9.09
Quantity: 82

Item ID: C01
Name: MUG
Retail Price: $9.99
Price: $6.99
Quantity: 150
Figure 10-4 GiftShop Ourtput

Let's get started with a Multiple Classes Application!

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland