Skip to content

Instantly share code, notes, and snippets.

@aldy505
Created November 25, 2021 14:11
Show Gist options
  • Save aldy505/0aa2c5eb7e3e62f469ef705e6ff2a5f1 to your computer and use it in GitHub Desktop.
Save aldy505/0aa2c5eb7e3e62f469ef705e6ff2a5f1 to your computer and use it in GitHub Desktop.
A simple financial-technology company to learn more about structs and method in Go
// Now we're creating a fintech (financial technology) company
// that you can store money to and can buy stocks.
//
// Like the previous challenge (well, not really lol - more like a homework)
// we use struct and methods to do things. Now we'll kind of
// do the same thing.
package main
import (
"fmt"
"os"
)
func main() {
_, err := NewCompany("Djikstra", 0)
if err == nil {
fmt.Println("This should return an error. Why didn't it?")
os.Exit(1)
}
_, err = NewCompany("ABC", 0)
if err != nil {
fmt.Println("This should not be an error. Why did it?")
os.Exit(1)
}
company, err := NewCompany("jnck", 10_000)
if err != nil {
fmt.Println("this one shouldn't return an error. why did it?")
os.Exit(1)
}
company.AddFunds(200)
company.WithdrawFunds(300)
company.AddFunds(90)
company.WithdrawFunds(1000)
company.AddFunds(10)
company.WithdrawFunds(999)
audit := company.ValidateTransactions()
if !audit {
fmt.Println("no body should be keciduk by KPK. benerin dulu logicnya")
os.Exit(1)
} else {
fmt.Println("passed the audit")
}
company.Holdings -= 200
audit = company.ValidateTransactions()
if audit {
fmt.Println("someone should be keciduk by KPK. benerin dulu logicnya")
os.Exit(1)
}
}
// Create a struct here first.
// The struct should be named "Company" and consist of:
// Name: string
// Holdings: int64 (How much money does the bank have)
// Transactions: []Transaction (another struct, which consist of
// a name, date, and amount - with date being time.Time and
// amount being int64)
func NewCompany(name string, holdings int64) (*Company, error) {
// With the provided name, you should validate
// the name so that it only has 4 maximum letter
// otherwise it will return an error.
//
// Also make the name uppercase.
// Clue: see strings standard library
// and the method named ToUpper.
if name != 4 {
// Please fix the if conditional logic
// above. It's not working!
return &Company{}, errors.New("name should have a maximum of 4 letters")
}
return &Company{
Name: name,
Transactions: []Transaction{{
Name: "initial value",
Amount: holdings,
}}
}, nil
}
func (c *Company) AddFunds(amount float64) {
// First of all, we should round the float64
// to its' nearest value. To do this,
// see the math standard library.
// https://pkg.go.dev/math and see how to
// round a float64.
// Also, make sure that the value provided is
// not a negative number. If it is, do an
// absolute convertion. To make things easier,
// see the math.Abs() method from the
// math standard library.
// Then, append the transactions of the company.
// on the c.Transactions.
// Treat it like a log book for banking.
}
func (c *Company) WithdrawFunds(amount float64) {
// Like the previous one, we should round the
// float64 first to its' nearest value.
// Before we do anything, let's validate first
// if that the amount that we withdraw won't
// make the company bankrupt. Meaning the amount
// of holdings after the withdraw must NOT be
// less than zero.
// If it makes the company bankrupt (meaning that
// the resulting amount is less than zero), we'll just:
panic("Company is bankrupt!")
// If it's not, log into the c.Transactions with the
// amount being minus.
}
func (c *Company) ValidateTransactions() bool {
// To prevent us from being keciduk by KPK, we'll
// need to audit our transactions.
// We'll walk through the c.Transactions one by one
// and calculate it, if the sum (jumlah) of the amount in
// each transaction would be equal to the holdings that
// we own.
var transactionAmount int64
for i := 0; i < len(c.Transactions); i++ {
transactionAmount += c.Transactions[i].Amount
}
return transactionAmount == c.Holdings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment