Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (Buffalo State University 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 edu.buffalostate.cis425.fa10.exercises.put-your-username-here;
  
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 edu.buffalostate.cis425.fa10.exercises.put-your-username-here;
  
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 edu.buffalostate.cis425.fa10.exercises.put-your-username-here;
  
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!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland