In: Computer Science
android studio
Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
<!— activity_main.xml -->
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/inputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/nameView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product Name : "
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#000000"
/>
<EditText
android:id="@+id/nameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameView"
android:textSize="20sp"
android:textColor="#000000"
/>
<TextView
android:id="@+id/codeView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameTxt"
android:text="Product Code : "
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#000000"
/>
<EditText
android:id="@+id/codeTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/codeView"
android:textSize="20sp"
android:textColor="#000000"
/>
<TextView
android:id="@+id/priceView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product Price : "
android:layout_below="@id/codeTxt"
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#000000"
/>
<EditText
android:id="@+id/priceTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/priceView"
android:textSize="20sp"
android:textColor="#000000"
android:inputType="numberDecimal"
/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/priceTxt"
android:text="Submit"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:textSize="20sp"/>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/priceTxt"
android:layout_toRightOf="@id/submit"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Show Products"
android:textSize="20sp"
/>
</RelativeLayout>
</RelativeLayout>
<!—end of activity_main.xml -->
//MainActivity.java
package com.example.productdetails;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import mysimpleapps.com.productdetails.models.Product;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText nameTxt, codeTxt, priceTxt;
Button submit, show;
FileWriter outFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTxt = findViewById(R.id.nameTxt);
codeTxt = findViewById(R.id.codeTxt);
priceTxt = findViewById(R.id.priceTxt);
submit = findViewById(R.id.submit);
show = findViewById(R.id.show);
submit.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == submit)
{
File file = new File(this.getFilesDir(),"products.txt");
try {
outFile = new FileWriter(file, true);
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"File cannot be created or opened",Toast.LENGTH_SHORT).show();
}
if(nameTxt.getText().length() > 0 && codeTxt.getText().length() > 0 && priceTxt.getText().length() > 0)
{
Product product = new Product(nameTxt.getText().toString(),codeTxt.getText().toString(),Double.parseDouble(priceTxt.getText().toString()
));
try {
outFile.write(product.getName()+","+product.getCode()+","+product.getPrice()+"\n");
Toast.makeText(getApplicationContext(),"Product data added to file",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Product data cannot be stored",Toast.LENGTH_SHORT).show();
}
}else
Toast.makeText(getApplicationContext(),"Please enter all the details for the product",Toast.LENGTH_SHORT).show();
}else if(v == show)
{
try {
if(outFile != null)
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<Product> products = new ArrayList<>();
File file = new File(this.getFilesDir(),"products.txt");
try {
Scanner reader = new Scanner(file);
while(reader.hasNextLine())
{
String line = reader.nextLine();
if(line.length() > 0)
{
Product product = new Product();
int index = line.indexOf(",");
product.setName(line.substring(0,index));
index = line.indexOf(",",index+1);
product.setCode(line.substring(line.indexOf(",")+1,index));
product.setPrice(Double.parseDouble(line.substring(index+1)));
products.add(product);
}
}
reader.close();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
if(products.size() > 0)
{
String alertMessage = "";
for(int i=0;i<products.size()-1;i++)
{
alertMessage += products.get(i).toString()+"\n";
}
alertMessage += products.get(products.size()-1).toString();
alert.setMessage(alertMessage)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}else
Toast.makeText(getApplicationContext(),"Add some products first",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"File products.txt doesn't exist",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if(outFile != null)
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//end of MainActivity.java
Output:

