package j;


import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;
 
class DataStorage {
 
    private RecordStore recordStore;
 
    public DataStorage(String storeName) {
        openRecordStore(storeName);
    }
 
    private void openRecordStore(String storeName) {
        try {
            recordStore = RecordStore.openRecordStore(storeName, true);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public void closeRecordStore() {
        try {
            if (recordStore != null) {
                recordStore.closeRecordStore();
            }
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public void addData(String data) {
        try {
            byte[] byteData = data.getBytes();
            recordStore.addRecord(byteData, 0, byteData.length);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
 
    public String[] getAllData() {
        try {
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            int numRecords = enumeration.numRecords();
            String[] data = new String[numRecords];
            int index = 0;
            while (enumeration.hasNextElement()) {
                int recordId = enumeration.nextRecordId();
                byte[] record = recordStore.getRecord(recordId);
                data[index++] = new String(record);
            }
            enumeration.destroy();
            return data;
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public void deleteAllData() {
        try {
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            while (enumeration.hasNextElement()) {
                int recordId = enumeration.nextRecordId();
                recordStore.deleteRecord(recordId);
            }
            enumeration.destroy();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }
}

 
public class MainApp extends MIDlet {
 

    public void startApp() {
        DataStorage dataStorage = new DataStorage("MyRecordStore");
 
        // Добавление данных в RecordStore
       
        /*
       dataStorage.addData("Data 1");
        dataStorage.addData("Data 22");
         dataStorage.addData("Data 33");
       */
     // dataStorage.addData("Data 2");
       // dataStorage.addData("Data 3");
 
        // Получение всех данных из RecordStore
        String[] allData = dataStorage.getAllData();
        if (allData != null) {
                           System.out.println("Retrieved Data: " + allData[0]);
                           // System.out.println("Retrieved Data: " + allData[1]);
                           //  System.out.println("Retrieved Data: " + allData[2]);
           
        }
 
        // Удаление всех данных из RecordStore
        //dataStorage.deleteAllData();
 
        dataStorage.closeRecordStore();
        notifyDestroyed();
    }
 
    public void pauseApp() {}
 
    public void destroyApp(boolean unconditional) {
    
    
    
    }
}