This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type foobar interface { | |
| foo() | |
| bar() | |
| } | |
| type itemA struct{} | |
| func (a *itemA) foo() { | |
| fmt.Println("foo on A") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| john := busservice.Prospect{ | |
| SSN: "12345612-22", | |
| Destination: &s2, | |
| } | |
| betty := busservice.Prospect{ | |
| SSN: "11223322-67", | |
| Destination: &s3, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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)) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |