Bokuga Kanga Etasaikyo's FizzBuzz (@Java) with 1 year of programmer experience

Motivation

It was a bit private and messy, so I decided to try something like FizzBuzz, which I hadn't done before. In the first place, I just registered an account and did nothing, so I decided to start by writing something for the time being. If you like, please give us feedback.

Target

Immutable as much as possible, versatile guy.

FizzBuzz requirements

I don't know very well,

1.3 When it is divisible by 3, Fizz 2.5 Buzz when divisible by 5 3. If you can divide either, Fizz Buzz (sometimes Fizz Buzz?) 4. If not divisible, the original number

I felt like saying

  1. Arbitrary character string when divisible by n
  2. If it is divisible by more than one, combine them (connect with "")
  3. If not divisible, the original number
  4. Inclusive to call the first and last number

I tried Source below

FizzBuzzCaller.java


package FizzBuzz;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class FizzBuzzCaller {
	final private Map<Integer, String> callMap;
	
	FizzBuzzCaller(Map<Integer, String> callMap){
		this.callMap = Collections.unmodifiableMap(new HashMap<>(callMap));
	}
	
	public Map<Integer, String> getCallMap(){
		return callMap;
	}
	
	public List<String> getCallsListOnClosedRange(int start, int end){
		return IntStream.rangeClosed(start, end)
			.mapToObj(n ->
				callMap.entrySet().stream()
				.filter(e -> n % e.getKey() == 0)
				.map(e -> e.getValue())
				.reduce((x, y) -> x +" "+ y)
				.orElse(String.valueOf(n))
			)
			.collect(Collectors.toList());
	}
}

test

I included the minus just in case.

CallTest.java


package FizzBuzz;

import java.util.HashMap;
import java.util.Map;

public class CallTest {
	public static void main(String[] arg){
		Map<Integer, String> callMap = new HashMap<>();
		callMap.put(3, "Fizz");
		callMap.put(5, "Buzz");
		callMap.put(-7, "Huss");
		callMap.put(13, "Boss");
		FizzBuzzCaller oneCaller = new FizzBuzzCaller(callMap);
		System.out.println(oneCaller.getCallMap());
		callMap.put(3, "DUZZ");// this does not work
		oneCaller.getCallsListOnClosedRange(-20,100).stream().forEach(System.out::println);
	}
}

The result is like this

console


{3=Fizz, 5=Buzz, -7=Huss, 13=Boss}
Buzz
-19
Fizz
-17
-16
Fizz Buzz
Huss
Boss
Fizz
-11
Buzz
Fizz
-8
Huss
Fizz
Buzz
-4
Fizz
-2
-1
Fizz Buzz Huss Boss
1
2
Fizz
4
Buzz
Fizz
Huss
8
Fizz
Buzz
11
Fizz
Boss
Huss
Fizz Buzz
16
17
Fizz
19
Buzz
Fizz Huss
22
23
Fizz
Buzz
Boss
Fizz
Huss
29
Fizz Buzz
31
32
Fizz
34
Buzz Huss
Fizz
37
38
Fizz Boss
Buzz
41
Fizz Huss
43
44
Fizz Buzz
46
47
Fizz
Huss
Buzz
Fizz
Boss
53
Fizz
Buzz
Huss
Fizz
58
59
Fizz Buzz
61
62
Fizz Huss
64
Buzz Boss
Fizz
67
68
Fizz
Buzz Huss
71
Fizz
73
74
Fizz Buzz
76
Huss
Fizz Boss
79
Buzz
Fizz
82
83
Fizz Huss
Buzz
86
Fizz
88
89
Fizz Buzz
Huss Boss
92
Fizz
94
Buzz
Fizz
97
Huss
Fizz
Buzz

Reflection

Referenced page

Recommended Posts

Bokuga Kanga Etasaikyo's FizzBuzz (@Java) with 1 year of programmer experience
[Java] One type of alphabet prohibited FizzBuzz with binding [Binding]
Let's talk about the passing experience of Java SE 8 Programmer II