Assignment

The purpose of this project is only to get you familiar with classes, member functions and data
members.
You are working in the electronic shop where you cell phones. Communication industry moves so fast
and there are a lot of cell phone brands with their facilities introduced to the market every now and
then.
You, as the programmer of the shop, would like to have a history of all cell phone and their capabilities
each year. Although this is possible with simple file processing, you would like to add some objectoriented flavor to it, so here it goes.
Each cell phone has an ID, a Make and a Model as well as the Date that is built and the Date that it is
sold. (Date is consisted of the name of the month, the year and the day of the month as well as weekday
(For example – Saturday, Sunday…).it also has a price that is sold which could be different each time due
to promotions and discounts.
You need to create two classes, one for Date (don’t call your class Date as Date is already used by Java
itself) and one for Cell phone.
Also the cell phone has a cool feature that is it is advertised for (for example graphic colored display or
ability to browse the web).
Phase one:

Create a Date class with a separate driver that shows the class is working. Make sure your object checks
the validity of the date you are entering.

Phase Two:

Create a Phone class that has two Date objects in it. (This is Called Object Composition) along with the
driver that shows the class is working.

Phase Three:

Create options for your program so you can save object information in a sequential file (Append to
the file) and load an object from the file by its ID. You also need to implement an option for listing all
data existing in the file.

Solution 

MainDriver.java 

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class MainDriver {

private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

// output file name

final String OUTPUT_FILE = “phones.txt”;

boolean exit = false;

while (!exit) {

//show menu

System.out.println(“1. Add to file”);

System.out.println(“2. Search in file”);

System.out.println(“3. Display all”);

System.out.println(“4. Quit”);

System.out.print(“Please enter your choice: “);

// read choice

String choice = scanner.nextLine();

System.out.println();

switch (choice) {

case “1”: //add to file

try {

Phone phone = readPhone();

PrintWriter pw = new PrintWriter(new FileWriter(new File(OUTPUT_FILE), true));

pw.append(phone.toFileString() + “\n”);

pw.close();

System.out.println(“Data saved to file”);

} catch (IllegalArgumentException e) {

System.out.println(“Error: ” + e.getMessage());

} catch (IOException e) {

System.out.println(“Erorr: Unable to save to file”);

} catch (Exception e) {

System.out.println(“Error: Invalid input”);

}

break;

case “2”: // search in file

try {

System.out.println(“Enter id to search: “);

String id = scanner.nextLine();

System.out.println(“Found records…”);

Scanner fileScanner = new Scanner(new File(OUTPUT_FILE));

while (fileScanner.hasNextLine()) {

Phone phone = readPhone(fileScanner);

if (phone.getId().equals(id)) {

System.out.println(phone);

System.out.println();

}

}

fileScanner.close();

} catch (IllegalArgumentException e) {

System.out.println(“Error: ” + e.getMessage());

} catch (IOException e) {

System.out.println(“Erorr: Unable to read from file”);

} catch (Exception e) {

System.out.println(“Error: Invalid input”);

}

break;

case “3”: //display all

try {

Scanner fileScanner = new Scanner(new File(OUTPUT_FILE));

while (fileScanner.hasNextLine()) {

Phone phone = readPhone(fileScanner);

System.out.println(phone);

System.out.println();

}

fileScanner.close();

} catch (IllegalArgumentException e) {

System.out.println(“Error: ” + e.getMessage());

} catch (IOException e) {

System.out.println(“Erorr: Unable to read from file”);

} catch (Exception e) {

System.out.println(“Error: Invalid input”);

}

break;

case “4”: //exit

System.out.println(“Good bye!”);

exit = true;

break;

default: //invalid choice

System.out.println(“Error: Invalid choice”);

break;

}

System.out.println();

}

}

// to read phone details from user and create a phone object

private static Phone readPhone() {

System.out.print(“Enter id: “);

String id = scanner.nextLine();

System.out.print(“Enter make: “);

String make = scanner.nextLine();

System.out.print(“Enter model: “);

String model = scanner.nextLine();

System.out.print(“Enter build date in format mm dd yyyy wd (wd – week day 0 for sunday, 1 for monday…): “);

int month = scanner.nextInt();

int day = scanner.nextInt();

int year = scanner.nextInt();

int weekday = scanner.nextInt();

MyDate buildDate = new MyDate(month, year, day, weekday);

System.out.print(“Enter sold date in format mm dd yyyy wd (wd – week day 0 for sunday, 1 for monday…): “);

month = scanner.nextInt();

day = scanner.nextInt();

year = scanner.nextInt();

weekday = scanner.nextInt();

MyDate soldDate = new MyDate(month, year, day, weekday);

System.out.print(“Enter prce: “);

double price = scanner.nextDouble();

scanner.nextLine();

System.out.print(“Enter special feature: “);

String feature = scanner.nextLine();

Phone phone = new Phone(id, make, model, buildDate, soldDate, price, feature);

return phone;

}

// read phone details from a file using scanner passed

private static Phone readPhone(Scanner scanner) {

String id = scanner.nextLine();

String make = scanner.nextLine();

String model = scanner.nextLine();

int month = scanner.nextInt();

int year = scanner.nextInt();

int day = scanner.nextInt();

int weekday = scanner.nextInt();

MyDate buildDate = new MyDate(month, year, day, weekday);

month = scanner.nextInt();

year = scanner.nextInt();

day = scanner.nextInt();

weekday = scanner.nextInt();

MyDate soldDate = new MyDate(month, year, day, weekday);

double price = scanner.nextDouble();

scanner.nextLine();

String feature = scanner.nextLine();

Phone phone = new Phone(id, make, model, buildDate, soldDate, price, feature);

return phone;

}

} 

