943,815 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 472
  • Java RSS
Dec 30th, 2008
0

Can Someone Help Please

Expand Post »
hey can anyone help me with some work please i have been asked
to:
provide information about the number and total amount of purchases from each suppliers each month, the number of sales and total price for each category of animal each month.

at the moment i have got the information of a supplier order in an arraylist this includes the name of the supplier, price date,and animal. but i really not sure on how to get this information or work it out when when i have got the info all i want to do is print to a jtextarea which i can do.

so any help will be much appericated Thanks
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 31st, 2008
0

Re: Can Someone Help Please

we don't know how you store the data in that ArrayList, so how can we tell?

let's say that you store your data in there as a object of a type 'Person' which has (among others) a String variable name and a String variable lastName.

Java Syntax (Toggle Plain Text)
  1. int location = // the location of the person in your ArrayList
  2. Person stored = (Person)myList.get(location);
  3. JTextArea tArea = new JTextArea();
  4. String in = stored.getName() + "\n" + stored.getLastName();
  5. tArea.setText(in);

is this what you were looking for?
Last edited by stultuske; Dec 31st, 2008 at 4:27 am. Reason: corrected code-tags
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,493 posts
since Jan 2007
Dec 31st, 2008
0

Re: Can Someone Help Please

If your class that you're making an ArrayList of is called Supplier, write a toString method in the Supplier class. The toString method should return a String that contains all the information (name, etc) for a Supplier. Then, for each Supplier in the ArrayList, do yourTextArea.append(Supplier.toString())
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Dec 31st, 2008
0

Re: Can Someone Help Please

hi thanks for the replies everyone. i have done a to string method to get it to display my information its just i dont know how to calculate the cost of that certain supplier and how many times it has been purchased from. any help with this cause i really am stuck thanks
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Jan 1st, 2009
0

Re: Can Someone Help Please

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.
Quote ...
Animal:
-name
-price
Purchase is relate to date and Animal
Quote ...
Purchase:
-date
-Animal
Supplier have a name and a list of Purchase
Quote ...
Supplier:
-name
-List<Purchase> purchaseList
Who have a list of suppliers ? - Owner.
Quote ...
Owner:
-List<Supplier> supplierList
Four classes are ready
Quote ...
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:
java Syntax (Toggle Plain Text)
  1. public String toString() {
  2. StringBuffer sb = new StringBuffer();
  3. sb.append("Animal:");
  4. sb.append("name=");
  5. sb.append(name);
  6. sb.append(",");
  7. sb.append("price=");
  8. sb.append(price);
  9. sb.append("|");
  10. return sb.toString();
  11. }
For Purchase:
java Syntax (Toggle Plain Text)
  1. public String toString() {
  2. StringBuffer sb = new StringBuffer();
  3. sb.append("Purchase:");
  4. sb.append("Date=");
  5. sb.append(date);
  6. sb.append(",");
  7. sb.append(animal);
  8. return sb.toString();
  9. }
For Supplier:
java Syntax (Toggle Plain Text)
  1. public String toString() {
  2. StringBuffer sb = new StringBuffer();
  3. sb.append("Supplier:");
  4. sb.append("name=");
  5. sb.append(name);
  6. sb.append(",");
  7. for (Purchase inner : purchaseList) {
  8. sb.append(inner);
  9. }
  10. return sb.toString();
  11. }
For Owner:
java Syntax (Toggle Plain Text)
  1. public String toString() {
  2. StringBuffer sb = new StringBuffer();
  3. sb.append("Owner:");
  4. //TODO
  5. return sb.toString();
  6. }
DATABASE FILLING
java Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2. import java.util.Date;
  3. import java.util.List;
  4.  
  5. public class Superstar {
  6.  
  7. Owner owner;
  8.  
  9. public Superstar() {
  10. owner = new Owner();
  11. String supplierName = "Smith";
  12. Date date = new Date();
  13. String animalName = "dog";
  14. int price = 100;
  15. owner.add(supplierName, date, animalName, price);
  16. date = new Date();
  17. long time = date.getTime() - 8888888888L;
  18. date.setTime(time);
  19. owner.add(supplierName, date, "cow", 1000);
  20. owner.add("Byron", date, "cat", 5);
  21. System.out.println(owner);
  22. //first simple question
  23. int numberOfSales = owner.getNumberOfSales();
  24. System.out.println("numberOfSales=" + numberOfSales);
  25.  
  26. }
  27. public static void main(String[] args) {
  28. new Superstar();
  29. }
  30. }
Java Syntax (Toggle Plain Text)
  1. public class Owner {
  2.  
  3. private List<Supplier> supplierList = new ArrayList<Supplier>();
  4.  
  5. // by name
  6. public Supplier getSupplier(String supplierName) {
  7. for (Supplier inner : supplierList) {
  8. if (inner.getName().equals(supplierName)) {
  9. return inner;
  10. }
  11. }
  12. return (Supplier) null;
  13. }
  14.  
  15. public void add(String supplierName, Date currDate, String animalName, int animalPrice) {
  16. Animal animal = new Animal(animalName, animalPrice);
  17. Purchase purchase = new Purchase(currDate, animal);
  18. // check for supplier
  19. Supplier supplier = getSupplier(supplierName);
  20. if (supplier == null) {
  21. supplier = new Supplier(supplierName, purchase);
  22. // add new suplier to supplierList
  23. supplierList.add(supplier);
  24. } else {
  25. //supplier exist,add
  26. supplier.add(purchase);
  27. }
  28. }
  29.  
  30. @Override
  31. public String toString() {
  32. StringBuffer sb = new StringBuffer();
  33. sb.append("Owner:");
  34. for (Supplier inner : supplierList) {
  35. sb.append("\n");
  36. sb.append(inner);
  37. }
  38. return sb.toString();
  39. }
  40.  
  41. public int getNumberOfSales() {
  42. int numberOfSales = 0;
  43. for (Supplier inner : supplierList) {
  44. numberOfSales += inner.getNumberOfSales();
  45. }
  46. return numberOfSales;
  47. }
  48. }
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
Quote ...
SupplierPrice:
-name // String supplierName
-price // int/double sumOfPricesForCurrentSuplier
...
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Jan 1st, 2009
0

Re: Can Someone Help Please

wow thanks very much for that i will try and implement this into my program and get it to work. all this is much apperciated and have a happy new year
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Network Monitoring
Next Thread in Java Forum Timeline: Help with java swing!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC