[GO] I solved it with Java! !! Paiza Exercise "Long Table Eel Shop" ~ Object Oriented Edition ~

Introduction

This article is written by beginners in programming. What's wrong, you should do this more! !! If you have any questions, Please comment.

This question is also a paiza exercise. Please be assured that there is nothing to do with the rank up or down.

スクリーンショット 2018-10-29 22.17.03.png

The one I wrote in a non-object oriented way

I tried to solve it in Java! !! Paiza Exercise "Long Table Eel Shop"

This is a procedural (is it correct?) Solution. Because I had some object-oriented code when I posted this article I digested it in my own way.

Please check here for the specific details of the problem.

Think object-oriented

Code written in non-object orientation

import java.util.*;

public class unagiya {

    public static void main(String[] args) {
        /*
         *Get the total number of seats in the store and the number of groups visiting the store as a String type and store it in line
         *Divide the data stored in line by split and store it in lineArray of String array
         */
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        String[] lineArray = line.split(" ");
        /*
         *Cast the data stored in the lineArray and store it in each variable
         * 
         * seatNum:Number of seats
         * totalGuest:Number of groups visiting the store
        */
        int seatNum = castString(lineArray[0]);
        int totalGuest = castString(lineArray[1]);

        /*
         * seats:Create the number of seats with hashMap
         *Integer is the seat number
         *Boolean is the presence or absence of people(If true, you can sit down,)
         */
        Map<Integer,Boolean> seats = new HashMap<>();
        for(int i = 1; i <= seatNum; i++){
            seats.put(i,true);
        }
        /*
         *isSitCustomer:Check if there are any people in the seat chosen by the customer
         *If not, throw true
         *
         * choiceSeat:Seat number that is the base point for customers to choose
         *menberNum:Number of customers who came
         */

        int choiceSeat = 0;
        int memberNum = 0;

        for(int i = 0; i < totalGuest; i++){
            //Receives console input
            //Assign to memberNum and choiceSeat
            line = scanner.nextLine();
            lineArray = line.split(" ");

            memberNum = castString(lineArray[0]);
            choiceSeat = castString(lineArray[1]);

            //If isSitGuest is true, rewrite the seat information selected by the customer and the number of seats to false.
            if(isSitGuest(seats,choiceSeat,memberNum)){
                for(int j = 0,index = 1 ; j < memberNum; j++){
                    if(seats.size() < choiceSeat + j){
                        seats.put(index, false);
                        index++;
                    } else {
                        seats.put(choiceSeat + j,false);
                    }
                }
            }
        }

        //Check the false number of seat information and assign it to the result
        int result = 0;

        for(int i = 1; i <= seats.size(); i++){
            if(seats.get(i).equals(false)){
                result++;
            }
        }
        System.out.println(result);
    }

    //Cast a String type number to an int type
    public static int castString(String strNum){
        int num = Integer.parseInt(strNum);
        return num;
    }

    /*
     *Map Receive seat information, seats selected by customers, number of customers,
     *Check if there is false for the number of customers from the seat selected by the customer
     * 
     *conditions
     *Returns false if there are more guests than seats
     *Returns false if there is even one false in the seat information
     */
    public static boolean isSitGuest(Map seats, int choiceSeat, int menberNum){
        if(menberNum > seats.size()){
            return false;    
        }

        boolean flag = true;

        for(int i = 0,index = 1; i < menberNum; i++){

            //index:Specify the very first 1 of the seat information
            //Used when counting from 1 when the number of customers exceeds the seat number

            if(seats.size() < choiceSeat + i){
                if(seats.get(index).equals(false)){
                    flag = false;
                    index++;
                } else {
                    index++;
                }
            } else if(seats.get(choiceSeat + i).equals(false)){
                flag = false;
            }

            }
        if(flag){
            return true;
        } else {
            return false;
        }
    }
}

But who wrote this code ...

I read it back after about two weeks, but it's hard to see. I make seats with Map and do various things ... (I stopped reading in the middle)

At that time, I was in a good mood, saying, "I was able to write a decent code."

However, a word from shiracamus who appeared like a meteor "Because it's a big deal, let's be object-oriented."

Here is the code I left with that word (We have permission from shiracamus to post the code)

@ Shiracamus's code

import java.util.*;

enum Seat { AVAILABLE, TAKEN };

class LongTable {
    private Seat seat[];

    LongTable(int seatNum) {
        seat = new Seat[seatNum];
        for (int i = 0; i < seatNum; i++) {
            seat[i] = Seat.AVAILABLE;
        }
    }

    boolean sit(int position, int num) {
        for (int i = position; i < position + num; i++) {
            if (seat[i % seat.length] != Seat.AVAILABLE) {
                return false;
            }
        }
        for (int i = position; i < position + num; i++) {
            seat[i % seat.length] = Seat.TAKEN;
        }
        return true;
    }
}

