Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------- Phone -------------------------------
- package Cls;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Phone {
- private String phoneNumber;
- private String owner;
- private int lastPayment;
- private final int LAST_PAYMENT = -1;
- public Phone(String phoneNumber, String owner) {
- this.phoneNumber = phoneNumber;
- this.owner = owner;
- this.lastPayment = LAST_PAYMENT;
- }
- public Phone(Scanner sc) throws FileNotFoundException {
- parsePhone(sc.nextLine());
- }
- public Phone(String fileName) throws FileNotFoundException {
- File file = new File(fileName);
- Scanner sc = new Scanner(file);
- this.phoneNumber = sc.next();
- this.owner = sc.next();
- sc.close();
- }
- public String getOwner() {
- return owner;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- public int getLastPayment() {
- return lastPayment;
- }
- public void setLastPayment(int lastPayment) {
- this.lastPayment = lastPayment;
- }
- public void save(PrintWriter pw) throws FileNotFoundException {
- pw.println(this.toString());
- }
- public void save(String fileName) throws FileNotFoundException {
- File file = new File(fileName);
- PrintWriter printWriter = new PrintWriter(file);
- printWriter.println(this.toString());
- printWriter.close();
- }
- @Override
- public String toString() {
- return "<" + this.phoneNumber + ">, <" + this.owner + ">, <" + this.lastPayment + ">";
- }
- // A utility method to parse the phone details from a string to the instance arguments
- private void parsePhone(String str) {
- this.phoneNumber = str.substring(str.indexOf('<')+1, str.indexOf('>'));
- this.owner = str.substring(str.indexOf('>')+4, str.lastIndexOf('<')-3);
- this.lastPayment = Integer.parseInt(str.substring(str.lastIndexOf('<')+1, str.lastIndexOf('>')));
- }
- }
- //----------------------- PhoneBook --------------------------
- package Cls;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class PhoneBook {
- //Instance variables
- private String owner;
- private Phone[] phones;
- private int numOfPhones;
- private int counter;
- //Constructors
- public PhoneBook(String owner, int numOfPhones) {
- this.owner = owner;
- this.numOfPhones = numOfPhones;
- counter = 0;
- phones = new Phone[numOfPhones];
- }
- public PhoneBook(String fileName) throws FileNotFoundException {
- File file = new File(fileName);
- Scanner sc = new Scanner(file);
- this.owner = sc.nextLine();
- this.numOfPhones = sc.nextInt();
- phones = new Phone[numOfPhones];
- this.counter = sc.nextInt();
- sc.nextLine();
- for(int i = 0; i < counter; i++) {
- phones[i] = new Phone(sc);
- }
- sc.close();
- }
- // Instance methods
- public void add(Phone phone) {
- phones[counter++] = phone;
- }
- public void save(String filename) throws FileNotFoundException {
- File file = new File(filename);
- PrintWriter printWriter = new PrintWriter(file);
- if(counter > 0) {
- printWriter.println(this.owner);
- printWriter.println(this.numOfPhones);
- printWriter.println(this.counter);
- for(int i = 0; i < counter; i++) {
- phones[i].save(printWriter);
- }
- }
- printWriter.close();
- }
- public void printPhonebook() {
- System.out.println("Owner --> " + this.owner);
- System.out.println("Max number of phone numbers --> " + this.numOfPhones);
- System.out.println("Current number of phones --> " + this.counter);
- System.out.println("\nPhones details:\n------------");
- for(int i = 0; i < this.counter; i++) {
- System.out.println(phones[i].toString());
- }
- }
- }
- //----------------------------- Tester for both parts (convert the part you want to ignore to notes) ----
- package Tester;
- import Cls.Phone;
- import Cls.PhoneBook;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Tester {
- public static void main(String[] args) {
- //Test part 1
- /*
- Phone phone1 = new Phone("055-1234567", "Eran");
- Phone phone2 = new Phone("077-7654321", "Ofri");
- Phone phone3 = new Phone("052-5555555", "Noa");
- String fileName = "PhoneBook.phones";
- try {
- save(phone1, phone2, phone3, fileName);
- // Assigning phones 1-3 to phones 4-6
- File file = new File(fileName);
- Scanner sc = new Scanner(file);
- Phone ph4 = new Phone(sc);
- Phone ph5 = new Phone(sc);
- Phone ph6 = new Phone(sc);
- //Check phones 4-6 initialization
- System.out.println(ph4.toString());
- System.out.println(ph5.toString());
- System.out.println(ph6.toString());
- sc.close();
- } catch(FileNotFoundException fnfe) {
- System.out.println(fnfe.toString());
- }
- }
- private static void save(Phone ph1, Phone ph2, Phone ph3, String fName) throws FileNotFoundException {
- File file = new File(fName);
- PrintWriter printWriter = new PrintWriter(file);
- ph1.save(printWriter);
- ph2.save(printWriter);
- ph3.save(printWriter);
- printWriter.close();
- }
- */
- //test part 2
- try {
- String fileName = "phonebook.phones";
- //section a
- PhoneBook phoneBook = new PhoneBook("Eran", 5);
- //section b
- phoneBook.add(new Phone("057-1234567", "Israel"));
- phoneBook.add(new Phone("056-1111111", "Shalom"));
- phoneBook.add(new Phone("054-8778787", "Yossi"));
- //section c
- phoneBook.save(fileName);
- //section d
- PhoneBook pb = new PhoneBook(fileName);
- //section e
- checkCorrectness(pb);
- } catch (FileNotFoundException fnfe) {
- System.out.println(fnfe.toString());
- }
- }
- private static void checkCorrectness(PhoneBook phBook) {
- phBook.printPhonebook();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement