問題を直接解決したい場合は、この段落をスキップしてください。練習として、私は経済をシミュレートするJavaプログラムを作成しようとしています。そのために、会社のクラスを作成しました。アイデアは、たとえば、それらのダースに、それらの収益を正規変量関数にラップさせることでした。それが経済です。
JFreeChartを使用して企業の出力をグラフ化する別のクラスを作成しました。ただし、グラフ作成クラスから毎年の金額を書き込むArrayListにアクセスできません。これを行う最善の方法はおそらくゲッターを使用することだと思いますが、うまくいかなかったようです。それがあなたのアドバイスである場合、例を挙げていただけますか?ありがとう!
会社:
public class ServiceProvider implements Company {
//Variables
public ArrayList getRecords(){
return records;
}
public ServiceProvider(){
money = 10000;
yearID = 0;
goodYears = 0;badYears = 0;
records = new ArrayList();
id++;
}
public void year() {
yearID++;
if(!getBankrupt()){
spend();
}
writeRecords();
}
public void spend() {
...
}
public void printRecords() {
for(int i=0;i<records.size();i++){
String[] tmp = (String[]) records.get(i);
for(String a:tmp){
System.out.print(a+" ");
}
System.out.print("\n");
}
}
public void writeRecords(){
String[] toWrite = new String[2];
toWrite[0] = String.valueOf(yearID);
toWrite[1] = String.valueOf(money);
records.add(toWrite);
}
public void writeRecords(String toWrite){
String temp = "\n"+yearID+" "+toWrite;
records.add(temp);
}
public boolean getBankrupt(){
boolean result = (money < 0) ? true : false;
return result;
}
}
私がそれにアクセスしようとしているもの:
public class grapher extends JFrame {
ArrayList records = s.getRecords();
public grapher(){
super("ServiceProvider");
final XYDataset dataset = getCompanyData();
}
private XYDataset getCompanyData(){
XYSeries series;
for(int i=0;i<s.getRecords().length;i++){ //S cannot be resolved, it's instantiated in the main class.
}
}
}
メインクラス:
public class start {
public static void main(String[] args) {
ServiceProvider s = new ServiceProvider();
for(int i=0;i<10;i++){
s.year();
}
s.printRecords();
}
}
PS混乱したレコードが何であるかを教えてはいけません。知っている。