public class Unagiya {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] input = scanner.nextLine().split(" ");
        int seatNum = Integer.parseInt(input[0]);
        int groupNum = Integer.parseInt(input[1]);

        LongTable table = new LongTable(seatNum);

        int sitCount = 0;
        for (int i = 0; i < groupNum; i++){
            input = scanner.nextLine().split(" ");
            int memberNum = Integer.parseInt(input[0]);
            int seatNumber = Integer.parseInt(input[1]);

            if (table.sit(seatNumber, memberNum)) {
                sitCount += memberNum;
            }
        }

        System.out.println(sitCount);
    }
}

What is object-oriented? ?? ??

The moment I saw this code, an electric shock struck my body.

Probably rewritten based on my code.

Then I spend more time, I will try to write in my own "object-oriented".

Appearance object of eel shop

I start thinking about the characters first.

Let's consider it with reference to the flowchart I wrote earlier. スクリーンショット 2018-10-27 23.01.49.png

First of all ・ Long table

enum Seat {AVAILABLE,UNAVAILABLE}

From TAKEN Changed to the target UNAVAILABLE.

And the table. Since the table is a table, there are no methods (behaviors). It just exists there.


class LongTable {
	Seat[] seats;
	/**
	 *Constructor to create a table
	 *The initial value is AVAILABLE
	 * @param tableNum
	 */
	LongTable(Integer tableNum) {
		this.seats = new Seat[tableNum];
		for (int i = 0; i < tableNum; i++) {
			seats[i] = Seat.AVAILABLE;
		}
	}
}

Next is the Edokko customer. This time he is a little quick.

If there are no people in the seat (isSit) Sit (sitting) If there is a person in the seat (isSit) go home

class Guest {
	/**
	 *A method of throwing true if there is no one in the seat chosen by the customer
	 * @param guestNum
	 * @param choiceNum
	 * @param table
	 * @return
	 */
	public static Boolean isSit(Integer guestNum, Integer choiceNum, LongTable table) {
		Boolean seatConfirmation = true;
		for (int i = choiceNum; i < choiceNum + guestNum; i++) {
			if (table.seats[i % table.seats.length] == Seat.UNAVAILABLE) {
				seatConfirmation = false;
			}
		}
		return seatConfirmation;
	}
	/**
	 *A method in which a customer sits and makes the seat UNAVAILABLE
	 * @param guestNum
	 * @param choiceNum
	 * @param table
	 */
	public static void sitting(Integer guestNum, Integer choiceNum, LongTable table) {
		for (int i = choiceNum; i < choiceNum + guestNum; i++) {
			table.seats[i % table.seats.length] = Seat.UNAVAILABLE; 
		}
	}
}

Then, counting the number of people sitting in the seat is ... Maybe you will be the staff of the store.

class Staff {
	/**
	 *A method for counting the number of people sitting
	 * @param table
	 * @return count(Count the number of UNAVAILABLE seats)
	 */
	public static Integer sitCount(LongTable table) {
		Integer count = 0;
		for (int i = 0; i < table.seats.length; i++) {
			if (table.seats[i] == Seat.UNAVAILABLE) {
				count++;
			}
		}
		return count;
	}
}

The characters are now complete. All you have to do is watch the story progress.

Main method description

The story is described in the main method.

public class Unagiya {
	public static void main(String[] args) throws Exception{
		//input: Get one line of input value from the console
		//inpuArray: Store the numbers in one row separately in an array
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		String[] inputArray = input.split(" ");
		
		//tableNum: Number of eel seats
		//toDayGuest: How many groups of guests will come today
		Integer tableNum = toCastString(inputArray[0]);
		Integer toDayGuest = toCastString(inputArray[1]);
		
		LongTable table = new LongTable(tableNum);
		
		//guestNum: Number of people in one group
		//choiceNum: Seat chosen by the guest
		Integer guestNum = 0;
		Integer choiceNum = 0;
		for (int i = 0; i < toDayGuest; i++) {
			
			input = scanner.nextLine();
			inputArray = input.split(" ");
			
			guestNum = toCastString(inputArray[0]);
			choiceNum = toCastString(inputArray[1]);
			
			if (Guest.isSit(guestNum, choiceNum, table)) {
				Guest.sitting(guestNum, choiceNum, table);
			}
		}
		Integer count = Staff.sitCount(table);
		System.out.println(count);
	}
	/**
	 *Method to convert Arabic numeral of String type to Integer type
	 * @param strNum
	 * @return num
	 */
	public static Integer toCastString(String strNum) {
		Integer num = Integer.parseInt(strNum);
		return num;
	}
	
}

result

スクリーンショット 2018-11-13 0.18.25.png

I passed without any problems.

reference

https://www.slideshare.net/masuda220/ss-14263541 This slide is very helpful, but Since the binding is very strict, I am doing it a little loosely ww

Paiza

Opinions, impressions, improvements, my bad words, looking for

This is the second article posted, though I'm afraid.

I'm probably ignoring cancer, such as Qiita's tacit understanding.

If you have read the above, I would appreciate it if you could keep some records. Thank you to everyone who read to the end.

Recommended Posts

I solved it with Java! !! Paiza Exercise "Long Table Eel Shop" ~ Object Oriented Edition ~
I solved it with Java! !! Paiza Exercise "Long Table Eel Shop"
[Spoilers] Long table eel shop (equivalent to paiza rank B)