specifications

The bookkeeping system can record income, expenditure and print a breakdown of income and expenditure.

Skill points

  1. The Scanf function
  2. The Printf function formats
  3. Map collections
  4. Array an array
  5. Slice slice

Implementation effect

Program source code

package main

import (
	"fmt"
	"os"
)

const LINE string = "* * * * * * * * * * * *"

func menu(a) {
	fmt.Println(LINE + "Accounting" + LINE)
	fmt.Printf("% s \ n" 8.10."1. Details of income and expenditure")
	fmt.Printf("% s \ n" 6.10."2. Spending")
	fmt.Printf("% s \ n" 6.10."3. Income")
	fmt.Printf("% s \ n" 6.10."4. Quit")}func main(a) {
	// Initialize variables
	var money int = 10000
	item := []map[string]string{}
	/ / a for loop
	for {
		var no int
		menu()
		fmt.Printf("Please select function number:")
		fmt.Scanf("%d", &no)
		switch no {
		case 1:
			// Account details
			fmt.Println("Serial number collection/Amount")
			/ / order
			for i := 0; len(item) > i; i++ {
				for sz := range item[i] {
					fmt.Printf(17 "% 2 d % 16.20 s % s \ n", i+1, sz, item[i][sz])
				}
			}
			fmt.Printf("Current balance %d yuan \n", money)
		case 2:
			/ / spending
			var expenditure int
			fmt.Printf("Current balance %d yuan \n", money)
			fmt.Printf("This expenditure: >>")
			fmt.Scanf("%d", &expenditure)
			money = money - expenditure
			expenditureStr := fmt.Sprintf("%d", expenditure)
			// Append to the item arraylist
			item = append(item, map[string]string{"-": expenditureStr})
		case 3:
			/ / income
			var earning int
			fmt.Printf("Revenue this time: >>")
			fmt.Scanf("%d", &earning)
			money = money + earning
			earningStr := fmt.Sprintf("%d", earning)
			// Append to the item arraylist
			item = append(item, map[string]string{"+": earningStr})
		case 4:
			/ / exit
			os.Exit(0)}}}Copy the code

Interpretation of important code points

Expenditure and income function, mainly implement logic: define the item array, record the amount in the map format, multiple expenditure/income will be appended to the array, income detailed function, for loop through the number group can be.

item := []map[string]string{}.../ / spending
var expenditure int
fmt.Printf("Current balance %d yuan \n", money)
fmt.Printf("This expenditure: >>")
fmt.Scanf("%d", &expenditure)
money = money - expenditure
expenditureStr := fmt.Sprintf("%d", expenditure)
// Append to the item arraylist
item = append(item, map[string]string{"-": expenditureStr})
Copy the code

More small program cases, continue to update…. Guys, stay tuned