Skip to main content
1 of 5
Baig
  • 123
  • 6

Refactor Java Code

Kindly accept my apologies as Java is something my mind cannot digest no matter what I do. At the moment I have an assignment and have written a piece of code and have used Constructor (I think so) but not sure if I have used it correctly and if Yes then will need some guidance in order to refactor the code.

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

public class LocalPlay extends Play {

    private static String playName;
    private static String playDate;

    private LocalPlay(String playName, String playDate) {
        this.playName = playName;
        this.playDate = playDate;
    }

    public String getShowName() {
        return playName;
    }

    public String getShowDate() {
        return playDate;
    }

    public void setShowName(String value) {
        this.playName = value;
    }

    public void setShowDate(String date) {
        this.playDate = date;
    }

    public static void lPlayDetails() {
        try {
            int selection = 0;
            Scanner option = new Scanner(System.in);
            System.out.println();
            System.out.println("Press 1 to Add a New Play/ Show: ");
            System.out.println("Press 2 to View existing Play: ");
            System.out.println("Press 3 to delete existing Play: ");
            System.out.println("Press 4 for Previous Menu: ");
            System.out.println();
            System.out.print("Kindly make the selection: ");
            selection = option.nextInt();
            if (selection == 1) {
                addPlay();
            } else if (selection == 2) {
                viewPlay();
            } else if (selection == 3) {
                deletePlay();
            } else if (selection == 4) {
                managePlay();
            } else
                while (selection != 1 || selection != 2 || selection != 3) {
                    System.out.println();
                    System.out.println(selection + " is not a valid option. ");
                    managePlay();

                }
        } catch (InputMismatchException ime) {
            System.out.println("Invalid option selected.");
            System.out.println("Try again");
            lPlayDetails();
        }
    }

    private static void addPlay() {
        String name;
        String date;
        Scanner option = new Scanner(System.in);
        System.out.print("Enter name of Play: ");
        name = option.nextLine();
        System.out.print("Enter date of Play: ");
        date = option.nextLine();
        LocalPlay lp = new LocalPlay(name, date);
        System.out.println("The name of play is " + playDate + " date played is " + playDate);
        lPlayDetails();
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("play.txt"));
            writer.write(name + " " + date);

        } catch (IOException e) {
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
            }
        }
        return;
    }

    private static void deletePlay() {
        System.out.println("Method not implemented");
        lPlayDetails();

    }

    private static void viewPlay() {
        System.out.println("Method not implemented");
        lPlayDetails();

    }

}

Any feedback will be highly appreciated. Cheers,

Baig
  • 123
  • 6