Public Class Form1
#Region "Classes"
Private Class 地區
Implements IComparable
'shared
Public Shared ReadOnly 地區表 As New SortedDictionary(Of String, 地區)
'properties
Public ReadOnly ID As Integer
Public ReadOnly 名稱 As String
Public 出版社 As New List(Of 出版社)
'generation
''' <summary>
''' It's for preventing the key(name) of the regions is repeated.
''' </summary>
''' <param name="取現有值">Actually, it's crap and can be removed.</param>
Public Shared Function 建立(ByVal 名稱 As String, ByVal ID As Integer, _
Optional ByVal 取現有值 As Boolean = False) As 地區
Dim nw地區 As 地區 = Nothing
If 地區表.TryGetValue(名稱, nw地區) Then
If 取現有值 Then Return nw地區 Else Return Nothing
Else
Return New 地區(名稱, ID)
End If
End Function
Private Sub New(ByVal 名稱 As String, ByVal ID As Integer)
Me.名稱 = 名稱
Me.ID = ID
地區表.Add(名稱, Me)
End Sub
Public Sub 移除() 'crap also.
Me.出版社.Clear()
地區表.Remove(Me.名稱)
GC.Collect(GC.GetGeneration(Me), GCCollectionMode.Forced)
End Sub
'Impements
Public Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo
Select Case obj.GetType
Case GetType(地區)
Return Me.名稱.CompareTo(CType(obj, 地區).名稱)
Case GetType(String)
Return Me.名稱.CompareTo(CStr(obj))
Case GetType(Byte), GetType(Short), GetType(Integer), GetType(Long)
Return Me.ID.CompareTo(CLng(obj))
Case Else
Throw New Exception(String.Format("地區 cannot compare to {0}.", obj.GetType()))
End Select
End Function
End Class
Private Class 出版社
Implements IComparable
'shared
Public Shared ReadOnly 出版社表 As New SortedDictionary(Of String, 出版社)
'properties
Public ReadOnly ID As Integer
Public ReadOnly 名稱 As String
Public ReadOnly 地區() As 地區
'generation
''' <summary>
''' It's for preventing the key(name) of the publishers is repeated.
''' </summary>
''' <param name="地區">The ParamArray for this program only.</param>
Public Shared Function 建立(ByVal 名稱 As String, ByVal ID As Integer, _
ByVal ParamArray 地區() As 地區) As 出版社
Dim nw出版社 As 出版社 = Nothing
If 出版社表.TryGetValue(名稱, nw出版社) Then
Return Nothing
Else
Return New 出版社(名稱, ID, 地區)
End If
End Function
Private Sub New(ByVal 名稱 As String, ByVal ID As Integer, ByVal 地區() As 地區)
Me.名稱 = 名稱
Me.ID = ID
Me.地區 = 地區
For Each e地區 As 地區 In 地區
e地區.出版社.Add(Me)
Next
出版社表.Add(名稱, Me)
End Sub
Public Sub 移除() 'crap+1
For Each e地區 As 地區 In 地區
e地區.出版社.Remove(Me)
Next
出版社表.Remove(Me.名稱)
GC.Collect(GC.GetGeneration(Me), GCCollectionMode.Forced)
End Sub
'implements
Public Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo
Select Case obj.GetType
Case GetType(出版社)
Return Me.名稱.CompareTo(CType(obj, 出版社).名稱)
Case GetType(String)
Return Me.名稱.CompareTo(CStr(obj))
Case GetType(Byte), GetType(Short), GetType(Integer), GetType(Long)
Return Me.ID.CompareTo(CLng(obj))
Case Else
Throw New Exception(String.Format("出版社 cannot compare to {0}.", obj.GetType()))
End Select
End Function
End Class
#End Region
#Region "Initialization"
Private Sub ME_LOAD(sender As Object, e As EventArgs) Handles Me.Load
'Creating the regions
Dim TWA As 地區 = 地區.建立("台灣957", 957)
Dim TWB As 地區 = 地區.建立("台灣986", 986)
Dim CNA As 地區 = 地區.建立("中國81", 81)
Dim CNB As 地區 = 地區.建立("中國93", 93)
'Dim KOR As 地區 = 地區.建立("北韓", 9946) 'No publishers
'Create Press,, of Tai Wan
出版社.建立("�眳p", 181, TWA, TWB)
出版社.建立("全華", 21, TWA, TWB)
出版社.建立("松崗", 22, TWA, TWB)
',, of Mainland CN
出版社.建立("中國北京大學", 301, CNA, CNB)
出版社.建立("北京市東方", 5060, CNA, CNB)
'GUIs
ComboBox1.Items.Clear()
ComboBox2.Items.Clear()
For Each 地區 As 地區 In 地區.地區表.Values
ComboBox1.Items.Add(地區.名稱)
Next
End Sub
#End Region
#Region "Controls Handles"
''' <summary>
''' Handling Button1.Click, generate a New ISBN number then display in Label4.
''' </summary>
''' <param name="sender">Button1</param>
''' <param name="e">Nothing</param>
''' <remarks>The ISBN number will start with 978 or 979 randomly with the same probability</remarks>
Private Sub Button1_Click(sender As Button, e As System.EventArgs) Handles Button1.Click
Randomize()
If ComboBox1.SelectedIndex < 0 Or ComboBox2.SelectedIndex < 0 Then Exit Sub 'debug
Dim s$ = CStr(IIf(Rnd() > 0.5, "978", "979")) & _
地區.地區表(ComboBox1.SelectedItem).ID & _
出版社.出版社表(ComboBox2.SelectedItem).ID
s &= New String("0", 12 - s.Length - TextBox1.Text.Length) & TextBox1.Text
Dim b As Boolean, sm As Integer
For Each c As Char In s.ToCharArray()
If b Then
sm += (Asc(c) - 48) * 3
Else
sm += Asc(c) - 48
End If
b = Not b
Next c
s &= 10 - (sm Mod 10)
#If DEBUG Then
Debug.Print(s)
#End If
Label4.Text = s
End Sub
''' <summary>
''' Handling Combox1.SelectedIndexChanged. Change the publishers while the region is changed.
''' </summary>
''' <param name="sender">Combobox1</param>
''' <remarks>If the region is changed to nothing, Combobox2 will be cleared. </remarks>
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As ComboBox, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
ComboBox2.Items.Clear()
If sender.SelectedIndex >= 0 Then
For Each 出版社 As 出版社 In 地區.地區表(sender.SelectedItem).出版社
ComboBox2.Items.Add(出版社.名稱)
Next
End If
End Sub
#End Region
End Class
★Kit↘..