Hi Xsi,
IMO the best would be, when I post you my little working example, you take a look at it, try to understand it and then have questions. I didn't use naming like Class1, Class2, ...etc, because after a while I don't know what for is which class
#1. I created the data-class. It holds data in an ArrayList of Records.
1a) So first I created the class for one array element
Record. It contains a data structure to hold my financial expenses (amount, date and a note which tells me what for I payed). Getters/setters were generated using the IDE (I have eclipse)
1b) Then I created the class
RecordSet which contains an the ArrayList of
Record and some methods to work with - which will be used later.
RecorSet.java
Code:
[COLOR=#800080]import[/color] java.util.ArrayList;
[COLOR=#2e8b57][b]final[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Record {
[COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] amount;
[COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] datum;
[COLOR=#2e8b57][b]private[/b][/color] String note;
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] getAmount() {
[COLOR=#a52a2a][b]return[/b][/color] amount;
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setAmount([COLOR=#2e8b57][b]double[/b][/color] amount) {
[COLOR=#2e8b57][b]this[/b][/color].amount = amount;
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] getDatum() {
[COLOR=#a52a2a][b]return[/b][/color] datum;
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setDatum([COLOR=#2e8b57][b]int[/b][/color] datum) {
[COLOR=#2e8b57][b]this[/b][/color].datum = datum;
}
[COLOR=#2e8b57][b]public[/b][/color] String getNote() {
[COLOR=#a52a2a][b]return[/b][/color] note;
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] setNote(String note) {
[COLOR=#2e8b57][b]this[/b][/color].note = note;
}
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSet {
[COLOR=#2e8b57][b]private[/b][/color] ArrayList<Record> recSet = [COLOR=#a52a2a][b]new[/b][/color] ArrayList<Record>();
[COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] recSetSize = [COLOR=#ff00ff]0[/color];
[COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] j = [COLOR=#ff00ff]0[/color];
[COLOR=#0000ff]// data from record on which the cursor position shows[/color]
[COLOR=#2e8b57][b]private[/b][/color] Record currentRecord = [COLOR=#a52a2a][b]new[/b][/color] Record();
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] add([COLOR=#2e8b57][b]double[/b][/color] amount, [COLOR=#2e8b57][b]int[/b][/color] datum, String note) {
Record rec = [COLOR=#a52a2a][b]new[/b][/color] Record();
rec.setAmount(amount);
rec.setDatum(datum);
rec.setNote(note);
[COLOR=#2e8b57][b]this[/b][/color].recSet.add(rec);
}
[COLOR=#2e8b57][b]private[/b][/color] [COLOR=#2e8b57][b]boolean[/b][/color] hasNext() {
[COLOR=#2e8b57][b]this[/b][/color].recSetSize = recSet.size();
[COLOR=#a52a2a][b]return[/b][/color] (j < [COLOR=#2e8b57][b]this[/b][/color].recSetSize);
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]boolean[/b][/color] next() {
[COLOR=#2e8b57][b]boolean[/b][/color] nextElementExist = hasNext();
[COLOR=#a52a2a][b]if[/b][/color] (nextElementExist) {
[COLOR=#0000ff]// fetch data in record[/color]
[COLOR=#2e8b57][b]this[/b][/color].currentRecord = [COLOR=#2e8b57][b]this[/b][/color].recSet.get(j++);
}
[COLOR=#a52a2a][b]return[/b][/color] nextElementExist;
}
[COLOR=#0000ff]// Getters for getting data from currentRecord[/color]
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]double[/b][/color] getAmount() {
[COLOR=#a52a2a][b]return[/b][/color] currentRecord.getAmount();
}
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] getDatum() {
[COLOR=#a52a2a][b]return[/b][/color] currentRecord.getDatum();
}
[COLOR=#2e8b57][b]public[/b][/color] String getNote() {
[COLOR=#a52a2a][b]return[/b][/color] currentRecord.getNote();
}
}
#2. I wrote the class
RecordSetFill which fills the data into the
RecordSet object. It has only one method
fillInData(). This method creates the instance of
RecordSet, then adds three Records into RecordSet using the method RecordSet.add() and at end returns the RecordSet object filled with data.
RecordSetFill.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSetFill {
[COLOR=#2e8b57][b]public[/b][/color] RecordSet fillInData() {
RecordSet result = [COLOR=#a52a2a][b]new[/b][/color] RecordSet();
[COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
System.out.println([COLOR=#ff00ff]"Start filling records:"[/color]);
System.out.printf([COLOR=#ff00ff]" record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
result.add([COLOR=#ff00ff]44.02[/color], [COLOR=#ff00ff]20180409[/color], [COLOR=#ff00ff]"Payment at the gas station"[/color]);
System.out.printf([COLOR=#ff00ff]" record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
result.add([COLOR=#ff00ff]7.90[/color], [COLOR=#ff00ff]20180413[/color], [COLOR=#ff00ff]"10 Pilsner Beers"[/color]);
System.out.printf([COLOR=#ff00ff]" record #%02d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ++n);
result.add([COLOR=#ff00ff]92.50[/color], [COLOR=#ff00ff]20180414[/color], [COLOR=#ff00ff]"Saturday shopping with my wife"[/color]);
System.out.println([COLOR=#ff00ff]"Done.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]);
[COLOR=#a52a2a][b]return[/b][/color] result;
}
}
#3. Then I created the class
RecordSetProcess which processes the RecordSet, i.e. it steps through all Records and prints them. For this purpose I have in the
RecordSet the method
next(). This method goes to the next Record (if possible) and fetch it's data into internal variable
currentRecord. Then using the getters
getAmount(),
getDatum() and
getNote() I will get the data out from data-object.
RecordSetProcess.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RecordSetProcess {
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] printRecordSet(RecordSet rs) {
[COLOR=#2e8b57][b]int[/b][/color] n = [COLOR=#ff00ff]0[/color];
[COLOR=#2e8b57][b]double[/b][/color] amount;
[COLOR=#2e8b57][b]double[/b][/color] total = [COLOR=#ff00ff]0[/color];
System.out.println([COLOR=#ff00ff]"Now processing records:"[/color]);
[COLOR=#a52a2a][b]while[/b][/color] (rs.next()) {
n++;
amount = rs.getAmount();
System.out.printf([COLOR=#ff00ff]"#%02d: cost=%5.2f, date: %8d, what: %s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], n,
amount, rs.getDatum(), rs.getNote());
[COLOR=#0000ff]// total cost[/color]
total += amount;
}
System.out.printf([COLOR=#ff00ff]"%d records processed, total expenses = %6.2f [/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], n,
total);
}
}
#4. Finally to test all 3 classes together I created the class
TestRecordSetProcessing. In the main() method it first creates the instance of RecordSetFill and calls it's method
fillInData() which creates the instance
RecordSet filled with data. Then it creates the instance of
RecordSetProcess class and calls it's method
printRecordSet, which takes as argument the instance of RecordSet and prints all records it contains.
TestRecordSetProcessing.java
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] TestRecordSetProcessing {
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] args) {
[COLOR=#0000ff]// create instance of a class which populates RecordSet[/color]
RecordSetFill recordSetFill = [COLOR=#a52a2a][b]new[/b][/color] RecordSetFill();
[COLOR=#0000ff]// call method which returns instance of RecordSet[/color]
RecordSet rs = recordSetFill.fillInData();
[COLOR=#0000ff]// creates instance of a class which processes the recordSet[/color]
RecordSetProcess recordSetProcess = [COLOR=#a52a2a][b]new[/b][/color] RecordSetProcess();
[COLOR=#0000ff]// call method which prints data contained in the RecordSet instance[/color]
recordSetProcess.printRecordSet(rs);
}
}
When I run the test class I get this output:
Code:
Start filling records:
record #01
record #02
record #03
Done.
Now processing records:
#01: cost=44.02, date: 20180409, what: Payment at the gas station
#02: cost= 7.90, date: 20180413, what: 10 Pilsner Beers
#03: cost=92.50, date: 20180414, what: Saturday shopping with my wife
3 records processed, total expenses = 144.42