Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*********************MAIN ACTIVITY
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity
- {
- EditText owner, lineNumber, lastPay;
- String lastPayStr;
- UtlFile utlFile;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- owner=(EditText)findViewById(R.id.owner);
- lineNumber=(EditText)findViewById(R.id.lineNumber);
- lastPay=(EditText)findViewById(R.id.lastPay);
- utlFile=new UtlFile(this);
- }
- public void btnAddData(View view)
- {
- lastPayStr=lastPay.getText().toString();
- if(!lineNumber.getText().toString().matches("((0[57]|[//+]?[1-9]{3}[57])([0-9]{8}))|(([0][23489]|[//+]?[1-9]{3}[23489])([0-9]{7}))"))
- {
- Toast.makeText(MainActivity.this, "Please type phone number", Toast.LENGTH_SHORT).show();
- return;
- }
- if(!lastPayStr.matches("[0-9]+"))
- {
- Toast.makeText(MainActivity.this, "Please type numbers", Toast.LENGTH_SHORT).show();
- return;
- }
- if(lineNumber.getText().toString().indexOf("050")>=0)
- {
- int newLastPay=Integer.parseInt(lastPay.getText().toString())*2;
- lastPayStr=newLastPay+"";
- }
- utlFile.writer(owner.getText().toString(),lineNumber.getText().toString(), lastPayStr);
- owner.setText("");
- lineNumber.setText("");
- lastPay.setText("");
- }
- public void btnDisplay(View view)
- {
- utlFile.reader();
- Intent displayIntent = new Intent(this, DisplayActivity.class);
- this.startActivity(displayIntent);
- }
- public void btnClear(View view)
- {
- utlFile.delete();
- }
- }
- //***************************DISPLAY ACTIVITY
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.TextView;
- public class DisplayActivity extends AppCompatActivity
- {
- private TextView txt, sumPay, sumCall;
- private ListView myListV;
- private Adapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_display);
- setPointer();
- myListV=(ListView)findViewById(R.id.myListV);
- adapter=new Adapter(this);
- myListV.setAdapter(adapter);
- sumPay.setText("Sum pay: "+UtlFile.getSumPay()+"");
- sumCall.setText("Most calls: "+UtlFile.getSumCall());
- }
- private void setPointer()
- {
- txt=(TextView)findViewById(R.id.text);
- sumPay=(TextView)findViewById(R.id.sumPay);
- sumCall=(TextView)findViewById(R.id.sumCall);
- txt.setText("Call Logs");
- }
- }
- //****************************UTL FILE
- import android.content.Context;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
- public class UtlFile
- {
- private static final String MY_FILE ="call_list.txt";
- private static final int READ_BLOCK_SIZE = 100;
- public Context context;
- private FileOutputStream fileos;
- private OutputStreamWriter oswFile;
- private FileInputStream fileIS;
- private InputStreamReader isrFile;
- public static String s="";
- private static String tempLog;
- public String owner;
- public String linenumber;
- public int lastPay;
- public static ArrayList<UtlFile> myLogList = new ArrayList<UtlFile>();
- public UtlFile(String owner, String linenumber, int lastPay)
- {
- this.owner=owner;
- this.linenumber=linenumber;
- this.lastPay=lastPay;
- }
- public UtlFile(Context context)
- {
- this.context=context;
- }
- public void writer(String owner, String linenumber, String lastPay)
- {
- try
- {
- fileos=context.openFileOutput(MY_FILE, context.MODE_PRIVATE);
- oswFile=new OutputStreamWriter(fileos);
- tempLog+=owner+","+linenumber+","+lastPay+",";
- oswFile.write(tempLog);
- oswFile.close();
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void delete()
- {
- try {
- tempLog="";
- fileos=context.openFileOutput(MY_FILE, context.MODE_PRIVATE);
- oswFile=new OutputStreamWriter(fileos);
- oswFile.write("");
- oswFile.close();
- myLogList.clear();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void reader()
- {
- try {
- myLogList.clear();
- fileIS= context.openFileInput(MY_FILE); //pointer for reading the file
- isrFile = new InputStreamReader(fileIS); //create a reader (inputStreamReader)
- char[] inputBuffer = new char[READ_BLOCK_SIZE]; //size of buffer to read
- int charRead; // indicate if we have still data to read.....
- while ((charRead=isrFile.read(inputBuffer))>0) //if we have still data to read
- {
- String readString=String.copyValueOf(inputBuffer,0,charRead); //we put into string variable the entire char read array
- s+=readString; //add read string to our string
- inputBuffer = new char[READ_BLOCK_SIZE]; //allocate new buffer to read
- }
- isrFile.close(); //close the connection to the file
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- public ArrayList<UtlFile> getLog()
- {
- int temp=1,temp1=2;
- myLogList.clear();
- String[] logArr = s.split(",");
- if(!s.equals("")) {
- myLogList.clear();
- for (int counter = 0; counter < logArr.length; counter += 3) {
- myLogList.add(new UtlFile(logArr[counter], logArr[temp], Integer.parseInt(logArr[temp1])));
- temp += 3;
- temp1 += 3;
- }
- }
- s="";
- return myLogList;
- }
- public static int getSumPay()
- {
- int sumPay=0;
- for(UtlFile pay:myLogList)
- {
- sumPay+=pay.lastPay;
- }
- return sumPay;
- }
- public static String getSumCall()
- {
- String sumCall="";
- Map<String,Integer> map = new HashMap<String, Integer>();
- for(int i=0;i<myLogList.size();i++){
- Integer count = map.get(myLogList.get(i).linenumber);
- map.put(myLogList.get(i).linenumber, count==null?1:count+1);//auto boxing and count
- }
- ArrayList<Integer> myInt = new ArrayList<>();
- for(Map.Entry<String,?> log:map.entrySet())
- {
- myInt.add((Integer)log.getValue());
- }
- Collections.sort(myInt);
- for(Map.Entry<String,?> log:map.entrySet())
- {
- if(log.getValue()==myInt.get(myInt.size()-1))
- {
- sumCall+=log.getKey()+", ";
- }
- }
- return sumCall;
- }
- }
- //************************************ADAPTER
- import android.content.Context;
- import android.graphics.Color;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class Adapter extends BaseAdapter {
- private Context context;
- private UtlFile utlfile;
- static ArrayList<UtlFile> myLogList;
- public Adapter(Context context)
- {
- this.context=context;
- utlfile = new UtlFile(context);
- myLogList=utlfile.getLog();
- }
- @Override
- public int getCount() {
- return myLogList.size();
- }
- @Override
- public Object getItem(int i) {
- return null;
- }
- @Override
- public long getItemId(int i) {
- return 0;
- }
- @Override
- public View getView(int i, View view, ViewGroup viewGroup)
- {
- LinearLayout mylayout=new LinearLayout(context);
- mylayout.setOrientation(LinearLayout.HORIZONTAL);
- mylayout.setBackgroundColor(Color.CYAN);
- TextView txt=new TextView(context);
- txt.setTextSize(30);
- txt.setText("Owner: "+myLogList.get(i).owner+"\n"
- +"Line number: "+myLogList.get(i).linenumber+"\n"
- +"Last pay: "+myLogList.get(i).lastPay);
- mylayout.addView(txt);
- return mylayout;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement