You are on page 1of 48

UNIVERSITY TEKNOLOGI MARA(UITM)

CAWANGAN PERAK, KAMPUS TAPAH

CSC 186: OBJECT ORIENTED PROGRAMMING

PROJECT TITLE: PHONE STORE SYSTEM


GROUP: CDCS1103B

NAME STUDENT ID
MUHAMMAD SHAFIQ HAIKAL BIN HISHAMUDDIN 2023147911
MUHAMMAD ADIB IMAN BIN SUHAIMI 2023167273
MUHAMMAD AFI3Q BIN ROY 2023164385

GROUP LEADER: MUHAMMAD SHAFIQ HAIKAL BIN HISHAMUDDIN


STUDENT ID: 2023147911
LECTURE NAME: ITAZA ARIANI BINTI MOHTAR

TABLE OF CONTENT

CONTENT PAGE

1
1.0 ORGANIZATIONAL STRUCTURE 3

2.0 INTRODUCTION 4
2.1 OBJECTIVE
3.0 UML DIAGRAM 5-6
3.1 CLASS DIAGRAM
3.2 USE CASE DIAGRAM

4.0 INPUT DATA 7

5.0 CLASS DEFINITION OF INHERITENCE AND POLYMORPHISM 8

6.0 CLASS APPLICATION 43

7.0 INFORMATION AND SAMPLE INTERFACE 50

8.0 REFERENCE 52

2
1.0 ORGANIZATIONAL STRUCTURE

2.0 INTRODUCTION

Technology has reshaped consumer behaviour where everything can communicate and
purchase through online shopping. This project report will delve into the Object-Oriented
Programming based Shopping System, outlining its design, implementation, and its core
features. The system blends technology and effective management to deliver user-friendly
interface for phone model selection and purchases, repair services, database
management, Shipping, and receipt. It encapsulates the spirit of Object-Oriented
Programming, offering modular, organized, and reusable code.
Our research report explores the intricacies of the Object-Oriented Programming
(OOP)based Shopping System and its impact on consumer interaction with businesses.
This easyto-use platform enables users to seamlessly browse and purchase mobile phone
models, and request repairs.
The system's user-friendly design ensures a hassle-free shopping experience. The repair
service feature efficiently resolves device-related issues, while the robust database
management system guarantees the accuracy of stored information, including product
details, customer information, and sales records.
In summary, the Object-Oriented Programming-based Shopping System is a
comprehensive and efficient platform that integrates technology with effective management
3
practices, resulting in an unmatched shopping experience. Its modular and scalable code
makes it suitable for organizations of all sizes.

2.1 OBJECTIVES
1. Develop an Object-Oriented Programmming based Shopping System of Phone
Market that smoothly integrates technology and effective management. The System will
include various of online shopping, including phone model selection, purchase
transactions and repair services also integrating more feature for seller so that the sellers
can access to data of availability of various phone model, customer data to track their
customer payment and repairment.
2. Design user-friendly interface that prioritize to ease the interaction for the user to
navigate through selection of phone models, simplify the purchase process and improving
the overall user experience.
3. Develop a system that emphasis on Object-Oriented programming through
fundamental concept of Object-Orented programming that are Abstraction, Encapsulation,
inheritance, and polymorphism to provide advantages such as code reusability, make the
system easier to maintained and effortless for new features to be added or removed.
4. Designing a system to ease the customer to have an appointment with the store
staff to be it repair, selling or purchasing.

4
3.0 UML DIAGRAM
3.1 CLASS DIAGRAM

3.2 USE CASE DIAGRAM

5
4.0 INPUT DATA

CustomerData.txt

6
PhoneData.txt Repair.txt

sellPrice.txt
5.0 CLASS DEFINITION OF INHERITENCE AND POLYMORPHISM

Super Class
Phone.java

import java.io.*; import


java.util.*;

//This is a super class and it's abstract so it will only work as a frame from every class
public abstract class Phone { private String model; private String variant;
private float price; private String color; private String condition; private int
stock;

7
// for Trade in or sell with condition public Phone(String m,
String v ,String c, String cond, float p){ model = m;
variant = v; price = p; color = c; condition = cond;
}
//for repair public
Phone (String m)
{ model = m;
}
// without condition public Phone(String ml,
String v, String c,float p ){ model = ml;
variant = v; price = p; color = c;
condition = "NEW";
}
//without price and stock public Phone(String m,
String v, String c, String cond){ model = m;
variant = v; color = c; condition = cond;
}
//default public
Phone(){ model
= null; variant =
null; price =
0.00f; color =
null; condition =
null;
}
//getter public String
getModel(){ return
model;
}
public String getVariant()
{ return variant;
}
8
public String getColor()
{ return color;
}
public float getPrice()
{ return price;
}
public String getCondition()
{ return condition;
}
//to string with condition
public String toString(){
return "Model : "+model + "\nVariant : "+variant+"Color : "+color + "Condition :
"+condition + "Price : RM"+price;
}
//getYesorNo method for return boolean
public boolean getYesorNo(String massage){
Scanner userInput = new Scanner(System.in);
System.out.println("\n"+massage+"y/n"); String userChoice =
userInput.next(); while(!userChoice.equalsIgnoreCase("y") && !
userChoice.equalsIgnoreCase("n")){
System.err.println("YOUR CHOICE IS NOT Y / N ");
System.out.println("\n"+massage+"(Y/N)"); userChoice
= userInput.next();
}
return userChoice.equalsIgnoreCase("y");
}
}

