CONCEPT (paper & pencil)
This is database.
You have a input Record: (supplierName, date, animalName, price).
Now is time to make a model. Only 4 fields. What is relation beetwen them. I started from end. Pair (animalName, price) is inextricably linked.
Purchase is relate to date and Animal
Supplier have a name and a list of Purchase
Supplier:
-name
-List<Purchase> purchaseList
Who have a list of suppliers ? - Owner.
Owner:
-List<Supplier> supplierList
Four classes are ready
Animal
Purchase
Supplier
Owner
where name is String-type, data is Data-type, price is int or float.
Make a constructors with suitable parameters and initialize Lists.
For all classes write toString() methods.
For example Animal:
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Animal:");
sb.append("name=");
sb.append(name);
sb.append(",");
sb.append("price=");
sb.append(price);
sb.append("|");
return sb.toString();
}
For Purchase:
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Purchase:");
sb.append("Date=");
sb.append(date);
sb.append(",");
sb.append(animal);
return sb.toString();
}
For Supplier:
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Supplier:");
sb.append("name=");
sb.append(name);
sb.append(",");
for (Purchase inner : purchaseList) {
sb.append(inner);
}
return sb.toString();
}
For Owner:
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Owner:");
//TODO
return sb.toString();
}
DATABASE FILLING
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Superstar {
Owner owner;
public Superstar() {
owner = new Owner();
String supplierName = "Smith";
Date date = new Date();
String animalName = "dog";
int price = 100;
owner.add(supplierName, date, animalName, price);
date = new Date();
long time = date.getTime() - 8888888888L;
date.setTime(time);
owner.add(supplierName, date, "cow", 1000);
owner.add("Byron", date, "cat", 5);
System.out.println(owner);
//first simple question
int numberOfSales = owner.getNumberOfSales();
System.out.println("numberOfSales=" + numberOfSales);
}
public static void main(String[] args) {
new Superstar();
}
}
public class Owner {
private List<Supplier> supplierList = new ArrayList<Supplier>();
// by name
public Supplier getSupplier(String supplierName) {
for (Supplier inner : supplierList) {
if (inner.getName().equals(supplierName)) {
return inner;
}
}
return (Supplier) null;
}
public void add(String supplierName, Date currDate, String animalName, int animalPrice) {
Animal animal = new Animal(animalName, animalPrice);
Purchase purchase = new Purchase(currDate, animal);
// check for supplier
Supplier supplier = getSupplier(supplierName);
if (supplier == null) {
supplier = new Supplier(supplierName, purchase);
// add new suplier to supplierList
supplierList.add(supplier);
} else {
//supplier exist,add
supplier.add(purchase);
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Owner:");
for (Supplier inner : supplierList) {
sb.append("\n");
sb.append(inner);
}
return sb.toString();
}
public int getNumberOfSales() {
int numberOfSales = 0;
for (Supplier inner : supplierList) {
numberOfSales += inner.getNumberOfSales();
}
return numberOfSales;
}
}
This proposal of computer program has a "hierarchical" structure in sense that all user questions are directed to owner. Question goes down to structure.
Try implement:
int sumOfPrices = owner.getSumOfPrices();
List<String> supplierNames = owner.getSupplierNames();
List<String> animalNames = owner.getAnimalNames();
List<String> uniqueAnimalNames = owner.getUniqueAnimalNames();
Iterate throught List<Supplier> you can ask owner.getSumOfPrices(supplierName); to obtain List<SupplierPrice> where
SupplierPrice:
-name // String supplierName
-price // int/double sumOfPricesForCurrentSuplier
...