MyDate.java

public class MyDate{

// dat details

private int month;

private int year;

private int day;

private int weekDay;

// create day after validation

public MyDate(int month, int year, int day, int weekDay) {

// valid details

if(month < 1 || month > 12)

throw new IllegalArgumentException(“Invalid month”);

if(day < 1 || day > 31)

throw new IllegalArgumentException(“Invalid day”);

if(year < 0)

throw new IllegalArgumentException(“Invalid year”);

if(weekDay < 0 || weekDay > 6)

throw new IllegalArgumentException(“Invalid week of day”);

this.month = month;

this.year = year;

this.day = day;

this.weekDay = weekDay;

}

// Getters

public int getMonth() {

return month;

}

public int getYear() {

return year;

}

public int getDay() {

return day;

}

// Week of day as String

public String getWeekDay() {

if( weekDay == 0)

return “Sunday”;

else if( weekDay == 1)

return “Monday”;

else if( weekDay == 2)

return “Tuesday”;

else if( weekDay == 3)

return “Wednesday”;

else if( weekDay == 4)

return “Thursday”;

else if( weekDay == 5)

return “Friday”;

else

return “Saturday”;

}

public String toFileString(){

return month+”\n”+year+”\n”+day+”\n”+weekDay;

}

// date to string value

public String toString() {

return String.format(“%s, %02d/%02d/%04d”, getWeekDay(), getMonth(), getDay(), getYear());

}

} 

MyDateTester.java 

// Tetster to test MyDate Class

public class MyDateTester {

public static void main(String[] args) {

// valid date

try{

MyDate myDate = new MyDate(5, 2016, 1, 0);

System.out.println(myDate);

}catch(Exception e){

System.out.println(“Error: “+e.getMessage());

}

// invalid month

try{

MyDate myDate = new MyDate(13, 2016, 1, 0);

System.out.println(myDate);

}catch(Exception e){

System.out.println(“Error: “+e.getMessage());

}

// invalid year

try{

MyDate myDate = new MyDate(5, -1, 1, 0);

System.out.println(myDate);

}catch(Exception e){

System.out.println(“Error: “+e.getMessage());

}

// invalid day

try{

MyDate myDate = new MyDate(5, 2016, 32, 0);

System.out.println(myDate);

}catch(Exception e){

System.out.println(“Error: “+e.getMessage());

}

// invalid week of day

try{

MyDate myDate = new MyDate(5, 2016, 31, 7);

System.out.println(myDate);

}catch(Exception e){

System.out.println(“Error: “+e.getMessage());

}

}

} 

Phone.java

// Represents a phone

public class Phone {

// phone details

private String id, make, model;

private MyDate builtDate, soldDate;

private double priceSold;

private String feature;

// constructor to create a phone

public Phone(String id, String make, String model, MyDate builtDate, MyDate soldDate, double priceSold,

String feature) {

this.id = id;

this.make = make;

this.model = model;

this.builtDate = builtDate;

this.soldDate = soldDate;

this.priceSold = priceSold;

this.feature = feature;

}

// GETTERs

public String getId() {

return id;

}

public String getMake() {

return make;

}

public String getModel() {

return model;

}

public MyDate getBuiltDate() {

return builtDate;

}

public MyDate getSoldDate() {

return soldDate;

}

public double getPriceSold() {

return priceSold;

}

public String getFeature() {

return feature;

}

// converts phone to write to file string

public String toFileString() {

return id + “\n” + make + “\n” + model + “\n” + builtDate.toFileString() + “\n” + soldDate.toFileString() + “\n”

+ priceSold + “\n” + feature;

}

// to display phone detials to user

public String toString() {

String out = “ID: ” + id + “\nMake: ” + make + ” Model: ” + model;

out += “\nBuilt on: ” + builtDate.toString();

out += “\nSold on: ” + soldDate.toString();

out += “\nPrice sold: ” + priceSold;

out += “\nSpecial Feature: ” + feature;

return out;

}

} 

PhoneTester.java 

// To test phone class

public class PhoneTester {

public static void main(String[] args) {

//create phone

Phone phone = new Phone(“I101”, “Apple”, “6s”, new MyDate(1, 2016, 1, 0), new MyDate(1, 2016, 2, 2), 540,

“Metal Finish”);

// display phone

System.out.println(phone);

// create phone

phone = new Phone(“I102”, “Samsung”, “Galaxy Edge S7”, new MyDate(2, 2016, 25, 0), new MyDate(2, 2017, 21, 2), 225,

“Edge Display”);

// display phone

System.out.println(“\n”+phone);

}

}