Sub Class

9
Repair.java import java.io.*; import
java.util.*; public class Repair
extends Phone
{

private int problem;


private String phoneModel;

//normal const private String []


item = new String[2]; public Repair
(int p ){ problem = p;
}
//calculate price for repair public int
showPhoneModel() throws IOException{
//Read Repair.txt file to see the repair price
FileReader read = new FileReader("Z:\\visual code\\java\\Groupproject\\Main.java\\
Repair.txt");
BufferedReader buff = new BufferedReader(read);

String data = buff.readLine();


System.out.println();
//for loop easier for customer to choose later base on the number
int i =0;
//this loop is to show all model we have to repair
while (data != null){ i++;
Scanner scan = new Scanner(data);
scan.useDelimiter(";"); System.out.println(i+".
"+scan.next()); data = buff.readLine();
}
Scanner input = new Scanner(System.in);

10
System.out.print("\nChoose Model : "); //user have to choose what model
int modelChoice = input.nextInt(); //will return the customer choice
return modelChoice;

//problem 1 = battery , problem 2 == LCD


//method to calculate price public void
repairPrice() throws IOException{
//Read Repair.txt file
FileReader read = new FileReader("Z:\\visual code\\java\\Groupproject\\Main.java\\
Repair.txt");
BufferedReader buff = new BufferedReader(read);
//call showPhoneModel method and insert the return value to choice
int choice = showPhoneModel();
String price = null;
//to count the entry int
entryCount =0;
String data = buff.readLine();
//call Customer class for write customer data later and for receipt
Customer cust = new Customer("Repair"); while (data != null){
Scanner scan = new Scanner(data);
scan.useDelimiter(";"); entryCount+
+; phoneModel = scan.next();

String fullItem = null;


//if customer choice same with entryCount it will run this
if(choice == entryCount){
//show the price if customer choose battery or lcd
if(problem == 1){

11
System.out.println("\nPhone Model : " + phoneModel);
item[0] = "Battery " + phoneModel; price =
scan.next();
System.out.println(item[0]+" : RM "+price);
fullItem = item[0] + " " + phoneModel;
}else if(problem == 2){
System.out.println("\nPhone Model : " + phoneModel);
item[1] = "LCD "+phoneModel; scan.next();
price = scan.next();
System.out.println(item[1]+" : RM "+price);
fullItem = item[1] + " " + phoneModel;
}
else{
System.err.println("Error Pls try again");
}
//ask customer if they really want to proceed boolean
yesOrNo = super.getYesorNo("Do you want to repair : ");
if(yesOrNo){
//if yes, the system will write the data to Customer file
cust.writeCustomerData(fullItem, price);
}
}
data = buff.readLine();
//item , phone model , price
}
read.close();
buff.close();
}
}

12
Sell.java import
java.io.*; import
java.util.*;

public class Sell extends Phone{

private double sellPrice;


//default constructor
Sell()
{ super(null,null,null,null
);
}
//normal
Sell(String model, String variant,String color,String condition)
{ super(model, variant, color,condition);
}
//model only for search price
Sell(String model, String variant)
{ super(model,variant,null,null);
}
//to find phone price public void findPhonePrice(boolean
showData) throws IOException{
//read whether the database have or not
//in this file we save price for each model if customer want to sell them
try{
File file = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\sellPrice.txt");
}catch(Exception e){

System.err.println("Data Base Not Found");


System.err.println("PLEASE MAKE THE DATABASE FIRST");
return;
}

13
//get model and variant to search if the model have in database
String m,v; m = getModel(); v = getVariant();
String findString = m+" "+v;
//to split the space
String [] keyword = findString.split("\\s+");
//call checkPhonePriceinDatabase method to check
checkPhonePriceinDatabase(keyword,showData);

}
public boolean checkPhonePriceinDatabase(String [] keyword, boolean
isDisplay)throws IOException{
//Read sellPrice.txt file
FileReader fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\sellPrice.txt");
BufferedReader bufferRead = new BufferedReader(fileRead);

String data = bufferRead.readLine();


boolean isExist = false; int totalData
=0; //if isDisplay is true
if(isDisplay){
System.out.println("\n|NO|\tMODEL\t\t|VARIANT\t|PRICE\t |");
System.out.println("=========================================");
}
while(data != null){
//check keyword inside the line
isExist = true; for(String keywords: keyword){ isExist =
isExist && data.toLowerCase().contains(keywords.toLowerCase());
}
if(isExist)
{ if(isDisplay){
totalData++;
StringTokenizer stringToken = new StringTokenizer(data,";");

14
//stringToken.nextToken();
System.out.printf("|%d ", totalData);
//model
System.out.printf("|%s ", stringToken.nextToken());
//variant
System.out.printf(" |%sGB \t", stringToken.nextToken());
//price
System.out.printf("|RM%s ", stringToken.nextToken());
System.out.println(); break;
}else{
//to take the price from the database
Scanner scan = new Scanner(data);
scan.useDelimiter(";"); for(int i =0; i
< 2; i++){ scan.next();
}
double price = scan.nextDouble();
sellPrice(price); break;
}
}
data = bufferRead.readLine();
} if(!
isExist){
System.out.println("NO DATA");
if(!isDisplay){
System.out.println("WE DONT ACCEPT THAT PHONE");
}
}
if(isDisplay){
System.out.println("=========================================");
}

15
bufferRead.close();
fileRead.close(); return
isExist;
}
//method to calculate sell price public void
sellPrice(double price) throws
IOException{ Customer cust = new Customer("Sell
Phone"); double endPrice ;
System.out.println(super.getCondition());
Integer condition =
Integer.valueOf(super.getCondition()); //condition for price the
price is based on the condition endPrice = price-(1000-
(condition*100));
//display the price and if the seller want to approve to buy this customer phone
System.out.println("\n=========================================");
System.out.println("Price We Take : RM" + endPrice);
System.out.println("=========================================");
boolean approve = super.getYesorNo("CONTINUE: ");
if(approve){
try{
//if approve will write the customer phone data on phoneData.txt file
FileWriter write = new FileWriter("Z:\\visual code\\java\\Groupproject\\Main.java\\
phoneData.txt",true); BufferedWriter buffWrite = new
BufferedWriter(write);

String Stringcondition = String.valueOf(condition);


String stringPrice = Double.toString(endPrice);
String [] keywords =
{super.getModel()+","+super.getVariant()+","+super.getColor()+","+Stringcondition};
Seller seller = new Seller();

boolean isExist = seller.checkPhoneinDatabase(keywords, false);

16
System.out.println("\nData that you enter is : ");
System.out.println("=============================");
System.out.println("Phone Model : " + super.getModel());
System.out.println("Phone Variant : "+super.getVariant());
System.out.println("Phone Price : "+ endPrice);
System.out.println("Phone Color : "+ super.getColor());
System.out.println("Phone Condition : " + condition); boolean
isAdd = getYesorNo("Approve the phone : ");
if(!isExist){
//if the phone customer phone data is not exist yet
if (isAdd){
String item = new String(super.getModel()+" "+super.getVariant()+"
"+super.getColor()+" "+Stringcondition+"/10");

buffWrite.write(getModel()+","+super.getVariant()+","+stringPrice+","+super.getColor
()+","+Stringcondition+"/10,"+"1"); cust.writeCustomerData(item,
stringPrice); System.out.println(item); cust.showReceipt();
buffWrite.newLine(); buffWrite.flush();
}
}
else{
//if exist the system will ask to just edit the stock
System.out.println("The data is already there you should update the stock");
}
buffWrite.close();
write.close();
}catch(Exception e){
System.err.println("Error");
}
}
}
}

17
Seller.java import
java.io.*; import
java.util.*;

//Class for seller access the database public class Seller


extends Phone{ //only have stock as their data member
private int stock; //normal public Seller(String m, String
v ,String c, String cond, float p){ super(m,v,c,cond,p);
}
//default
public Seller(){
super();
}
//to show phone data public void
showPhoneData() throws IOException{ //take
data from phoneData.txt and show them

FileReader fileRead;
BufferedReader bufferRead;

try{
//try to open phoneData.txt file in this file has all the information for phone in stock
fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\phoneData.txt"); bufferRead = new
BufferedReader(fileRead);
}
catch(Exception e){
System.err.println("DATABASE NOT FOUND");
System.err.println("PLEASE MAKE THE DATABASE FIRST !!!");
//if there's no database so it will call addData method to make a new file
addData(); return;

18
}

System.out.println("\n|NO|\tMODEL\t\t|VARIANT|PRICE\t|\tCOLOR\t|CONDITION\t|
STOCK\t");

System.out.println("================================================
=================================");
String data = bufferRead.readLine();
int i =0; while
(data != null) {
//display all phone in stock
i++;
StringTokenizer stringToken = new StringTokenizer(data, ",");
//yang ni untuk primary key
//stringToken.nextToken();
System.out.printf("|%d ", i);
//model
System.out.printf("|%s", stringToken.nextToken());
//variant
System.out.printf("|%sGB\t", stringToken.nextToken());
//price
System.out.printf("|RM%s\t", stringToken.nextToken());
//color
System.out.printf("|%s\t", stringToken.nextToken());
//condition
System.out.printf("|%s\t", stringToken.nextToken());
System.out.printf("|%s\t", stringToken.nextToken());
System.out.println(); data = bufferRead.readLine();
}
fileRead.close();
bufferRead.close();

19
System.out.println("\n===============================================
=================================");
}
//to find phone data public void
findData() throws IOException{

//read whether the database have or not


try{
File file = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\phoneData.txt");
}catch(Exception e){
System.err.println("Data Base Not Found");
System.err.println("PLEASE MAKE THE DATABASE FIRST");
addData();
return;
}
//take keyword from user
Scanner userInput = new Scanner(System.in);
System.out.print("Enter keyword : ");
String findString = userInput.nextLine();
//Untuk split kan space yang user masuk so akan masuk ke array 0 and array 1
String [] keyword = findString.split("\\s+"); checkPhoneinDatabase(keyword,true);
}
//to check phone in database
public boolean checkPhoneinDatabase(String [] keyword, boolean isDisplay)throws
IOException{
FileReader fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\phoneData.txt");
BufferedReader bufferRead = new BufferedReader(fileRead);

String data = bufferRead.readLine();


boolean isExist = false; int totalData
=0; if(isDisplay){
System.out.println("\n|NO|\tMODEL\t\t|VARIANT\t|PRICE\t
20
|\tCOLOR\t|\tCONDITION|\tSTOCK");

System.out.println("================================================
=====================================================");
}
while(data != null){
//check keyword inside the line isExist = true; for(String
keywords: keyword){ isExist = isExist &&
data.toLowerCase().contains(keywords.toLowerCase());
}
if(isExist)
{ if(isDisplay
)
{ totalDat
a++;

//if data is exist and isDisplay is true it will display


StringTokenizer stringToken = new StringTokenizer(data,",");
//stringToken.nextToken();
System.out.printf("|%d ", totalData);
//model
System.out.printf("|%s ", stringToken.nextToken());
//variant
System.out.printf(" |%sGB \t", stringToken.nextToken());
//price
System.out.printf("|RM%s ", stringToken.nextToken());
//color
System.out.printf("|\t%s\t", stringToken.nextToken());
//condition
System.out.printf("|\t%s ", stringToken.nextToken());
//STOCK
System.out.printf("|\t%s ", stringToken.nextToken());
System.out.println();
21
}
}

data = bufferRead.readLine();
}
if(isDisplay){

System.out.println("================================================
====================================================");
}
bufferRead.close();
fileRead.close(); return
isExist;

}
//method to add data public void
addData() throws IOException{
//true mean it will append to exist file instead of overwrite it
FileWriter fileWrite = new FileWriter("Z:\\visual code\\java\\Groupproject\\Main.java\\
phoneData.txt",true);
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);

Scanner userInput = new Scanner (System.in);


String model, variant, color, condition; float
price; int stock;
//take input from user
System.out.println("Enter phone model : ");
model = userInput.nextLine();
System.out.println("Enter phone variant : ");
variant = userInput.nextLine();

22
System.out.println("Enter phone Price : ");
price = userInput.nextFloat();
userInput.nextLine();
System.out.println("Enter phone Color : ");
color = userInput.nextLine();
System.out.println("Enter phone Condition : ");
condition = userInput.nextLine();
System.out.println("Enter Stock : "); stock =
userInput.nextInt();

//check phone in database


String [] keywords = {model+","+variant+","+price+","+color+","+condition};
boolean isExist = checkPhoneinDatabase(keywords, false);
System.out.println(isExist);

// if the data isn't exist system will write phone data in database
if(!isExist){
System.out.println("\nData that you enter is : ");
System.out.println("=============================");
System.out.println("Phone Model : " + model);
System.out.println("Phone Variant : "+variant);
System.out.println("Phone Price : "+ price);
System.out.println("Phone Color : "+ color);
System.out.println("Phone Condition : " + condition);

boolean isAdd = getYesorNo("Do you want to add the data ? ");


//if user want to add if (isAdd)
{ bufferWrite.write(model+","+variant+","+price+","+color+","+condition+","+stock
); bufferWrite.newLine(); bufferWrite.flush();
}
//if data already have in database
}else{
23
System.out.println("The data already have ");
checkPhoneinDatabase(keywords, true);
}

bufferWrite.close();
fileWrite.close();
}

//decrease stock public void decreaseStock(int updateStock)


throws IOException{
//read phoneData.txt file
File databases = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\
phoneData.txt"); FileReader read = new FileReader(databases);
BufferedReader buffRead = new BufferedReader(read);
//make a temporary file to update the stock
File tempDatas = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\
temppDB.txt");
FileWriter temp = new FileWriter(tempDatas);
BufferedWriter buffTempData = new BufferedWriter(temp);

String [] tempData = new String[6];


String data = buffRead.readLine();
String originalData; int entryCount
=0;

while(data != null)
{ entryCount++;
StringTokenizer st = new StringTokenizer(data, ",");
if(updateStock == entryCount){ for(int i =0; i <
tempData.length; i++){ originalData =
st.nextToken(); if(i==5){

24
String stockToken = originalData;
int stock = Integer.parseInt(stockToken);
stock--; tempData[i] =
String.valueOf(stock); if(stock != 0){
String tempp =
tempData[0]+","+tempData[1]+","+tempData[2]+","+tempData[3]+","+tempData[4]+",
"+tempData[5];
System.out.println(tempp);
buffTempData.write(tempp); buffTempData.newLine();
}else{
System.out.println("LAST STOCK !!!!");
}
}else{ tempDa
ta[i] = originalData;
}
}
}else{ buffTempData.
write(data);
buffTempData.newLine();
}
data = buffRead.readLine();
} buffTempData.flush();
buffRead.close();
buffTempData.close();
read.close(); temp.close();
System.gc();
//delete the original file and rename temporary file to original file
databases.delete(); tempDatas.renameTo(databases);

}
//update stock public void updateData()
throws IOException{

25
//take database original
File dataBase = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\
phoneData.txt");
FileReader fileRead = new FileReader(dataBase);
BufferedReader bufferRead = new BufferedReader(fileRead);
//make temporary database
File tempDB = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\tempDb.txt");
FileWriter fileWrite = new FileWriter(tempDB);
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);

//show all data


System.out.println("List Phone");
showPhoneData();

//take user input


Scanner userInput = new Scanner(System.in);
System.out.println("Enter number data you want to update: ");
int updateNum = userInput.nextInt();

//show data that want to update


String data = bufferRead.readLine();
int entryCount =0; while (data !=
null){ entryCount ++;

StringTokenizer st = new StringTokenizer(data, ",");

//show entry count == updateNum


if(updateNum == entryCount){
System.out.println("\nData you want to update is : ");
System.out.println("=====================================");
System.out.println("Phone Model : "+ st.nextToken());
System.out.println("Phone Variant : "+ st.nextToken());
26
System.out.println("Phone Price : "+ st.nextToken());
System.out.println("Phone Color : "+ st.nextToken());
System.out.println("Phone Condition : "+ st.nextToken());
System.out.println("Phone Stock : "+ st.nextToken());
//update data
String [] fieldData = {"model", "variant", "price", "color","condition","stock"};
String [] tempData = new String[6];
//refresh token st= new StringTokenizer(data, ","); String
originalData = st.nextToken(); for(int i =0; i < (fieldData.length); i++){
boolean isUpdate = getYesorNo("Do you want to edit the " + fieldData[i]+"
"); //ask user if they want to update mode, variant, price, color, condition, or
stock if(isUpdate){ userInput = new Scanner(System.in);
System.out.println("\nEnter "+fieldData[i]+" new data : ");
tempData[i]= userInput.nextLine();
} else{
tempData[i] = originalData;
}
//will stop the token if the system reach last fieldData or the system will be
error
if(!fieldData[i].equals("stock")){
originalData = st.nextToken();
}

}
//display new data
//confirmation st = new
StringTokenizer(data, ",");
System.out.println("\nNEW DATA");
System.out.println("===================================");
System.out.println("Phone Model :" + tempData[0]);
System.out.println("Phone Variant :" + tempData[1]);
System.out.println("Phone Price :" + tempData[2]);
27
System.out.println("Phone Color :" + tempData[3]);
System.out.println("Phone Condition :" + tempData[4]);
System.out.println("Phone Stock :" + tempData[5]);
boolean isUpdate = getYesorNo("Are you sure you want to update the data ");
if(isUpdate){
String tmodel = tempData[0];
String tvariant = tempData[1];
String tprice = tempData[2];
String tcolor = tempData[3];
String tcondition = tempData[4];
String tstock = tempData[5];

//write data in data base

bufferWrite.write(tmodel+","+tvariant+","+tprice+","+tcolor+","+tcondition+","+tstock);
} else{
bufferWrite.write(data);
}
}else{ bufferWrite.
write(data);
}
bufferWrite.newLine();
data = bufferRead.readLine();
}

//write data to file


bufferWrite.flush();
bufferRead.close();
bufferWrite.close();
fileRead.close(); fileWrite.close();
System.gc();

28
//delete original database
dataBase.delete(); //rename temporary file
tempDB.renameTo(dataBase);

}
//to delete phone data from file public void
deletePhoneData () throws IOException{
//take database original
//just read the file
File dataBase = new File ("Z:\\visual code\\java\\Groupproject\\Main.java\\
phoneData.txt");
FileReader fileRead = new FileReader(dataBase);
BufferedReader bufferRead = new BufferedReader(fileRead);
//make temporary database
File tempData = new File("tempdb.txt");
FileWriter fileWrite = new FileWriter(tempData);
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);
//show data
System.out.println("List Phone ");
showPhoneData();
//take use input for delete data
Scanner userInput = new Scanner(System.in);
System.out.println("Enter no you want to delete : "); int
deleteNum = userInput.nextInt();
//loop to read every row data and skip data selected
boolean isFound = false; int entryCounts = 0;
String data = bufferRead.readLine();
while (data != null) { entryCounts
++; boolean isDelete = false;
StringTokenizer st = new StringTokenizer(data, ",");
//show data that use want to delete
if(deleteNum == entryCounts){
29
System.out.println("Data you want to delete : ");
System.out.println("=================================");
System.out.println("Phone Model : "+ st.nextToken());
System.out.println("Phone Variant : "+ st.nextToken());
System.out.println("Phone Price : "+ st.nextToken());
System.out.println("Phone Color : "+ st.nextToken());
System.out.println("Phone Condition : "+ st.nextToken());
System.out.println("Phone Stock : "+ st.nextToken());

isDelete = getYesorNo("Do you want to delete :");


isFound = true;
}
if(isDelete){
//skip transfer data original to temporary
System.out.println("Data already deleted");
}else{
//transfer original data to temporary
bufferWrite.write(data); bufferWrite.newLine();
}

data = bufferRead.readLine();
} if(!
isFound){
System.err.println("DATA IS NOT FOUND");
}

//write data at file


bufferWrite.flush();
bufferWrite.close();
bufferRead.close(); fileRead.close();
fileWrite.close();

30
System.gc();
//delete original data
dataBase.delete();
//rename temporary file to database
tempData.renameTo(dataBase);

}
}

Customer.java import
java.io.*; import
java.util.*;

public class Customer {

//this class have name, phone number, email, service and date as their data member
private String name; private String phoneNumber; private String email; private
String service; private Date tarikh = new Date();

//Call seller class to take some method from Seller class


private Seller phone = new Seller();

public Customer(String s)
{ service = s;

//default constructor for this class


public Customer(){ name =
null; phoneNumber = null;
email = null; service = null;
}

31
//getter public String getName(){ return name;}
public String getPhoneNumber() {return phoneNumber;}
public String getEmail() { return email;}

//to show customer info public void


showCustInfo() throws IOException{
FileReader fileRead ;
BufferedReader bufferRead ;
//try to read data from CustomerData if file not found it wil print "DATABASE NOT
FOUND"
//in this file we save customer information
try{
fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\CustomerData.txt"); bufferRead = new
BufferedReader(fileRead);
}
catch(Exception e){
System.err.println("DATABASE NOT FOUND");
System.err.println("PLEASE MAKE THE DATABASE FIRST !!!");
return;
}
//display for customer havae all data members

System.out.println("\n===============================================
=====================");
System.out.println("|NO|\tNAME\t\t|EMAIL|NO
PHONE\t|\tSERVICE\t|ITEM\t|DATE\t|TOTAL\t|");

System.out.println("================================================
====================");
int totalData =0;

32
String data = bufferRead.readLine();
//print untill the end of line in file
while(data != null){ totalData++;
StringTokenizer st = new StringTokenizer(data , ";");
System.out.printf("|%d ", totalData);
//name
System.out.printf("|%s", st.nextToken());
//email
System.out.printf("|%s\t", st.nextToken());
//no phone
System.out.printf("|%s\t", st.nextToken());
//service
System.out.printf("|%s\t", st.nextToken());
//item
System.out.printf("|%s\t", st.nextToken());
//date
System.out.printf("|%s\t", st.nextToken());
//total
System.out.printf("|%s\t", st.nextToken());
System.out.println(); data =
bufferRead.readLine();

}
//close the file
fileRead.close();
bufferRead.close();

System.out.println("================================================
====================");
}
public void writeCustomerData( String item , String total ) throws IOException{
//try to write Customer Data
try {
33
FileWriter fileWrite = new FileWriter("Z:\\visual code\\java\\Groupproject\\
Main.java\\CustomerData.txt", true);
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);
//User will input their name, phone number and email
Scanner userInput = new Scanner(System.in);
System.out.println("\n===================================");
System.out.print("Enter your name : "); name = userInput.nextLine();
System.out.print("Enter your phone number : ");
phoneNumber = userInput.nextLine();
System.out.print("Enter your email: "); email =
userInput.nextLine();
System.out.println("===================================");
//write and flush the data into CustomerData.txt file
bufferWrite.write( name +";" +phoneNumber + ";" +
email+";"+service+";"+item+";"+tarikh+";"+total);
bufferWrite.flush();
bufferWrite.newLine();
bufferWrite.close(); fileWrite.close();
}
catch(Exception e){
//if the file is error to open
System.err.println("Error To Open File ");
}
}
public void findCustomerData() throws IOException{
//read whether the database have or not
try{
File file = new File("Z:\\visual code\\java\\Groupproject\\Main.java\\
CustomerData.txt");
}catch(Exception e){
//if there's no CustomerData.txt file user can make a file on add data
System.err.println("Data Base Not Found");
System.err.println("PLEASE MAKE THE DATABASE FIRST");
34
return;
}
//take keyword from user
Scanner userInput = new Scanner(System.in);
System.out.print("Enter Customer Name : ");
String findString = userInput.nextLine();
//For split the keyword by the space and to find the keyword have in database or not
String [] keyword = findString.split("\\s+");
//true is to display the data if false itsnot going to display the data it will only return the
boolean checkCustomerInDatabase(keyword,true);
}
public boolean checkCustomerInDatabase(String[] keyword, boolean isDisplay) throws
IOException{
//take data from CustomerData.txt and show them
FileReader fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\CustomerData.txt");
BufferedReader bufferRead = new BufferedReader(fileRead);

//isExist is false by default


boolean isExist =false;
//data string hold CustomerData.txt line and will read next line for next loop
String data = bufferRead.readLine();

//if isDisplay is true


if(isDisplay){
System.out.println("\n|NO|\tNAME\t\t|EMAIL|NO
PHONE\t|\tSERVICE\t|ITEM\t|DATE\t|TOTAL\t|");

System.out.println("================================================
=================================");
}
//just for number on the left indicate how many customer there's
int totalData = 0; while (data != null) {

35
//isExist is true to make a logic gate &&
isExist = true; for(String keywords:
keyword){
//so isExist is true so if in data there's a keyword than isExist will be true
because true + true = true isExist = isExist &&
data.toLowerCase().contains(keywords.toLowerCase());
}
if(isExist)
{ if(isDisplay){
totalData++;
StringTokenizer stringToken = new StringTokenizer(data, ";");
//stringToken.nextToken();
System.out.printf("|%d ", totalData);
//name
System.out.printf("|%s", stringToken.nextToken());
//email
System.out.printf("|%s\t", stringToken.nextToken());
//no phone
System.out.printf("|%s\t", stringToken.nextToken());
//service
System.out.printf("|%s\t", stringToken.nextToken());
//item
System.out.printf("|%s\t", stringToken.nextToken());
//date
System.out.printf("|%s\t", stringToken.nextToken());
//total
System.out.printf("|%s\t", stringToken.nextToken());
System.out.println();
}
}
//to read next line for next loop
data = bufferRead.readLine();
36
}
if(isDisplay){

System.out.println("================================================
================================");
}
fileRead.close();
bufferRead.close(); return
isExist;
}
//method for buy phone public void buyPhone(int
choice) throws IOException{
//open read file for phoneData in this file we save each phone price, stock and every
info about the phone
FileReader fileRead = new FileReader("Z:\\visual code\\java\\Groupproject\\
Main.java\\phoneData.txt");
BufferedReader buff = new BufferedReader(fileRead);
//for looping
int entryCount = 0;
String data =
buff.readLine();
boolean wantBuy =
false;//wantBuy is false
by default
String item, model , variant , price, color , condition; //declare item ,model, variant ,
price, color, condition to take data from file
while(data != null)
{ entryCount++;
StringTokenizer st = new StringTokenizer(data,",");
if(choice == entryCount){
//take the data using String tokenizer and the delimeter is ","
model = st.nextToken(); variant = st.nextToken();

37
price = st.nextToken(); color = st.nextToken();
condition = st.nextToken();
//to make sure if customer really want to buy
System.out.println("\nModel : " + model);
System.out.println("Variant : "+variant);
System.out.println("Price: " + price);
System.out.println("Color: " + color);
System.out.println("Condition: " + condition); wantBuy =
phone.getYesorNo("Are you sure you want to buy this : ");
if(wantBuy){
//if yes it will call writeCustomerData method to write customer data and call
decreaseStock method from Seller class to decrease the stock item =
model+" "+variant+" "+color+" "+condition; fileRead.close();
buff.close(); writeCustomerData(item, price);
phone.decreaseStock(entryCount);
break;
}
}
data = buff.readLine();
}
fileRead.close();
buff.close();
}
public void showReceipt() throws IOException{
// read CustomerData.txt file , in this file we save customer data
FileReader read = new FileReader("Z:\\visual code\\java\\Groupproject\\Main.java\\
CustomerData.txt");
BufferedReader buff = new BufferedReader(read);
//output for receipt
System.out.println("\n\nRECEIPT");
System.out.println("\n===================================");
System.out.println("KEDAI JUAL PHONE");

38
System.out.println("===================================");

String data = buff.readLine();


String lastLine = null;
//to know the last data because the latest customer data is on the last line
while(data != null){ lastLine = data; data = buff.readLine();
}
StringTokenizer st = new StringTokenizer(lastLine, ";");

System.out.println("\nName : " + st.nextToken());


System.out.println("No Phone : " + st.nextToken());
System.out.println("Email : " + st.nextToken());
System.out.println("Service : " + st.nextToken());
System.out.println("Item : " + st.nextToken());
System.out.println("Date : " + st.nextToken());
System.out.println("Total : RM" + st.nextToken()); System.out.println("\n\
n===================================");
System.out.println("Store Location: Lowyat");
System.out.println("Thanks Come again");
System.out.println("===================================");

}
}

39
6.0 CLASS APPLICATION

Main.java (Main class)


import java.io.*; import
java.util.*;

public class Main {

public static void main(String[] args) throws IOException {

String userChoice;
Scanner userInput = new Scanner(System.in);
boolean isContinue = true; int buyerOrSeller,
dbOrSell, choice; Seller phone1 = new
Seller();
Customer cust = new Customer();

while(isContinue){
//interface
System.out.println("\n======================");
System.out.println("WELCOME TO KEDAI PHONE");
System.out.println("======================");
System.out.println("1.Seller");
System.out.println("2.Buyer");
System.out.println("======================");
System.out.print("Your Option : "); buyerOrSeller =
userInput.nextInt();
40
switch (buyerOrSeller) {
case 1:
//user need to insert password to open seller page String
password = "0";
System.out.print("\nEnter password to access Seller account : ");
password = userInput.next(); System.out.println(password);
if(password.equals("999")){
System.out.println("\n1.Access Database");
System.out.println("2.Sell Phone");
System.out.print("Your option : ");
dbOrSell = userInput.nextInt();
userInput.nextLine(); switch
(dbOrSell) { //access store
database case 1:
System.out.println("----------------");
System.out.println("STORE DATABASE");
System.out.println("----------------");
System.out.println("1.\tView Phone");
System.out.println("2.\tFind Phone");
System.out.println("3.\tAdd Phone");
System.out.println("4.\tEdit Phone Data");
System.out.println("5.\tDelete Phone");
System.out.println("6.\tFind Customer Data");
System.out.println("7.\tShow Customer Data");
System.out.print("\nEnter your choice : "); userChoice
= userInput.next();
//String model, String variant, String color, String condition
switch (userChoice) { case "1":
System.out.println("\n______________");
System.out.println("List All Phone");
System.out.println("______________"); phone1.showPhoneData();
41
break;
case "2":
System.out.println("\n______________");
System.out.println("Find A Phone");
System.out.println("______________");
//FIND DATA
phone1.findData();
break; case "3":
System.out.println("\n______________");
System.out.println("Add Phone");
System.out.println("______________");
phone1.addData(); break;
case "4":
System.out.println("\n______________");
System.out.println("Edit Phone Data");
System.out.println("______________");
phone1.updateData(); break;
case "5" :
System.out.println("\n______________");
System.out.println("Delete Phone on list");
System.out.println("______________");
phone1.deletePhoneData(); break;
case "6" :
System.out.println("\n______________");
System.out.println("Find Cust");
System.out.println("______________"); cust.findCustomerData();
break; case "7" :
System.out.println("\n______________");
System.out.println("Show Customer Data");
System.out.println("______________");
cust.showCustInfo(); break;
default:
42
System.err.println("INPUT IS NOT FOUND");
break;

}
break;
case 2:
//if customer want to sell phone and this page only seller can open and
approve the phone
int FindOrSell;
System.out.println("\n----------------");
System.out.println("SELL PHONE");
System.out.println("----------------\n");
System.out.println("1.Find phone Price");
System.out.println("2.Sell Phone");
FindOrSell = userInput.nextInt();
//just want to fill condition value
String model,variant, color, condition="5";
int cond =5; userInput.nextLine();
switch (FindOrSell) { case 1:
//if only want to see the phone price
System.out.println("\n=================");
System.out.println("FIND PHONE PRICE");
System.out.println("=================\n");
System.out.print("Enter phone model : "); model
= userInput.nextLine();
System.out.print("Enter phone Variant : ");
variant = userInput.nextLine(); Sell find =
new Sell(model,variant);
find.findPhonePrice(true); break;
case 2:
//if customer want to sell phone
System.out.println("=================");

43
System.out.println("SELL PHONE");
System.out.println("=================\n");
System.out.print("Enter phone model : ");
model = userInput.nextLine();
System.out.println("Enter Variant: "); variant =
userInput.nextLine();
System.out.println("Enter Color: "); color =
userInput.nextLine(); while(true){
System.out.print("Enter condition : ");
cond = userInput.nextInt(); if(cond
<= 10 && cond >=1){ condition =
String.valueOf(cond); break;
}
}
Sell sell = new Sell(model, variant,color,condition);
sell.findPhonePrice(false);

break;
default:
break;
}
break;
default:
System.out.println("WRONG INPUT");
break;
}
}else{
System.out.println("WRONG PASSWORD");
break;
}
break;
case 2:

44
//this is customer page so it's public everyone can access
System.out.println("\n1. Buy Phone");
System.out.println("2. Repair Phone");
System.out.println("Your Option : "); int
buyOrRepair = userInput.nextInt();
if(buyOrRepair == 1){
//page to buy phone
Customer custBuy = new Customer("Buy Phone");
System.out.println("----------------");
System.out.println("BUY PHONE");
System.out.println("----------------\n\n");
phone1.showPhoneData();
System.out.print("Choose Phone: "); int
phoneChoose = userInput.nextInt();
custBuy.buyPhone(phoneChoose); }else
if(buyOrRepair == 2){

//page to repair phone


System.out.println("----------------");
System.out.println("REPAIR PHONE");
System.out.println("----------------\n");
System.out.println("1. Battery");
System.out.println("2. LCD");
System.out.print("Choose: "); choice
= userInput.nextInt();
userInput.nextLine();
Repair baiki = new Repair(choice );
baiki.repairPrice();
}else{
System.err.println("Wrong Input !!!");
break;
}

45
cust.showReceipt();
break; default:
System.out.println("WRONG INPUT");
break;
}
//if user want to continue isContinue =
phone1.getYesorNo("Do you want to Continue : ");
}

7.0 INFORMATION AND SAMPLE INTERFACE

Diagram 1 (receipt example)

46
Diagram 2 (seller access database)

diagram 3 (Customer buy phone)

47
8.0 REFERENCE
1) Danny Poo, Derek Kiong, Swarnalatha Ashok(2017). Object-Oriented programming and
Java(2nd ed.). Google Books.
https://books.google.com.my/books?hl=en&lr=&id=r10U16kgmkwC&oi=fnd&pg=PR3&dq=i
nfo:gu5E8-
ejZSAJ:scholar.google.com/&ots=5VOz5DHOCI&sig=a7Se2p5JaNGqi_0DnUHDdtb
zWzY&redir_esc=y#v=onepage&q&f=false
2) w3schools https://www.w3schools.com/java/
3) Alex Lee (2018). Full Java Course By Alex Lee. YouTube
https://m.youtube.com/playlist?list=PL59LTecnGM1NRUyune3SxzZlYpZezK-oQ

48

You might also like