Golang-demo exercises

main/main.go

package main

import (
	"laurence/utils"
)
func main() {
	//面向对象
	familAccount := utils.NewFamilyAccount()
	familAccount.MainMenu()
}

utils/familAccount.go 

package utils 
import "fmt" 
type FamilAccount struct { 
	Option string `json: option` 
	Loop bool` json: loop` 
	Balance float64 `json: balance` 
	Money float64` json: money` 
	Note string `json: note` 
	Flag bool` json: flag` 
	Detail string `json: detail` 
} 
// Factory mode returns a pointer to the front end using 
func NewFamilyAccount () * FamilAccount { 
	return & FamilAccount { 
		Option:" ", 
		Loop: true, 
		Balance: 10000.00, 
		Money: 0.00, 
		Note:" ", 
		Flag: false, 
		Detail:" Revenue and Expenditure \ 
tAccount Balance \ tAmount and Expenses \ tDescription \ n ", 
	} 
} func (familAccount * FamilAccount) showDetail () { 
	if familAccount.Flag {
		fmt.Println (familAccount.Detail) 
	} else { 
		fmt.Println ("No records !!!!!!!!!!") 
	} 
	fmt.Println () 
} 
func (familAccount * FamilAccount) Income () { 
	fmt. Print ("Amount of this income:") 
	fmt.Scanln (& familAccount.Money) 
	fmt.Print ("Description of this income:") 
	fmt.Scanln (& familAccount.Note) 
	familAccount.Balance + = familAccount.Money 
	// Format Splice 
	familAccount.Detail + = fmt.Sprintf ("Revenue \ t% v \ t \ t% v \ t \ t% v \ n", familAccount.Balance, familAccount.Money, familAccount.Note) 
	familAccount.Flag = true 
} 
func (familAccount * FamilAccount) Pay () { 
	fmt.Print ("This spending amount:") 
	fmt.Scanln (& familAccount.Money) 
	fmt.Print ("Description of this expenditure: ")
	fmt.Scanln (& familAccount.Note) 
	if familAccount.Money> familAccount.Balance { 
		fmt.Print ("Awkward, insufficient balance !!!!!!!!!!!!!!") 
	} 
	familAccount.Balance-= familAccount. Money 
	// Format stitching 
	familAccount.Detail + = fmt.Sprintf ("Expenditure \ t% v \ t \ t% v \ t \ t% v \ n", familAccount.Balance, familAccount.Money, familAccount.Note) 
	familAccount .Flag = true 
} 
func (familAccount * FamilAccount) Exit () { 
	fmt.Print ("Are you sure you want to exit? Y / n:") 
	choice: = "" 
	for { 
		fmt.Scanln (& choice) 
		if choice == " y "|| choice ==" n "{ 
			break 
		} 
		fmt.Print (" The input is incorrect, please re-enter: ") 
	} 
	if choice ==" y "{
		familAccount.Loop = false
	}
} 
func (familAccount * FamilAccount) MainMenu () { 
	for { 
		fmt.Println ("---------- Family revenue and expenditure accounting details ------------") 
		fmt. Println ("1 Income and Expenditure Details") 
		fmt.Println ("2 Registered Income") 
		fmt.Println ("3 Registered Expenses") 
		fmt.Println ("4 Exit") 
		fmt.Println ("Please select (1-4) ") 
		fmt.Println (" -------------------------------------- ") 
		fmt.Print ("Please select the option you want to operate:") 
		fmt.Scanln (& familAccount.Option) 
		switch familAccount.Option { 
			case "1": 
				familAccount.showDetail () 
			case "2": 
				familAccount.Income () 
			case "3":
				familAccount.Pay()
			case "4":
				familAccount.Exit () 
			default:
				fmt.Println ("The input is incorrect, please reselect:") 
		} 
		if! familAccount.Loop { 
			fmt.Println ("Welcome to visit next time, Goodbay !!!!!!!!!!!! ") 
			break 
		} 
	} 
} 
// TODO 
// Verify login 
// Complete the transfer function

  

Guess you like

Origin www.cnblogs.com/Essaycode/p/12677743.html