Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go and Vue.js

Martin Kock mkock

💭
Currently building stuff in Go and Vue.js
View GitHub Profile
public interface Vehicle {
    public void start();
    public void stop();
}
public class Car implements Vehicle {
public void start() {
System.out.println("starting engine...");
}
public void stop() {
type student interface {
register(context.Context, string, string, int)
enrollCourse(uint32, time.Time) error
pass(uint32) error
fail(uint32) error
}
type alsoStudent interface {
register(ctx context.Context, name, profession string, age int)
enrollCourse(courseID uint32, start time.Time) error
type foobar interface {
foo()
bar()
}
type itemA struct{}
func (a *itemA) foo() {
fmt.Println("foo on A")
}
@mkock
mkock / busride_code_snippet_30.go
Created August 20, 2020 19:29
Bus Ride, code snippet 30
Starting simulation
Express Line: starting
Express Line: arriving at "Downtown"
Passenger with SSN 12345612-22: charged 5.00 of arbitrary money
Express Line: boarded passenger with SSN "12345612-22"
Passenger with SSN 11223322-67: charged 3.50 of arbitrary money
Express Line: boarded passenger with SSN "11223322-67"
    Passenger with SSN "12345612-22" is heading to "The University"
    Passenger with SSN "11223322-67" is heading to "The Village"
Express Line: carrying 2 passengers: heading for next stop
@mkock
mkock / busride_code_snippet_29.go
Created August 20, 2020 19:29
Bus Ride, code snippet 29
john := busservice.Prospect{
SSN:         "12345612-22",
Destination: &s2,
}
betty := busservice.Prospect{
SSN:         "11223322-67",
Destination: &s3,
}
@mkock
mkock / busride_code_snippet_28.go
Created August 20, 2020 19:28
Bus Ride, code snippet 28
// SeniorAge is the minimum age from which a Passenger is considered a senior to the BusCompany.
const SeniorAge = 65
// IsSenior returns true if the Passenger is a senior, and false otherwise.
// IsSenior detects age by extracting the last two digits from the SSN and treating them like an age.
func (p Passenger) IsSenior() bool {
age,err:=strconv.ParseInt(p.SSN[len(p.SSN)-2:],10,8)
if err != nil {
panic("invalid SSN: " + p.SSN)
}
@mkock
mkock / busride_code_snippet_27.go
Created August 20, 2020 19:27
Bus Ride, code snippet 27
// NotifyBusArrival is called by Bus upon arrival.
func (b *BusStop) NotifyBusArrival(bus *Bus) {
for _, p := range b.prospects {
if bus.StopsAt(p.Destination) {
pas := p.ToPassenger()
bus.Board(pas, bus.Company.GetPricing()(pas))
}
}
}
@mkock
mkock / busride_code_snippet_26.go
Created August 20, 2020 19:26
Bus Ride, code snippet 26
// BusCompany represents the bus company responsible for the Bus service. BusCompany determines price policies.
type BusCompany string
// GetPricing returns a price calculator based on the pricing policy of the day.
func (b BusCompany) GetPricing() PriceCalculator {
wd := time.Now().Weekday()
if wd == time.Saturday || wd == time.Sunday {
return WeekendPricing
}
return WorkdayPricing
@mkock
mkock / busride_code_snippet_25.go
Created August 20, 2020 19:25
Bus Ride, code snippet 25
// WorkdayPricing charges EUR 6 for regular Passengers and EUR 4.5 for seniors during workdays.
func WorkdayPricing(p Passenger) float64 {
if p.IsSenior() {
return 4.5
}
return 6.0
}
// WeekendPricing charges EUR 5 for regular Passengers and EUR 3.5 for seniors during weekends.
func WeekendPricing(p Passenger) float64 {
@mkock
mkock / busride_code_snippet_24.go
Last active September 6, 2020 11:32
Bus Ride, code snippet 24
// Board adds the given Passenger to the Bus and charges them a ticket price calculated by chargeFn if they don't already have a paid ticket.
// Board returns false if the Passenger was not allowed to board the Bus.
func (b *Bus) Board(p *Passenger, chargeFn PriceCalculator) bool {
var allowed bool // Default value is false
if p.HasValidTicket {
allowed = true
} else {
amount := chargeFn(*p)
passenger := p.Charge(amount)
allowed = passenger.HasValidTicket