Initial commit
This commit is contained in:
204
.gitignore
vendored
Normal file
204
.gitignore
vendored
Normal file
@@ -0,0 +1,204 @@
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
.idea/**/sonarlint/
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
.idea/**/sonarIssues.xml
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
.idea/**/markdown-navigator.xml
|
||||
.idea/**/markdown-navigator-enh.xml
|
||||
.idea/**/markdown-navigator/
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
.idea/$CACHE_FILE$
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
.idea/codestream.xml
|
||||
|
||||
# Azure Toolkit for IntelliJ plugin
|
||||
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
|
||||
.idea/**/azureSettings.xml
|
||||
|
||||
### Java ###
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
|
||||
### MicrosoftOffice ###
|
||||
*.tmp
|
||||
|
||||
# Word temporary
|
||||
~$*.doc*
|
||||
|
||||
# Word Auto Backup File
|
||||
Backup of *.doc*
|
||||
|
||||
# Excel temporary
|
||||
~$*.xls*
|
||||
|
||||
# Excel Backup File
|
||||
*.xlk
|
||||
|
||||
# PowerPoint temporary
|
||||
~$*.ppt*
|
||||
|
||||
# Visio autosave temporary files
|
||||
*.~vsd*
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
### VisualStudioCode Patch ###
|
||||
# Ignore all local history of files
|
||||
.history
|
||||
.ionide
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
127
BankSystem/.github/copilot-instructions.md
vendored
Normal file
127
BankSystem/.github/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
# 角色设定
|
||||
|
||||
你是一位经验丰富的软件开发专家与编码助手,精通所有主流编程语言与框架。你的用户是一名独立开发者,正在进行个人或自由职业项目开发。你的职责是协助生成高质量代码、优化性能、并主动发现和解决技术问题。
|
||||
|
||||
---
|
||||
|
||||
# 核心目标
|
||||
|
||||
高效协助用户开发代码,并在无需反复提示的前提下主动解决问题。关注以下核心任务:
|
||||
|
||||
- 编写代码
|
||||
- 优化代码
|
||||
- 调试与问题解决
|
||||
|
||||
确保所有解决方案都清晰易懂,逻辑严密。
|
||||
|
||||
---
|
||||
|
||||
## 阶段一:初始评估
|
||||
|
||||
1. 用户发出请求时,优先检查项目中的 `README.md` 文档以理解整体架构与目标。
|
||||
2. 若无文档,主动创建一份 `README.md`,包括功能说明、使用方式和核心参数。
|
||||
3. 利用已有上下文(文件、代码)充分理解需求,避免偏差。
|
||||
|
||||
---
|
||||
|
||||
## 阶段二:代码实现
|
||||
|
||||
### 1. 明确需求
|
||||
- 主动确认需求是否清晰,若有疑问,应立即询问。
|
||||
- 推荐最简单有效的方案,避免不必要的复杂设计。
|
||||
|
||||
### 2. 编写代码
|
||||
- 阅读现有代码,明确实现步骤。
|
||||
- 选择合适语言与框架,并遵循最佳实践(如 SOLID 原则)。
|
||||
- 编写简洁、可读、带注释的代码。
|
||||
- 优化可维护性与性能。
|
||||
- 按需提供单元测试。
|
||||
- 遵循语言标准编码规范(如 Python 使用 PEP 8)。
|
||||
|
||||
### 3. 调试与问题解决
|
||||
- 系统化分析问题,找出根因。
|
||||
- 明确说明问题来源及解决方式。
|
||||
- 在问题解决过程中持续与用户沟通,如需求变动能快速适应。
|
||||
|
||||
---
|
||||
|
||||
## 阶段三:完成与总结
|
||||
|
||||
1. 清晰总结本轮改动、完成目标与优化内容。
|
||||
2. 标注潜在风险或需留意的边界情况。
|
||||
3. 更新项目文档(如 `README.md`)以反映最新进展。
|
||||
|
||||
---
|
||||
|
||||
# 最佳实践
|
||||
|
||||
### Sequential Thinking(逐步思考工具)
|
||||
|
||||
使用 [Sequential Thinking](https://github.com/smithery-ai/reference-servers/tree/main/src/sequentialthinking) 工具,以结构化的思维方式处理复杂、开放性问题。
|
||||
|
||||
- 将任务拆解为若干 **思维步骤(thought steps)**。
|
||||
- 每一步应包括:
|
||||
1.**明确当前目标或假设**(如:“分析登录方案”,“优化状态管理结构”)。
|
||||
2.**调用合适的 MCP 工具**(如 `search_docs`、`code_generator`、`error_explainer`),用于执行查文档、生成代码或解释错误等操作。Sequential Thinking 本身不产出代码,而是协调过程。
|
||||
3.**清晰记录本步骤的结果与输出**。
|
||||
4.**确定下一步目标或是否分支**,并继续流程。
|
||||
|
||||
- 在面对不确定或模糊任务时:
|
||||
- 使用“分支思考”探索多种方案。
|
||||
- 比较不同路径的优劣,必要时回滚或修改已完成的步骤。
|
||||
|
||||
- 每个步骤可带有如下结构化元数据:
|
||||
-`thought`: 当前思考内容
|
||||
-`thoughtNumber`: 当前步骤编号
|
||||
-`totalThoughts`: 预估总步骤数
|
||||
-`nextThoughtNeeded`, `needsMoreThoughts`: 是否需要继续思考
|
||||
-`isRevision`, `revisesThought`: 是否为修订行为,及其修订对象
|
||||
-`branchFromThought`, `branchId`: 分支起点编号及标识
|
||||
|
||||
- 推荐在以下场景使用:
|
||||
- 问题范围模糊或随需求变化
|
||||
- 需要不断迭代、修订、探索多解
|
||||
- 跨步骤上下文保持一致尤为重要
|
||||
- 需要过滤不相关或干扰性信息
|
||||
|
||||
---
|
||||
|
||||
### Context7(最新文档集成工具)
|
||||
|
||||
使用 [Context7](https://github.com/upstash/context7) 工具获取特定版本的最新官方文档与代码示例,用于提升生成代码的准确性与当前性。
|
||||
|
||||
-**目的**:解决模型知识过时问题,避免生成已废弃或错误的 API 用法。
|
||||
|
||||
-**使用方式**:
|
||||
1.**调用方式**:在提示词中加入 `use context7` 触发文档检索。如果不确定部分用法时也建议自动使用。
|
||||
2.**获取文档**:Context7 会拉取当前使用框架/库的相关文档片段。
|
||||
3.**集成内容**:将获取的示例与说明合理集成到你的代码生成或分析中。
|
||||
|
||||
-**按需使用**:**仅在需要时调用 Context7**,例如遇到 API 模糊、版本差异大或用户请求查阅官方用法。避免不必要的调用,以节省 token 并提高响应效率。
|
||||
|
||||
-**集成方式**:
|
||||
- 支持 Cursor、Claude Desktop、Windsurf 等 MCP 客户端。
|
||||
- 通过配置服务端集成 Context7,即可在上下文中获取最新参考资料。
|
||||
|
||||
-**优势**:
|
||||
- 提升代码准确性,减少因知识过时造成的幻觉与报错。
|
||||
- 避免依赖训练时已过期的框架信息。
|
||||
- 提供明确、权威的技术参考材料。
|
||||
|
||||
---
|
||||
|
||||
# 沟通规范
|
||||
|
||||
- 除非用户要求以另一种语言回答,否则所有内容必须使用用户向你发出请求时使用的语言交流(包括代码注释)。
|
||||
- 遇到不清楚的内容应立即向用户提问。
|
||||
- 表达清晰、简洁、技术准确。
|
||||
- 在代码中应添加必要的注释解释关键逻辑。
|
||||
- 无论何时你想提问,总是调用 MCP mcp-feedback-enhanced(interactive_feedback命令)。
|
||||
|
||||
# MCP Interactive Feedback 规则
|
||||
|
||||
1. 在任何流程、任务、对话进行时,无论是询问、回复、或完成阶段性任务,皆必须调用 MCP mcp-feedback-enhanced。
|
||||
2. 每当收到用户反馈,若反馈内容非空,必须再次调用 MCP mcp-feedback-enhanced,并根据反馈内容调整行为。
|
||||
3. 仅当用户明确表示「结束」或「不再需要交互」时,才可停止调用 MCP mcp-feedback-enhanced,流程才算结束。
|
||||
4. 除非收到结束指令,否则所有步骤都必须重复调用 MCP mcp-feedback-enhanced。
|
||||
5. 完成任务前,必须使用 MCP mcp-feedback-enhanced 工具向用户询问反馈。
|
||||
38
BankSystem/.gitignore
vendored
Normal file
38
BankSystem/.gitignore
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
10
BankSystem/.idea/.gitignore
generated
vendored
Normal file
10
BankSystem/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# 依赖于环境的 Maven 主目录路径
|
||||
/mavenHomeManager.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
7
BankSystem/.idea/encodings.xml
generated
Normal file
7
BankSystem/.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
14
BankSystem/.idea/misc.xml
generated
Normal file
14
BankSystem/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
9
BankSystem/.idea/runConfigurations/BankSystem.xml
generated
Normal file
9
BankSystem/.idea/runConfigurations/BankSystem.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="BankSystem" type="Application" factoryName="Application" singleton="false">
|
||||
<option name="MAIN_CLASS_NAME" value="BankSystem" />
|
||||
<module name="BankSystem" />
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
124
BankSystem/.idea/uiDesigner.xml
generated
Normal file
124
BankSystem/.idea/uiDesigner.xml
generated
Normal file
@@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
||||
4
BankSystem/.idea/vcs.xml
generated
Normal file
4
BankSystem/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||
</project>
|
||||
76
BankSystem/README.md
Normal file
76
BankSystem/README.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# 简易银行管理系统
|
||||
|
||||
这是一个使用 Java Swing 构建的简单银行账户管理系统。
|
||||
|
||||
## 功能
|
||||
|
||||
* **账户管理**:
|
||||
* 开户:创建一个新的银行账户,系统会自动生成唯一的账户ID。
|
||||
* 销户:删除一个现有账户。只有当账户余额为零时才能成功销户。
|
||||
* 查询:根据账户ID查询账户的详细信息,包括账户持有人姓名和当前余额。
|
||||
* **资金管理**:
|
||||
* 存款:向指定账户存入金额。
|
||||
* 取款:从指定账户取出金额,账户余额必须充足。
|
||||
* 转账:在两个指定账户之间转移金额,源账户余额必须充足。
|
||||
|
||||
## 使用方式
|
||||
|
||||
1. 运行 `BankSystem.java` 文件启动应用程序。
|
||||
2. **开户**:
|
||||
* 点击“开户”按钮。
|
||||
* 在弹出的对话框中输入账户持有人姓名。
|
||||
* 系统会提示账户创建成功,并显示新生成的账户ID。
|
||||
3. **查询账户**:
|
||||
* 在“账户号”输入框中输入要查询的账户ID。
|
||||
* 点击“查询”按钮。
|
||||
* 如果账户存在,其姓名和余额将显示在相应的不可编辑字段中。
|
||||
4. **存款**:
|
||||
* 首先查询一个账户或在“账户号”输入框中输入账户ID。
|
||||
* 点击“存款”按钮。
|
||||
* 在弹出的对话框中输入存款金额。
|
||||
* 操作成功后,账户余额会自动更新(如果当前查询的是该账户)。
|
||||
5. **取款**:
|
||||
* 首先查询一个账户或在“账户号”输入框中输入账户ID。
|
||||
* 点击“取款”按钮。
|
||||
* 在弹出的对话框中输入取款金额。
|
||||
* 操作成功后,账户余额会自动更新(如果当前查询的是该账户)。
|
||||
6. **转账**:
|
||||
* 首先查询源账户或在“账户号”输入框中输入源账户ID。
|
||||
* 点击“转账”按钮。
|
||||
* 在弹出的第一个对话框中输入目标账户ID。
|
||||
* 在弹出的第二个对话框中输入转账金额。
|
||||
* 操作成功后,源账户余额会自动更新(如果当前查询的是源账户)。
|
||||
7. **销户**:
|
||||
* 首先查询一个账户或在“账户号”输入框中输入要销户的账户ID。
|
||||
* 点击“销户”按钮。
|
||||
* 系统会要求确认操作。
|
||||
* 只有当账户余额为零时,销户才能成功。成功后,界面上的账户信息字段将被清空。
|
||||
|
||||
## 数据存储
|
||||
|
||||
所有账户数据都存储在项目根目录下的 `accounts.json` 文件中。每个账户对象包含以下键:
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"accountId": "ACC16228383922",
|
||||
"name": "张三",
|
||||
"balance": 1000.50
|
||||
},
|
||||
{
|
||||
"accountId": "ACC16228383950",
|
||||
"name": "李四",
|
||||
"balance": 500.00
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
* `accountId`: (String) 唯一的账户标识符。
|
||||
* `name`: (String) 账户持有人的姓名。
|
||||
* `balance`: (double) 账户的当前余额。
|
||||
|
||||
## 技术栈
|
||||
|
||||
* Java
|
||||
* Swing (用于GUI)
|
||||
* Gson (用于JSON数据处理)
|
||||
1
BankSystem/accounts.json
Normal file
1
BankSystem/accounts.json
Normal file
@@ -0,0 +1 @@
|
||||
[{"accountId":"ACC1749542730783","name":"lisi","balance":1.0},{"accountId":"ACC1749553162251","name":"zhangsan","balance":50.0},{"accountId":"ACC1749560275526","name":"张三","balance":10.0}]
|
||||
30
BankSystem/pom.xml
Normal file
30
BankSystem/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>me.momokko</groupId>
|
||||
<artifactId>BankSystem</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.13.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.formdev</groupId>
|
||||
<artifactId>flatlaf</artifactId>
|
||||
<version>3.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
12
BankSystem/src/main/java/BankSystem.java
Normal file
12
BankSystem/src/main/java/BankSystem.java
Normal file
@@ -0,0 +1,12 @@
|
||||
import com.formdev.flatlaf.FlatDarculaLaf;
|
||||
|
||||
public class BankSystem {
|
||||
public static void main(String[] args) {
|
||||
FlatDarculaLaf.setup();
|
||||
javax.swing.SwingUtilities.invokeLater(() -> {
|
||||
BankSystemUI ui = new BankSystemUI();
|
||||
ui.pack();
|
||||
ui.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
241
BankSystem/src/main/java/BankSystemCore.java
Normal file
241
BankSystem/src/main/java/BankSystemCore.java
Normal file
@@ -0,0 +1,241 @@
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BankSystemCore {
|
||||
private final Gson gson = new Gson();
|
||||
private static final String ACCOUNTS_FILE_PATH = "accounts.json";
|
||||
|
||||
private List<Account> loadAccounts() {
|
||||
if (!Files.exists(Paths.get(ACCOUNTS_FILE_PATH))) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
try (FileReader reader = new FileReader(ACCOUNTS_FILE_PATH)) {
|
||||
Type accountListType = new TypeToken<ArrayList<Account>>() {}.getType();
|
||||
List<Account> accounts = gson.fromJson(reader, accountListType);
|
||||
return accounts == null ? new ArrayList<>() : accounts;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean saveAccounts(List<Account> accounts) {
|
||||
try (FileWriter writer = new FileWriter(ACCOUNTS_FILE_PATH)) {
|
||||
gson.toJson(accounts, writer);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Account CreateAccount(String name) {
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<Account> accounts = loadAccounts();
|
||||
String accountId = "ACC" + System.currentTimeMillis();
|
||||
Account newAccount = new Account(accountId, name.trim(), 0.0);
|
||||
accounts.add(newAccount);
|
||||
if (saveAccounts(accounts)) {
|
||||
return newAccount;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Account QueryAccount(String accountId) throws AccountNotFoundException {
|
||||
if (accountId == null || accountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("账户ID不能为空。");
|
||||
}
|
||||
List<Account> accounts = loadAccounts();
|
||||
Optional<Account> accountOpt = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(accountId.trim()))
|
||||
.findFirst();
|
||||
if (accountOpt.isPresent()) {
|
||||
return accountOpt.get();
|
||||
} else {
|
||||
throw new AccountNotFoundException("账户 " + accountId + " 未找到。");
|
||||
}
|
||||
}
|
||||
|
||||
void DeleteAccount(String accountId) throws AccountNotFoundException, AccountOperationException {
|
||||
if (accountId == null || accountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("账户ID不能为空。");
|
||||
}
|
||||
List<Account> accounts = loadAccounts();
|
||||
Optional<Account> accountOpt = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(accountId.trim()))
|
||||
.findFirst();
|
||||
|
||||
if (accountOpt.isPresent()) {
|
||||
Account accountToDelete = accountOpt.get();
|
||||
if (accountToDelete.getBalance() == 0.0) {
|
||||
accounts.remove(accountToDelete);
|
||||
if (!saveAccounts(accounts)) {
|
||||
throw new AccountOperationException("删除账户 " + accountId + " 时保存失败。");
|
||||
}
|
||||
} else {
|
||||
throw new AccountOperationException("账户 " + accountId + " 余额不为零,无法删除。");
|
||||
}
|
||||
} else {
|
||||
throw new AccountNotFoundException("要删除的账户 " + accountId + " 未找到。");
|
||||
}
|
||||
}
|
||||
|
||||
void Deposit(String accountId, double amount) throws AccountNotFoundException, InvalidAmountException, AccountOperationException {
|
||||
if (accountId == null || accountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("账户ID不能为空。");
|
||||
}
|
||||
if (amount <= 0) {
|
||||
throw new InvalidAmountException("存款金额必须为正数。");
|
||||
}
|
||||
List<Account> accounts = loadAccounts();
|
||||
Optional<Account> accountOpt = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(accountId.trim()))
|
||||
.findFirst();
|
||||
|
||||
if (accountOpt.isPresent()) {
|
||||
Account account = accountOpt.get();
|
||||
accounts.remove(account);
|
||||
Account updatedAccount = new Account(account.getAccountId(), account.getName(), account.getBalance() + amount);
|
||||
accounts.add(updatedAccount);
|
||||
if (!saveAccounts(accounts)) {
|
||||
throw new AccountOperationException("存款到账户 " + accountId + " 时保存失败。");
|
||||
}
|
||||
} else {
|
||||
throw new AccountNotFoundException("存款账户 " + accountId + " 未找到。");
|
||||
}
|
||||
}
|
||||
|
||||
void Withdraw(String accountId, double amount) throws AccountNotFoundException, InvalidAmountException, InsufficientBalanceException, AccountOperationException {
|
||||
if (accountId == null || accountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("账户ID不能为空。");
|
||||
}
|
||||
if (amount <= 0) {
|
||||
throw new InvalidAmountException("取款金额必须为正数。");
|
||||
}
|
||||
List<Account> accounts = loadAccounts();
|
||||
Optional<Account> accountOpt = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(accountId.trim()))
|
||||
.findFirst();
|
||||
|
||||
if (accountOpt.isPresent()) {
|
||||
Account account = accountOpt.get();
|
||||
if (account.getBalance() >= amount) {
|
||||
accounts.remove(account);
|
||||
Account updatedAccount = new Account(account.getAccountId(), account.getName(), account.getBalance() - amount);
|
||||
accounts.add(updatedAccount);
|
||||
if (!saveAccounts(accounts)) {
|
||||
throw new AccountOperationException("从账户 " + accountId + " 取款时保存失败。");
|
||||
}
|
||||
} else {
|
||||
throw new InsufficientBalanceException("账户 " + accountId + " 余额不足。当前余额: " + account.getBalance() + ", 取款金额: " + amount);
|
||||
}
|
||||
} else {
|
||||
throw new AccountNotFoundException("取款账户 " + accountId + " 未找到。");
|
||||
}
|
||||
}
|
||||
|
||||
void Transfer(String fromAccountId, String toAccountId, double amount) throws AccountNotFoundException, InvalidAmountException, InsufficientBalanceException, AccountOperationException {
|
||||
if (fromAccountId == null || fromAccountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("源账户ID不能为空。");
|
||||
}
|
||||
if (toAccountId == null || toAccountId.trim().isEmpty()) {
|
||||
throw new AccountNotFoundException("目标账户ID不能为空。");
|
||||
}
|
||||
if (fromAccountId.trim().equals(toAccountId.trim())) {
|
||||
throw new AccountOperationException("源账户和目标账户不能相同。");
|
||||
}
|
||||
if (amount <= 0) {
|
||||
throw new InvalidAmountException("转账金额必须为正数。");
|
||||
}
|
||||
|
||||
List<Account> accounts = loadAccounts();
|
||||
Account fromAccount = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(fromAccountId.trim()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AccountNotFoundException("源账户 " + fromAccountId + " 未找到。"));
|
||||
|
||||
Account toAccount = accounts.stream()
|
||||
.filter(acc -> acc.getAccountId().equals(toAccountId.trim()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AccountNotFoundException("目标账户 " + toAccountId + " 未找到。"));
|
||||
|
||||
if (fromAccount.getBalance() >= amount) {
|
||||
accounts.remove(fromAccount);
|
||||
accounts.remove(toAccount);
|
||||
|
||||
Account updatedFromAccount = new Account(fromAccount.getAccountId(), fromAccount.getName(), fromAccount.getBalance() - amount);
|
||||
Account updatedToAccount = new Account(toAccount.getAccountId(), toAccount.getName(), toAccount.getBalance() + amount);
|
||||
|
||||
accounts.add(updatedFromAccount);
|
||||
accounts.add(updatedToAccount);
|
||||
|
||||
if (!saveAccounts(accounts)) {
|
||||
throw new AccountOperationException("转账时保存账户信息失败。");
|
||||
}
|
||||
} else {
|
||||
throw new InsufficientBalanceException("源账户 " + fromAccountId + " 余额不足。当前余额: " + fromAccount.getBalance() + ", 转账金额: " + amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Custom Exception Classes
|
||||
class AccountNotFoundException extends Exception {
|
||||
public AccountNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class InsufficientBalanceException extends Exception {
|
||||
public InsufficientBalanceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidAmountException extends Exception {
|
||||
public InvalidAmountException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class AccountOperationException extends Exception {
|
||||
public AccountOperationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
class Account {
|
||||
private String accountId;
|
||||
private String name;
|
||||
private double balance;
|
||||
|
||||
public Account(String accountId, String name, double balance) {
|
||||
this.accountId = accountId;
|
||||
this.name = name;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public String getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
128
BankSystem/src/main/java/BankSystemUI.form
Normal file
128
BankSystem/src/main/java/BankSystemUI.form
Normal file
@@ -0,0 +1,128 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="BankSystemUI">
|
||||
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="286a" class="javax.swing.JTextField" binding="accountIdField">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="ecf1" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="账户号"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="4efa6" class="javax.swing.JButton" binding="QueryButton">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="查询"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="fd2f8" class="javax.swing.JButton" binding="CreateButton">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="开户"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ea349" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="姓名"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="63a1c" class="javax.swing.JTextField" binding="NameField">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<editable value="false"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="67ce4" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="余额"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="1c909" class="javax.swing.JTextField" binding="BalanceField">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<editable value="false"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="41396" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="b7d15" class="javax.swing.JButton" binding="DepositButton">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="存款"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="e9d2e">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="5b666" class="javax.swing.JButton" binding="WithdrawButton">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="取款"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="98815" class="javax.swing.JButton" binding="TransferButton">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="转账"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="8faef" class="javax.swing.JButton" binding="DeleteButton">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="销户"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
142
BankSystem/src/main/java/BankSystemUI.java
Normal file
142
BankSystem/src/main/java/BankSystemUI.java
Normal file
@@ -0,0 +1,142 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class BankSystemUI extends JDialog {
|
||||
private JTextField accountIdField;
|
||||
private JButton QueryButton, CreateButton, DepositButton, WithdrawButton, TransferButton, DeleteButton;
|
||||
private JTextField NameField, BalanceField;
|
||||
private JPanel contentPane;
|
||||
|
||||
private BankSystemCore core = new BankSystemCore();
|
||||
|
||||
public BankSystemUI() {
|
||||
setResizable(false);
|
||||
setContentPane(contentPane);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
setTitle("简易银行系统");
|
||||
|
||||
QueryButton.addActionListener(e -> queryAccountAction());
|
||||
|
||||
CreateButton.addActionListener(e -> {
|
||||
String name = JOptionPane.showInputDialog(this, "请输入账户名:", "创建账户", JOptionPane.PLAIN_MESSAGE);
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
if (name != null) {
|
||||
JOptionPane.showMessageDialog(this, "账户名不能为空。", "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Account account = core.CreateAccount(name.trim());
|
||||
if (account != null) {
|
||||
JOptionPane.showMessageDialog(this, "账户创建成功!\n账户ID: " + account.getAccountId(), "创建账户成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
accountIdField.setText(account.getAccountId());
|
||||
queryAccountAction();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "创建账户失败。", "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, "创建账户时发生错误: " + ex.getMessage(), "创建账户错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
DepositButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入账户ID。", "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入存款金额:", "存款", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Deposit(id, Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "存款成功。", "存款成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "存款错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
WithdrawButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入账户ID。", "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入取款金额:", "取款", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Withdraw(id, Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "取款成功。", "取款成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "取款错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
TransferButton.addActionListener(e -> {
|
||||
String from = accountIdField.getText().trim();
|
||||
if (from.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入源账户ID。", "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String to = JOptionPane.showInputDialog(this, "请输入目标账户ID:", "转账", JOptionPane.PLAIN_MESSAGE);
|
||||
if (to == null || to.trim().isEmpty()) return;
|
||||
String amt = JOptionPane.showInputDialog(this, "请输入转账金额:", "转账", JOptionPane.PLAIN_MESSAGE);
|
||||
if (amt == null || amt.trim().isEmpty()) return;
|
||||
try {
|
||||
core.Transfer(from, to.trim(), Double.parseDouble(amt.trim()));
|
||||
JOptionPane.showMessageDialog(this, "转账成功。", "转账成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
queryAccountAction();
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "无效的金额格式。", "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "转账错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
DeleteButton.addActionListener(e -> {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "请输入要删除的账户ID。", "删除错误", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
int res = JOptionPane.showConfirmDialog(this, "确认删除账户 " + id + "?", "删除确认", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
|
||||
if (res == JOptionPane.YES_OPTION) {
|
||||
try {
|
||||
core.DeleteAccount(id);
|
||||
JOptionPane.showMessageDialog(this, "删除成功。", "删除成功", JOptionPane.INFORMATION_MESSAGE);
|
||||
accountIdField.setText("");
|
||||
clearFields();
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "删除错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void queryAccountAction() {
|
||||
String id = accountIdField.getText().trim();
|
||||
if (id.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this, "账户ID不能为空。", "查询错误", JOptionPane.ERROR_MESSAGE);
|
||||
clearFields();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Account acc = core.QueryAccount(id);
|
||||
NameField.setText(acc.getName());
|
||||
BalanceField.setText(String.format("%.2f", acc.getBalance()));
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "查询错误", JOptionPane.ERROR_MESSAGE);
|
||||
clearFields();
|
||||
}
|
||||
}
|
||||
|
||||
private void clearFields() {
|
||||
NameField.setText("");
|
||||
BalanceField.setText("");
|
||||
}
|
||||
|
||||
}
|
||||
BIN
BankSystem/实验报告-实验8.docx
Normal file
BIN
BankSystem/实验报告-实验8.docx
Normal file
Binary file not shown.
8
Example_src/Chapter1-10/.classpath
Normal file
8
Example_src/Chapter1-10/.classpath
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||
<classpathentry kind="lib" path="/ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
8
Example_src/Chapter1-10/.idea/.gitignore
generated
vendored
Normal file
8
Example_src/Chapter1-10/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
6
Example_src/Chapter1-10/.idea/encodings.xml
generated
Normal file
6
Example_src/Chapter1-10/.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
5
Example_src/Chapter1-10/.idea/misc.xml
generated
Normal file
5
Example_src/Chapter1-10/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
Example_src/Chapter1-10/.idea/modules.xml
generated
Normal file
8
Example_src/Chapter1-10/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/javaEE.iml" filepath="$PROJECT_DIR$/javaEE.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
4
Example_src/Chapter1-10/.idea/vcs.xml
generated
Normal file
4
Example_src/Chapter1-10/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||
</project>
|
||||
28
Example_src/Chapter1-10/.project
Normal file
28
Example_src/Chapter1-10/.project
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>javaEE</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1741074534549</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
11
Example_src/Chapter1-10/.settings/org.eclipse.jdt.core.prefs
Normal file
11
Example_src/Chapter1-10/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
11
Example_src/Chapter1-10/bin/ch10/userinfo.sql
Normal file
11
Example_src/Chapter1-10/bin/ch10/userinfo.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
DROP TABLE IF EXISTS `userinfo`;
|
||||
CREATE TABLE `userinfo`
|
||||
(
|
||||
`userId` int(11) NOT NULL auto_increment,
|
||||
`loginname` varchar(20) default NULL,
|
||||
`password` varchar(10) default NULL,
|
||||
PRIMARY KEY (`userId`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
INSERT INTO `userinfo` (`userId`, `loginname`, `password`)
|
||||
VALUES (1, 'admin', 'admin');
|
||||
52
Example_src/Chapter1-10/javaEE.iml
Normal file
52
Example_src/Chapter1-10/javaEE.iml
Normal file
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="EclipseModuleManager">
|
||||
<libelement value="file:///ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar" />
|
||||
<src_description expected_position="1">
|
||||
<src_folder value="file://$MODULE_DIR$/src" expected_position="1" />
|
||||
</src_description>
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<output url="file://$MODULE_DIR$/bin" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="junit5">
|
||||
<CLASSES>
|
||||
<root url="$MAVEN_REPOSITORY$/junit/junit/3.8.2/junit-3.8.2.jar" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="mysql-connector-java-5.1.44-bin.jar">
|
||||
<CLASSES>
|
||||
<root url="file:///ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit5.8.1">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
10
Example_src/Chapter1-10/src/ch01/HelloApplet.java
Normal file
10
Example_src/Chapter1-10/src/ch01/HelloApplet.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package ch01;
|
||||
|
||||
import java.applet.Applet;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class HelloApplet extends Applet {
|
||||
public void paint(Graphics g) {
|
||||
g.drawString("这是一个最简单的Applet程序", 40, 50);
|
||||
}
|
||||
}
|
||||
14
Example_src/Chapter1-10/src/ch01/HelloWorld.java
Normal file
14
Example_src/Chapter1-10/src/ch01/HelloWorld.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ch01;
|
||||
|
||||
public class HelloWorld {
|
||||
public static void main(String args[]) {
|
||||
System.out.println("Hello World!");
|
||||
int a = 123, b;
|
||||
b = a >>> 33;
|
||||
System.out.println("b=" + b);
|
||||
int[] AB = new int[]{6, 80, 2, 55, 451};
|
||||
System.out.println("AB[0]=" + AB[0]);
|
||||
String[][] str1 = new String[][]{new String[3], new String[]{"hello"}};
|
||||
System.out.println("str1[0][0]=" + str1[0]);
|
||||
}
|
||||
}
|
||||
27
Example_src/Chapter1-10/src/ch02/ArrayDemo.java
Normal file
27
Example_src/Chapter1-10/src/ch02/ArrayDemo.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package ch02;
|
||||
|
||||
public class ArrayDemo {
|
||||
public static void main(String args[]) {
|
||||
char[] AB;
|
||||
AB = new char[3];
|
||||
AB[0] = 'a';
|
||||
AB[1] = 'b';
|
||||
AB[2] = 'c';
|
||||
System.out.println(AB[0]);
|
||||
System.out.println(AB[1]);
|
||||
System.out.println(AB[2]);
|
||||
int[] AC = new int[2];
|
||||
AC[0] = 100;
|
||||
AC[1] = 200;
|
||||
System.out.println(AC[0]);
|
||||
System.out.println(AC[1]);
|
||||
int[] AD = {10, 20, 30, 40};
|
||||
System.out.println(AD[0]);
|
||||
System.out.println(AD[1]);
|
||||
System.out.println(AD[2]);
|
||||
System.out.println(AD[3]);
|
||||
System.out.println(AB.length);
|
||||
System.out.println(AC.length);
|
||||
System.out.println(AD.length);
|
||||
}
|
||||
}
|
||||
9
Example_src/Chapter1-10/src/ch02/EscapeRout.java
Normal file
9
Example_src/Chapter1-10/src/ch02/EscapeRout.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ch02;
|
||||
|
||||
public class EscapeRout {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("a\u0022.length() + \u0022b".length());
|
||||
System.out.println("a".length() + "b".length());
|
||||
System.out.println("a\".length()+\"b".length());
|
||||
}
|
||||
}
|
||||
29
Example_src/Chapter1-10/src/ch02/LeapYear.java
Normal file
29
Example_src/Chapter1-10/src/ch02/LeapYear.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ch02;
|
||||
|
||||
public class LeapYear {
|
||||
int year, month, day;
|
||||
|
||||
void setdate(int y, int m, int d) //成员方法,设置日期值
|
||||
{ //公有的,无返回值,有三个参数
|
||||
year = y;
|
||||
month = m;
|
||||
day = d;
|
||||
}
|
||||
|
||||
boolean isleapyear() //判断年份是否为闰年
|
||||
{ //布尔型返回值,无参数
|
||||
return (year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0);
|
||||
}
|
||||
|
||||
void print() //输出日期值,无返回值,无参数
|
||||
{
|
||||
System.out.println("date is " + year + '-' + month + '-' + day);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
LeapYear a = new LeapYear(); //创建对象
|
||||
a.setdate(2012, 12, 25); //调用类方法
|
||||
a.print();
|
||||
System.out.println(a.year + " is a leap year? " + a.isleapyear());
|
||||
}
|
||||
}
|
||||
45
Example_src/Chapter1-10/src/ch02/Num2Rmb.java
Normal file
45
Example_src/Chapter1-10/src/ch02/Num2Rmb.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package ch02;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Num2Rmb {
|
||||
private String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
||||
private String[] unitArr = {"拾", "佰", "仟"};
|
||||
|
||||
/*private String[] divide(double num)把一个浮点数分解成整数部分和小数部分字符串
|
||||
num 需要被分解的浮点数,return 分解出来的整数部分和小数部分。
|
||||
第一个数组元素是整数部分,第二个数组元素是小数部分 */
|
||||
private String[] divide(double num) {
|
||||
long zheng = (long) num; //将一个浮点数强制类型转换为long,即得到它的整数部分
|
||||
long xiao = Math.round((num - zheng) * 100);//浮点数减去整数部分乘以100再取整得到两位小数
|
||||
//下面用了两种方法把整数转换为字符串
|
||||
return new String[]{zheng + "", String.valueOf(xiao)};
|
||||
}
|
||||
|
||||
/*private String toHanStr(String numStr)把一个四位的数字字符串变成汉字字符串
|
||||
numStr 需要被转换的四位的数字字符串,return 四位的数字字符串被转换成的汉字字符串 */
|
||||
private String toHanStr(String numStr) {
|
||||
String result = "";
|
||||
int numLen = numStr.length();
|
||||
for (int i = 0; i < numLen; i++)//依次遍历数字字符串的每一位数字
|
||||
{
|
||||
int num = numStr.charAt(i) - 48;//把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48
|
||||
if (i != numLen - 1 && num != 0) //如果不是最后一位数字且数字不是零,则需要添加单位(仟、佰、拾)
|
||||
{
|
||||
result += hanArr[num] + unitArr[numLen - 2 - i];
|
||||
} else //否则不要添加单位
|
||||
{
|
||||
result += hanArr[num];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Num2Rmb nr = new Num2Rmb();
|
||||
System.out.println(Arrays.toString(nr.divide(326811125.45)));//测试把一个浮点数分解成整数部分和小数部分
|
||||
System.out.println(nr.toHanStr("8906"));//测试把一个四位的数字字符串变成汉字字符串
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
Example_src/Chapter1-10/src/ch02/SymmetryArray.java
Normal file
20
Example_src/Chapter1-10/src/ch02/SymmetryArray.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ch02;
|
||||
|
||||
public class SymmetryArray {
|
||||
public static void main(String[] args) {
|
||||
int[] n = {1, 2, 0, 2, 1};
|
||||
boolean flag = true; //假设对称
|
||||
for (int i = 0; i < n.length / 2; i++) { //循环数组长度的一半次
|
||||
if (n[i] != n[n.length - i - 1]) //比较元素
|
||||
{
|
||||
flag = false; //不对称
|
||||
break; //结束循环
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
System.out.println("对称");
|
||||
} else {
|
||||
System.out.println("不对称");
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Example_src/Chapter1-10/src/ch02/TestArrays.java
Normal file
18
Example_src/Chapter1-10/src/ch02/TestArrays.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ch02;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestArrays {
|
||||
public static void main(String[] args) {
|
||||
int[] a = new int[]{3, 4, 5, 6}; // 定义一个a数组
|
||||
int[] a2 = new int[]{3, 4, 5, 6}; // 定义一个a2数组
|
||||
System.out.println("a数组和a2数组是否相等:" + Arrays.equals(a, a2)); // 将输出true
|
||||
int[] b = Arrays.copyOf(a, 6); // 通过复制a数组,生成一个新的b数组
|
||||
System.out.println("a数组和b数组是否相等:" + Arrays.equals(a, b));
|
||||
System.out.println("b数组的元素为:" + Arrays.toString(b)); // 输出b数组的元素
|
||||
Arrays.fill(b, 2, 4, 1); // 将b数组的第2个元素(包含)到第4个元素(不包含)赋值为1
|
||||
System.out.println("b数组的元素为:" + Arrays.toString(b)); // 输出b数组的元素
|
||||
Arrays.sort(b); // 对b数组进行排序
|
||||
System.out.println("b数组的元素为:" + Arrays.toString(b)); // 输出b数组的元素
|
||||
}
|
||||
}
|
||||
13
Example_src/Chapter1-10/src/ch02/TestLoan.java
Normal file
13
Example_src/Chapter1-10/src/ch02/TestLoan.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ch02;
|
||||
|
||||
public class TestLoan {
|
||||
public static void main(String args[]) {
|
||||
double dbp = 300000;
|
||||
double dbi = 0.0459 / 12;
|
||||
int nt = 30 * 12;
|
||||
double dbpow = 1;
|
||||
for (int i = 0; i < nt; i++) dbpow = dbpow * (1 + dbi);
|
||||
double dbr = dbp * dbi * dbpow / (dbpow - 1);
|
||||
System.out.println("你每月需还贷款R=" + dbr + "元/月");
|
||||
}
|
||||
}
|
||||
18
Example_src/Chapter1-10/src/ch02/TestTwoDimension.java
Normal file
18
Example_src/Chapter1-10/src/ch02/TestTwoDimension.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ch02;
|
||||
|
||||
public class TestTwoDimension {
|
||||
public static void main(String[] args) {
|
||||
int[][] a; //定义一个二维数组
|
||||
a = new int[3][]; //初始化a是一个长度为3的数组,a数组的数组元素又是引用类型
|
||||
for (int i = 0; i < a.length; i++) //把a数组当成一维数组,遍历a数组的每个数组元素
|
||||
{
|
||||
System.out.println(a[i]);
|
||||
}
|
||||
a[0] = new int[2]; //初始化a数组的第一个元素
|
||||
a[0][1] = 6; //访问a数组的第一个元素所指数组的第二个元素
|
||||
for (int i = 0; i < a[0].length; i++) //a数组的第一个元素是一个一维数组,遍历这个一维数组
|
||||
{
|
||||
System.out.println(a[0][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Example_src/Chapter1-10/src/ch02/TestparseInt.java
Normal file
12
Example_src/Chapter1-10/src/ch02/TestparseInt.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package ch02;
|
||||
|
||||
public class TestparseInt {
|
||||
public static void main(String args[]) {
|
||||
String str1 = "100";
|
||||
String str2 = "12345678";
|
||||
int int1 = Integer.parseInt(str1, 16);
|
||||
long lng = Long.valueOf(str2).intValue();
|
||||
System.out.println(int1);
|
||||
System.out.println(lng);
|
||||
}
|
||||
}
|
||||
12
Example_src/Chapter1-10/src/ch02/Testvalue.java
Normal file
12
Example_src/Chapter1-10/src/ch02/Testvalue.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package ch02;
|
||||
|
||||
public class Testvalue {
|
||||
public static void main(String args[]) {
|
||||
long lng = 12345678;
|
||||
int int1 = 100;
|
||||
String str1 = String.valueOf(lng);
|
||||
String str2 = Integer.toString(int1, 16).toUpperCase();
|
||||
System.out.println(str1);
|
||||
System.out.println(str2);
|
||||
}
|
||||
}
|
||||
15
Example_src/Chapter1-10/src/ch02/TwoDementionArray.java
Normal file
15
Example_src/Chapter1-10/src/ch02/TwoDementionArray.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package ch02;
|
||||
|
||||
public class TwoDementionArray {
|
||||
public static void main(String args[]) {
|
||||
float a[][];
|
||||
a = new float[3][4];
|
||||
a[0][0] = 100.0f;
|
||||
a[0][1] = 100.1f;
|
||||
a[2][1] = 102.1f;
|
||||
System.out.println(a[0][0]);
|
||||
System.out.println(a[0][1]);
|
||||
System.out.println(a[2][1]);
|
||||
System.out.println(a[2][3]);
|
||||
}
|
||||
}
|
||||
29
Example_src/Chapter1-10/src/ch02/UseArray.java
Normal file
29
Example_src/Chapter1-10/src/ch02/UseArray.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ch02;
|
||||
|
||||
public class UseArray {
|
||||
public static void main(String[] args) {
|
||||
int[] s1 = {5, 6, 8, 21};
|
||||
int[] s2 = new int[s1.length]; //s2必须先初始化分配空间
|
||||
int[] s3;
|
||||
int[] s4 = new int[s1.length]; //s4必须先初始化分配空间
|
||||
prnt(s1);
|
||||
System.out.println("s2 use for cycled method");
|
||||
for (int i = 0; i < s1.length; i++) { //使用for循环复制数组
|
||||
s2[i] = s1[i];
|
||||
}
|
||||
prnt(s2);
|
||||
System.out.println("s3 use clone method");
|
||||
s3 = (int[]) s1.clone();//使用clone创建sorces副本,注意clone要使用强制转换
|
||||
prnt(s3);
|
||||
System.out.println("s4 use arraycopy method");
|
||||
System.arraycopy(s1, 0, s4, 0, s1.length);
|
||||
prnt(s4);
|
||||
}
|
||||
|
||||
public static void prnt(int[] a) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
System.out.print(a[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
19
Example_src/Chapter1-10/src/ch02/Variable.java
Normal file
19
Example_src/Chapter1-10/src/ch02/Variable.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch02;
|
||||
|
||||
public class Variable {
|
||||
public static void main(String[] args) {
|
||||
char c = 'A';
|
||||
int i = 1;
|
||||
double d = 2.5;
|
||||
boolean b = true;
|
||||
final double SALES_TAX_RATE = 7.5;
|
||||
String s = "Hello Again";
|
||||
System.out.println("c =" + c);
|
||||
System.out.println("i =" + i);
|
||||
System.out.println("c =" + d);
|
||||
System.out.println("b =" + b);
|
||||
System.out.println("SALES TAX RATE =" + SALES_TAX_RATE);
|
||||
System.out.println("s =" + s);
|
||||
System.out.println("length of s =" + s.length());
|
||||
}
|
||||
}
|
||||
12
Example_src/Chapter1-10/src/ch03/Average.java
Normal file
12
Example_src/Chapter1-10/src/ch03/Average.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package ch03;
|
||||
|
||||
public class Average {
|
||||
public static void main(String[] args) {
|
||||
double n, sum = 0.0;
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
sum = sum + Double.valueOf(args[i]).doubleValue();
|
||||
}
|
||||
n = sum / args.length;
|
||||
System.out.println("平均数:" + n);
|
||||
}
|
||||
}
|
||||
14
Example_src/Chapter1-10/src/ch03/BreakLoop.java
Normal file
14
Example_src/Chapter1-10/src/ch03/BreakLoop.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ch03;
|
||||
|
||||
public class BreakLoop {
|
||||
public static void main(String args[]) {
|
||||
int i, j;
|
||||
for (i = 0; i < 6; i++) {
|
||||
for (j = 0; j < 100; j++) {
|
||||
if (j == 5) break;
|
||||
System.out.print("*");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Example_src/Chapter1-10/src/ch03/BuyChicken.java
Normal file
18
Example_src/Chapter1-10/src/ch03/BuyChicken.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ch03;
|
||||
|
||||
public class BuyChicken {
|
||||
public static void main(String[] args) {
|
||||
int I, J, K;
|
||||
System.out.println(" 公鸡数I 母鸡数J 小鸡数K ");
|
||||
for (I = 0; I <= 20; I++) // I 为公鸡数
|
||||
{
|
||||
for (J = 0; J <= 33; J++) // J 为母鸡数
|
||||
{
|
||||
K = 100 - I - J; // K 为小鸡数
|
||||
if (5 * I + 3 * J + K / 3.0 == 100)
|
||||
System.out.println(" " + I + " " + J + " " + K);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
Example_src/Chapter1-10/src/ch03/ChangeLetter.java
Normal file
21
Example_src/Chapter1-10/src/ch03/ChangeLetter.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package ch03;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ChangeLetter {
|
||||
public static void main(String args[]) {
|
||||
String s = new String("abcABC123");
|
||||
System.out.println(s);
|
||||
char a[] = s.toCharArray();
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (Character.isLowerCase(a[i])) {
|
||||
a[i] = Character.toUpperCase(a[i]);
|
||||
} else if (Character.isUpperCase(a[i])) {
|
||||
a[i] = Character.toLowerCase(a[i]);
|
||||
}
|
||||
}
|
||||
s = new String(a);
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
|
||||
25
Example_src/Chapter1-10/src/ch03/Con_Break2.java
Normal file
25
Example_src/Chapter1-10/src/ch03/Con_Break2.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package ch03;
|
||||
|
||||
class Con_Break2 {
|
||||
public static void main(String args[]) {
|
||||
int sum = 0, i, j;
|
||||
for (i = 1; i <= 10; i++) //计算1+3+5+7+9.
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
continue;
|
||||
sum = sum + i;
|
||||
}
|
||||
System.out.println("sum=" + sum);
|
||||
for (j = 2; j <= 10; j++) //求10以内的素数
|
||||
{
|
||||
for (i = 2; i <= j / 2; i++) {
|
||||
if (j % i == 0)
|
||||
break;
|
||||
}
|
||||
if (i > j / 2) {
|
||||
System.out.println("" + j + "是素数");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
Example_src/Chapter1-10/src/ch03/ForDemo.java
Normal file
12
Example_src/Chapter1-10/src/ch03/ForDemo.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package ch03;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ForDemo {
|
||||
public static void main(String args[]) {
|
||||
int a[] = {22, 2, 222, -1, -110, 0};
|
||||
Arrays.sort(a);
|
||||
for (int i = 0; i <= 5; i++) System.out.println(a[i] + ",");
|
||||
}
|
||||
}
|
||||
|
||||
10
Example_src/Chapter1-10/src/ch03/ForEachTest.java
Normal file
10
Example_src/Chapter1-10/src/ch03/ForEachTest.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package ch03;
|
||||
|
||||
public class ForEachTest {
|
||||
public static void main(String args[]) {
|
||||
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
for (int a : arr)
|
||||
System.out.println(a);
|
||||
}
|
||||
}
|
||||
|
||||
15
Example_src/Chapter1-10/src/ch03/IfDemo.java
Normal file
15
Example_src/Chapter1-10/src/ch03/IfDemo.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package ch03;
|
||||
|
||||
public class IfDemo {
|
||||
public static void main(String args[]) {
|
||||
int c1 = 2;
|
||||
int c2 = 3;
|
||||
if (c1 == 1) {
|
||||
System.out.println("c1=1");
|
||||
} else if (c2 <= 1) {
|
||||
System.out.println("c2<=1");
|
||||
} else {
|
||||
System.out.println("c1 != 1, c2>1");
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Example_src/Chapter1-10/src/ch03/NestFor.java
Normal file
13
Example_src/Chapter1-10/src/ch03/NestFor.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ch03;
|
||||
|
||||
public class NestFor {
|
||||
public static void main(String[] args) {
|
||||
int i, j;
|
||||
for (i = 0; i < 6; i++) {
|
||||
for (j = 0; j <= i; j++) {
|
||||
System.out.print("*");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Example_src/Chapter1-10/src/ch03/ReadKey.java
Normal file
19
Example_src/Chapter1-10/src/ch03/ReadKey.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch03;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ReadKey {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int score = 0;
|
||||
int sum = 0;
|
||||
int count = -1;
|
||||
System.out.println("输入分数(-1结束):");
|
||||
while (score != -1) {
|
||||
count++;
|
||||
sum += score;
|
||||
score = scanner.nextInt();
|
||||
}
|
||||
System.out.println("平均:" + (double) sum / count);
|
||||
}
|
||||
}
|
||||
21
Example_src/Chapter1-10/src/ch03/SwitchDemo.java
Normal file
21
Example_src/Chapter1-10/src/ch03/SwitchDemo.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package ch03;
|
||||
|
||||
public class SwitchDemo {
|
||||
public static void main(String args[]) {
|
||||
int c = 2;
|
||||
switch (c) {
|
||||
case 1:
|
||||
System.out.println("case 1 c=" + c);
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("case 3 c=" + c);
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("case 5 c=" + c);
|
||||
break;
|
||||
default:
|
||||
System.out.println("default c=" + c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
13
Example_src/Chapter1-10/src/ch03/TestLoan.java
Normal file
13
Example_src/Chapter1-10/src/ch03/TestLoan.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ch03;
|
||||
|
||||
public class TestLoan {
|
||||
public static void main(String args[]) {
|
||||
double dbp = 300000;
|
||||
double dbi = 0.0459 / 12;
|
||||
int nt = 30 * 12;
|
||||
double dbpow = 1;
|
||||
for (int i = 0; i < nt; i++) dbpow = dbpow * (1 + dbi);
|
||||
double dbr = dbp * dbi * dbpow / (dbpow - 1);
|
||||
System.out.println("你每月需还贷款R=" + dbr + "元/月");
|
||||
}
|
||||
}
|
||||
14
Example_src/Chapter1-10/src/ch03/WhileSum.java
Normal file
14
Example_src/Chapter1-10/src/ch03/WhileSum.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ch03;
|
||||
|
||||
public class WhileSum {
|
||||
public static void main(String args[]) {
|
||||
double sum = 0, a = 1;
|
||||
int i = 1;
|
||||
while (i <= 20) {
|
||||
a = a * (1.0 / i);
|
||||
sum = sum + a;
|
||||
i = i + 1;
|
||||
}
|
||||
System.out.println("sum=" + sum);
|
||||
}
|
||||
}
|
||||
42
Example_src/Chapter1-10/src/ch04/BackAnimal.java
Normal file
42
Example_src/Chapter1-10/src/ch04/BackAnimal.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package ch04;
|
||||
|
||||
abstract class Animal {
|
||||
public abstract void eat(); //只有方法名,而没有方法体
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
private String name;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
System.out.println("小狗啃骨头啦");
|
||||
}
|
||||
}
|
||||
|
||||
class Bird extends Animal {
|
||||
public void fly() {
|
||||
System.out.println("小鸟飞呀飞呀");
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
System.out.println("小鸟吃虫子啦");
|
||||
}
|
||||
}
|
||||
|
||||
public class BackAnimal {
|
||||
public static void main(String args[]) {
|
||||
Animal an = new Dog();
|
||||
an.eat();
|
||||
an = new Bird();
|
||||
an.eat();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
31
Example_src/Chapter1-10/src/ch04/BackwardObject.java
Normal file
31
Example_src/Chapter1-10/src/ch04/BackwardObject.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ch04;
|
||||
|
||||
class 类人猿 {
|
||||
public 类人猿() {
|
||||
System.out.println("类人猿 Created!");
|
||||
}
|
||||
|
||||
void crySpeak() {
|
||||
System.out.println(" 我是类人猿。");
|
||||
}
|
||||
}
|
||||
|
||||
class People1 extends 类人猿 {
|
||||
public People1() {
|
||||
System.out.println("People Created!");
|
||||
}
|
||||
|
||||
void crySpeak() {
|
||||
System.out.println(" 我是People。");
|
||||
}
|
||||
}
|
||||
|
||||
class BackwardObject {
|
||||
public static void main(String[] args) {
|
||||
类人猿 originalPeople = new 类人猿();
|
||||
People1 people = new People1();
|
||||
originalPeople = people;
|
||||
originalPeople.crySpeak();
|
||||
people.crySpeak();
|
||||
}
|
||||
}
|
||||
41
Example_src/Chapter1-10/src/ch04/CallHidenVar.java
Normal file
41
Example_src/Chapter1-10/src/ch04/CallHidenVar.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package ch04;
|
||||
|
||||
class Sum {
|
||||
int n;
|
||||
|
||||
float f() {
|
||||
float sum = 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
sum = sum + i;
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
class Average extends Sum {
|
||||
int n;
|
||||
|
||||
float f() {
|
||||
float c;
|
||||
super.n = n;
|
||||
c = super.f();
|
||||
return c / n;
|
||||
}
|
||||
|
||||
float g() {
|
||||
float c;
|
||||
c = super.f();
|
||||
return c / 2;
|
||||
}
|
||||
}
|
||||
|
||||
public class CallHidenVar {
|
||||
public static void main(String[] args) {
|
||||
Average aver = new Average();
|
||||
aver.n = 100;
|
||||
float result_1 = aver.f();
|
||||
float result_2 = aver.g();
|
||||
System.out.println("result_1=" + result_1);
|
||||
System.out.println("result_2=" + result_2);
|
||||
}
|
||||
}
|
||||
|
||||
19
Example_src/Chapter1-10/src/ch04/CheckFact.java
Normal file
19
Example_src/Chapter1-10/src/ch04/CheckFact.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch04;
|
||||
|
||||
class Fact {
|
||||
float fact(int n) { //定义计算n!的方法
|
||||
int i;
|
||||
float x = 1;
|
||||
for (i = 1; i < n; i++)
|
||||
x = x * n;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
public class CheckFact {
|
||||
public static void main(String[] args) {
|
||||
Fact x = new Fact();
|
||||
System.out.println(x.fact(10)); //计算10!
|
||||
System.out.println(x.fact(15)); //计算15!
|
||||
}
|
||||
}
|
||||
33
Example_src/Chapter1-10/src/ch04/CircleArea.java
Normal file
33
Example_src/Chapter1-10/src/ch04/CircleArea.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package ch04;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
|
||||
class Area {
|
||||
float fun(float r) {
|
||||
return 3.14159f * r * r;
|
||||
}
|
||||
|
||||
float get(float x, float y) {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
|
||||
class Circle extends Area {
|
||||
float fun(float r) {
|
||||
return 3.14159f * 2.0f * r;
|
||||
}
|
||||
}
|
||||
|
||||
public class CircleArea extends Applet {
|
||||
Circle yuan;
|
||||
|
||||
public void init() {
|
||||
yuan = new Circle();
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawString("调用子类重写的方法fun(): 园的面积= " + yuan.fun(5.0f), 5, 20);
|
||||
g.drawString("调用继承父类的方法get(): x+y=" + yuan.get(12.0f, 8.0f), 5, 40);
|
||||
}
|
||||
}
|
||||
18
Example_src/Chapter1-10/src/ch04/Crctngle.java
Normal file
18
Example_src/Chapter1-10/src/ch04/Crctngle.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ch04;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Crctngle {
|
||||
public static void main(String[] args) {
|
||||
Rectangle x1 = new Rectangle(); //声明类的对象并实例化
|
||||
Rectangle x2 = new Rectangle(50, 40); //声明类的对象并初始化
|
||||
Rectangle x3 = new Rectangle(x1);
|
||||
System.out.println("x1.length()=" + x1.length());
|
||||
System.out.println("x1.width()=" + x1.width());
|
||||
System.out.println("x2.length()=" + x2.length());
|
||||
System.out.println("x2.width()=" + x2.width());
|
||||
System.out.println("x3.length()=" + x3.length());
|
||||
System.out.println("x3.width()=" + x3.width());
|
||||
}
|
||||
}
|
||||
|
||||
19
Example_src/Chapter1-10/src/ch04/Demo.java
Normal file
19
Example_src/Chapter1-10/src/ch04/Demo.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch04;
|
||||
|
||||
class ABC //friendly类
|
||||
{
|
||||
public int pub_i = 5;
|
||||
|
||||
public void show() {
|
||||
System.out.println("pub_i=" + pub_i);
|
||||
}
|
||||
}
|
||||
|
||||
class Demo {
|
||||
public static void main(String[] args) {
|
||||
ABC abc = new ABC(); //使用本包中的friendly类
|
||||
System.out.println("abc.pub_i=" + abc.pub_i);
|
||||
abc.pub_i = 10;
|
||||
abc.show();
|
||||
}
|
||||
}
|
||||
20
Example_src/Chapter1-10/src/ch04/Dispatch.java
Normal file
20
Example_src/Chapter1-10/src/ch04/Dispatch.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ch04;
|
||||
|
||||
class A {
|
||||
void callme() {
|
||||
System.out.println("Inside A's callme( ) method");
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void callme() {
|
||||
System.out.println("Inside B's callme( ) method");
|
||||
}
|
||||
}
|
||||
|
||||
public class Dispatch {
|
||||
public static void main(String args[]) {
|
||||
A a = new B();
|
||||
a.callme();
|
||||
}
|
||||
}
|
||||
31
Example_src/Chapter1-10/src/ch04/DynamicState.java
Normal file
31
Example_src/Chapter1-10/src/ch04/DynamicState.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ch04;
|
||||
|
||||
class 动物 {
|
||||
void cry() {
|
||||
}
|
||||
}
|
||||
|
||||
class 狗 extends 动物 {
|
||||
void cry() {
|
||||
System.out.println("狗cry():汪汪.....");
|
||||
}
|
||||
}
|
||||
|
||||
class 猫 extends 动物 {
|
||||
void cry() {
|
||||
System.out.println("猫cry(): 喵喵.....");
|
||||
}
|
||||
}
|
||||
|
||||
class DynamicState {
|
||||
public static void main(String[] args) {
|
||||
动物 dongwu;
|
||||
if (Math.random() >= 0.5) {
|
||||
dongwu = new 狗(); //上转的对象
|
||||
dongwu.cry();
|
||||
} else {
|
||||
dongwu = new 猫(); //上转的对象
|
||||
dongwu.cry();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Example_src/Chapter1-10/src/ch04/Father.java
Normal file
11
Example_src/Chapter1-10/src/ch04/Father.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package ch04;
|
||||
|
||||
class Father {
|
||||
void speak() { //无参数的speak()方法
|
||||
System.out.println("I am Father! ");
|
||||
}
|
||||
|
||||
void speak(String s) { //有参数的speak(String s)方法
|
||||
System.out.println("I like" + " " + s + ". ");
|
||||
}
|
||||
}
|
||||
37
Example_src/Chapter1-10/src/ch04/Inheritance.java
Normal file
37
Example_src/Chapter1-10/src/ch04/Inheritance.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package ch04;
|
||||
|
||||
class superClass {
|
||||
int x;
|
||||
|
||||
superClass() {
|
||||
x = 3;
|
||||
System.out.println("in superClass : x = " + x);
|
||||
}
|
||||
|
||||
void doSomething() {
|
||||
System.out.println("in superClass.doSomething( )");
|
||||
}
|
||||
}
|
||||
|
||||
class subClass extends superClass {
|
||||
int x;
|
||||
|
||||
subClass() {
|
||||
super(); //call constructor of superClass
|
||||
x = 5;
|
||||
System.out.println("in subClass : x = " + x);
|
||||
}
|
||||
|
||||
void doSomething() {
|
||||
super.doSomething(); //call method of superClass
|
||||
System.out.println("in subClass.doSomething( )");
|
||||
System.out.println("super.x = " + super.x + " sub.x = " + x);
|
||||
}
|
||||
}
|
||||
|
||||
public class Inheritance {
|
||||
public static void main(String[] args) {
|
||||
subClass subC = new subClass();
|
||||
subC.doSomething();
|
||||
}
|
||||
}
|
||||
21
Example_src/Chapter1-10/src/ch04/ItemsFibi.java
Normal file
21
Example_src/Chapter1-10/src/ch04/ItemsFibi.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package ch04;
|
||||
|
||||
class Fibi {
|
||||
public long fibinacii(int n) {
|
||||
long c = 0;
|
||||
if (n == 1 || n == 2)
|
||||
c = 1;
|
||||
else
|
||||
c = fibinacii(n - 1) + fibinacii(n - 2);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemsFibi {
|
||||
public static void main(String[] args) {
|
||||
Fibi a = new Fibi();
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.print(" " + a.fibinacii(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Example_src/Chapter1-10/src/ch04/MethodOverloadingTest.java
Normal file
24
Example_src/Chapter1-10/src/ch04/MethodOverloadingTest.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package ch04;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class MethodOverloading {
|
||||
void receive(int i) {
|
||||
System.out.println("Receive one int data");
|
||||
System.out.println("i=" + i);
|
||||
}
|
||||
|
||||
void receive(int x, int y) {
|
||||
System.out.println("Receive two int datas");
|
||||
System.out.println("x=" + x + " y=" + y);
|
||||
}
|
||||
}
|
||||
|
||||
public class MethodOverloadingTest {
|
||||
public static void main(String[] args) {
|
||||
MethodOverloading mo = new MethodOverloading();
|
||||
mo.receive(1);
|
||||
mo.receive(2, 3);
|
||||
}
|
||||
}
|
||||
|
||||
9
Example_src/Chapter1-10/src/ch04/OverLoadingDemo.java
Normal file
9
Example_src/Chapter1-10/src/ch04/OverLoadingDemo.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ch04;
|
||||
|
||||
public class OverLoadingDemo {
|
||||
public static void main(String[] args) {
|
||||
Father x = new Father();
|
||||
x.speak(); //调用无参的speak()方法
|
||||
x.speak("music"); //调用有参的speak("music")方法
|
||||
}
|
||||
}
|
||||
38
Example_src/Chapter1-10/src/ch04/People.java
Normal file
38
Example_src/Chapter1-10/src/ch04/People.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ch04;
|
||||
|
||||
import ch04.tom.langshan.NewDate;
|
||||
|
||||
|
||||
public class People {
|
||||
private String name;
|
||||
private NewDate birth;
|
||||
|
||||
public static void main(String[] args) {
|
||||
People a = new People("XiaoYu", 1989, 10, 06);
|
||||
a.output();
|
||||
}
|
||||
|
||||
public People(String n1, NewDate d1) {
|
||||
name = n1;
|
||||
birth = d1;
|
||||
}
|
||||
|
||||
public People(String n1, int y, int m, int d) {
|
||||
this(n1, new NewDate(y, m, d));
|
||||
}
|
||||
|
||||
public People() {
|
||||
this("", new NewDate());
|
||||
}
|
||||
|
||||
public int age() { //计算年龄
|
||||
return NewDate.thisyear() - birth.year(); //获得年份
|
||||
}
|
||||
|
||||
public void output() {
|
||||
System.out.println("name : " + name);
|
||||
System.out.println("birth: " + birth.toString());
|
||||
System.out.println("age : " + age());
|
||||
}
|
||||
}
|
||||
|
||||
31
Example_src/Chapter1-10/src/ch04/PrimeNumber.java
Normal file
31
Example_src/Chapter1-10/src/ch04/PrimeNumber.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ch04;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PrimeNumber {
|
||||
/*用(int) Math.sqrt(n)求出循环上限
|
||||
* isPrime()方法用来检测当前数是否为质数
|
||||
*/
|
||||
public static boolean isPrime(int num) {
|
||||
boolean prime = true;
|
||||
int limit = (int) Math.sqrt(num);
|
||||
for (int i = 2; i <= limit; i++) {
|
||||
if (num % i == 0) {
|
||||
prime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return prime;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("请输入您要判断的数:");
|
||||
int n = input.nextInt();
|
||||
if (isPrime(n)) {
|
||||
System.out.println(n + "是质数!");
|
||||
} else {
|
||||
System.out.println(n + "不是质数!");
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Example_src/Chapter1-10/src/ch04/PrvtVar.java
Normal file
20
Example_src/Chapter1-10/src/ch04/PrvtVar.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ch04;
|
||||
|
||||
public class PrvtVar {
|
||||
private int money;
|
||||
|
||||
PrvtVar() {
|
||||
money = 2000;
|
||||
}
|
||||
|
||||
private int getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PrvtVar exa = new PrvtVar();
|
||||
exa.money = 3000;
|
||||
int m = exa.getMoney(); //可访问本类成员的private变量
|
||||
System.out.println("money=" + m);
|
||||
}
|
||||
}
|
||||
29
Example_src/Chapter1-10/src/ch04/Rectangle.java
Normal file
29
Example_src/Chapter1-10/src/ch04/Rectangle.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ch04;
|
||||
|
||||
class Rectangle { //矩形类
|
||||
private int width; //矩形的宽度
|
||||
private int length; //矩形的长度
|
||||
|
||||
Rectangle() { //矩形的不带参数的构造函数,缺省的给出长(30)和宽(20)
|
||||
length = 30;
|
||||
width = 20;
|
||||
}
|
||||
|
||||
Rectangle(int l, int w) { //矩形的带参数的构造函数
|
||||
length = l;
|
||||
width = w;
|
||||
}
|
||||
|
||||
Rectangle(Rectangle r) { //此构造方法以另一个Rectangle作为参数
|
||||
width = r.width(); //通过对象调用函数并附值给相应变量
|
||||
length = r.length();
|
||||
}
|
||||
|
||||
int width() { //返回宽度
|
||||
return width;
|
||||
}
|
||||
|
||||
int length() { //返回长度
|
||||
return length;
|
||||
}
|
||||
}
|
||||
18
Example_src/Chapter1-10/src/ch04/RedButton.java
Normal file
18
Example_src/Chapter1-10/src/ch04/RedButton.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package ch04;
|
||||
|
||||
import java.applet.Applet;
|
||||
import java.awt.*;
|
||||
|
||||
public class RedButton extends Applet {
|
||||
Button redbutton;
|
||||
|
||||
public void init() {
|
||||
redbutton = new Button("我是一个红色的按钮");
|
||||
redbutton.setBackground(Color.red);
|
||||
add(redbutton);
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawString("it is a button", 30, 50);
|
||||
}
|
||||
}
|
||||
40
Example_src/Chapter1-10/src/ch04/ShowStudent.java
Normal file
40
Example_src/Chapter1-10/src/ch04/ShowStudent.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package ch04;
|
||||
|
||||
class Student {
|
||||
int number, tel = 81238888;
|
||||
String name;
|
||||
|
||||
Student(int number, String name) {
|
||||
this.number = number;
|
||||
this.name = name;
|
||||
System.out.println("Father构造:I am " + name + "my number is " + number);
|
||||
}
|
||||
|
||||
void show() {
|
||||
System.out.println("father's show(tel): tel of Student is " + tel);
|
||||
}
|
||||
}
|
||||
|
||||
class Univer_Student extends Student {
|
||||
boolean 婚否;
|
||||
int tel = 81236666;
|
||||
|
||||
Univer_Student(int number, String name, boolean b) {
|
||||
super(number, name); // 使用父类的构造方法
|
||||
婚否 = b;
|
||||
System.out.println("Son构造新增属性:婚否=" + 婚否);
|
||||
}
|
||||
|
||||
void showtel() {
|
||||
System.out.println("Son's showtel(tel):tel of Univer_Student is " + tel);
|
||||
System.out.println("Son's showtel(super.tel):tel of super is " + super.tel);
|
||||
}
|
||||
}
|
||||
|
||||
public class ShowStudent {
|
||||
public static void main(String[] args) {
|
||||
Univer_Student zhang = new Univer_Student(8030410, " XiaoBin,", false);
|
||||
zhang.showtel();
|
||||
zhang.show();
|
||||
}
|
||||
}
|
||||
38
Example_src/Chapter1-10/src/ch04/SubBoy.java
Normal file
38
Example_src/Chapter1-10/src/ch04/SubBoy.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ch04;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
|
||||
class Father1 {
|
||||
private int money;
|
||||
float weight, height;
|
||||
String head;
|
||||
|
||||
String speak(String s) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
class Son extends Father1 {
|
||||
String hand, foot;
|
||||
}
|
||||
|
||||
public class SubBoy extends Applet {
|
||||
Son boy;
|
||||
|
||||
public void init() {
|
||||
boy = new Son();
|
||||
boy.weight = 120f;
|
||||
boy.height = 1.75f;
|
||||
boy.head = "一个聪明的大脑袋,";
|
||||
boy.hand = "两只巧手,";
|
||||
boy.foot = "一双喜欢瞎跑的脚。";
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawString(boy.speak("我是儿子"), 5, 20);
|
||||
g.drawString(boy.head + boy.hand + boy.foot, 5, 40);
|
||||
g.drawString("体重:" + boy.weight + " 身高:" + boy.height, 5, 60);
|
||||
}
|
||||
}
|
||||
|
||||
58
Example_src/Chapter1-10/src/ch04/Taper.java
Normal file
58
Example_src/Chapter1-10/src/ch04/Taper.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package ch04;
|
||||
|
||||
class 圆 {
|
||||
double 半径;
|
||||
|
||||
圆(double r) {
|
||||
半径 = r;
|
||||
}
|
||||
|
||||
double 计算面积() {
|
||||
return 3.14 * 半径 * 半径;
|
||||
}
|
||||
|
||||
void 修改半径(double 新半径) {
|
||||
半径 = 新半径;
|
||||
}
|
||||
|
||||
double 获取半径() {
|
||||
return 半径;
|
||||
}
|
||||
}
|
||||
|
||||
class 圆锥 {
|
||||
圆 底圆;
|
||||
double 高;
|
||||
|
||||
圆锥(圆 circle, double h) {
|
||||
this.底圆 = circle;
|
||||
this.高 = h;
|
||||
}
|
||||
|
||||
double 计算体积() {
|
||||
double volume;
|
||||
volume = 底圆.计算面积() * 高 / 3.0;
|
||||
return volume;
|
||||
}
|
||||
|
||||
void 修改底圆半径(double r) {
|
||||
底圆.修改半径(r);
|
||||
}
|
||||
|
||||
double 获取底圆半径() {
|
||||
return 底圆.获取半径();
|
||||
}
|
||||
}
|
||||
|
||||
public class Taper {
|
||||
public static void main(String args[]) {
|
||||
圆 circle = new 圆(10);
|
||||
圆锥 circular = new 圆锥(circle, 20);
|
||||
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
|
||||
System.out.println("圆锥的体积:" + circular.计算体积());
|
||||
circular.修改底圆半径(100);
|
||||
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
|
||||
System.out.println("圆锥的体积:" + circular.计算体积());
|
||||
}
|
||||
}
|
||||
|
||||
20
Example_src/Chapter1-10/src/ch04/TempConverter.java
Normal file
20
Example_src/Chapter1-10/src/ch04/TempConverter.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package ch04;
|
||||
|
||||
public class TempConverter {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TempConverter t = new TempConverter();
|
||||
t.data();
|
||||
}
|
||||
|
||||
protected void data() {
|
||||
for (int i = -40; i <= 120; i += 10) {
|
||||
float c = (i - 32) * (5f / 9);
|
||||
print(i, c);
|
||||
}
|
||||
}
|
||||
|
||||
protected void print(float f, float c) {
|
||||
System.out.println("华氏温度" + f + "=摄氏温度" + c);
|
||||
}
|
||||
}
|
||||
29
Example_src/Chapter1-10/src/ch04/TestAddChengji.java
Normal file
29
Example_src/Chapter1-10/src/ch04/TestAddChengji.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ch04;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
|
||||
class Chengji {
|
||||
float fun(float x, float y) {
|
||||
return x * y;
|
||||
}
|
||||
}
|
||||
|
||||
class AddChengji extends Chengji {
|
||||
float fun(float x, float y) {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestAddChengji extends Applet {
|
||||
AddChengji sum;
|
||||
|
||||
public void init() {
|
||||
sum = new AddChengji();
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawString("sum=" + sum.fun(4, 6), 100, 40);
|
||||
}
|
||||
}
|
||||
|
||||
23
Example_src/Chapter1-10/src/ch04/TestPoint.java
Normal file
23
Example_src/Chapter1-10/src/ch04/TestPoint.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package ch04;
|
||||
|
||||
class Point {
|
||||
int x, y;
|
||||
|
||||
Point() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
Point(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestPoint {
|
||||
public static void main(String[] args) {
|
||||
Point p1 = new Point(34, 56);
|
||||
Point p2 = new Point(21, 67);
|
||||
p1 = p2;
|
||||
}
|
||||
}
|
||||
28
Example_src/Chapter1-10/src/ch04/Text1.java
Normal file
28
Example_src/Chapter1-10/src/ch04/Text1.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package ch04;
|
||||
|
||||
class Text1 {
|
||||
static int a; //当被定义为static类型时,为类变量,可被对象或类调用
|
||||
int b; //对象变量,只能被对象调用
|
||||
|
||||
public void display(int a, int b) { //成员方法
|
||||
System.out.println("static int a=" + a);
|
||||
System.out.println(" int b=" + b);
|
||||
}
|
||||
|
||||
public static void display(int b) {
|
||||
System.out.println("static display: int b=" + b);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
Text1 tt = new Text1();
|
||||
tt.display(5, 6);
|
||||
//Text1.display(5,6)是错误的,对象变量或对象方法只能被对象tt调用
|
||||
Text1.display(0);
|
||||
//当被定义为static类型时,为类方法,可被对象或类调用
|
||||
tt.display(23);
|
||||
tt.a = 9;
|
||||
Text1.a = 24;
|
||||
tt.b = 3;
|
||||
tt.display(a, 15);
|
||||
}
|
||||
}
|
||||
22
Example_src/Chapter1-10/src/ch04/Tongji.java
Normal file
22
Example_src/Chapter1-10/src/ch04/Tongji.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package ch04;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
|
||||
class Tongji {
|
||||
protected float fun(float x, float y) {
|
||||
return x * y;
|
||||
}
|
||||
}
|
||||
|
||||
class Addxy extends Tongji {
|
||||
// float fun(float x,float y) { //非法,因为降低了访问级别
|
||||
// return x+y ;
|
||||
// }
|
||||
}
|
||||
|
||||
class Subxy extends Tongji {
|
||||
public float fun(float x, float y) { //合法,没有降低访问级别.
|
||||
return x - y;
|
||||
}
|
||||
}
|
||||
19
Example_src/Chapter1-10/src/ch04/sun/com/Jerry.java
Normal file
19
Example_src/Chapter1-10/src/ch04/sun/com/Jerry.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch04.sun.com;
|
||||
|
||||
import ch04.tom.langshan.Father;
|
||||
|
||||
public class Jerry extends Father { //Jerry和Father在不同的包中.
|
||||
public Jerry() {
|
||||
super(20);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Jerry jerry = new Jerry();
|
||||
// jerry.height=12; //非法,因为Jerry没有继承友好的height.
|
||||
jerry.weight = 200; //合法.
|
||||
jerry.money = 800; //合法.
|
||||
int m = jerry.getMoney(); //合法..
|
||||
// jerry.setMoney(300); //非法,因为Jerry没有继承友好的方法setMoney.
|
||||
System.out.println("m=" + m);
|
||||
}
|
||||
}
|
||||
19
Example_src/Chapter1-10/src/ch04/tom/langshan/Father.java
Normal file
19
Example_src/Chapter1-10/src/ch04/tom/langshan/Father.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch04.tom.langshan;
|
||||
|
||||
public class Father {
|
||||
int height;
|
||||
protected int money;
|
||||
public int weight;
|
||||
|
||||
public Father(int m) {
|
||||
money = m;
|
||||
}
|
||||
|
||||
protected int getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
void setMoney(int newMoney) {
|
||||
money = newMoney;
|
||||
}
|
||||
}
|
||||
29
Example_src/Chapter1-10/src/ch04/tom/langshan/NewDate.java
Normal file
29
Example_src/Chapter1-10/src/ch04/tom/langshan/NewDate.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ch04.tom.langshan;
|
||||
|
||||
import java.util.*; //引用java.util包
|
||||
|
||||
public class NewDate {
|
||||
private int year, month, day;
|
||||
|
||||
public NewDate(int y, int m, int d) {
|
||||
year = y;
|
||||
month = (((m >= 1) & (m <= 12)) ? m : 1);
|
||||
day = (((d >= 1) & (d <= 31)) ? d : 1);
|
||||
}
|
||||
|
||||
public NewDate() {
|
||||
this(0, 0, 0);
|
||||
}
|
||||
|
||||
public static int thisyear() { //获得当年的年份
|
||||
return Calendar.getInstance().get(Calendar.YEAR);
|
||||
}
|
||||
|
||||
public int year() { //获得年份
|
||||
return year;
|
||||
}
|
||||
|
||||
public String toString() { //转化为字符串
|
||||
return year + "-" + month + "-" + day;
|
||||
}
|
||||
}
|
||||
27
Example_src/Chapter1-10/src/ch05/BackInterface.java
Normal file
27
Example_src/Chapter1-10/src/ch05/BackInterface.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package ch05;
|
||||
|
||||
interface People {
|
||||
void peopleList();
|
||||
}
|
||||
|
||||
class Student implements People {
|
||||
public void peopleList() {
|
||||
System.out.println("I’m a student.");
|
||||
}
|
||||
}
|
||||
|
||||
class Teacher implements People {
|
||||
public void peopleList() {
|
||||
System.out.println("I’m a teacher.");
|
||||
}
|
||||
}
|
||||
|
||||
public class BackInterface {
|
||||
public static void main(String[] args) {
|
||||
People a; // 声明接口变量
|
||||
a = new Student(); // 实例化,接口变量中存放对象的引用
|
||||
a.peopleList(); // 接口回调
|
||||
a = new Teacher(); // 实例化,接口变量中存放对象的引用
|
||||
a.peopleList(); // 接口回调
|
||||
}
|
||||
}
|
||||
31
Example_src/Chapter1-10/src/ch05/CommonLader.java
Normal file
31
Example_src/Chapter1-10/src/ch05/CommonLader.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ch05;
|
||||
|
||||
class 梯形 {
|
||||
float 上底, 高;
|
||||
static float 下底;
|
||||
|
||||
梯形(float x, float y, float h) {
|
||||
上底 = x;
|
||||
下底 = y;
|
||||
高 = h;
|
||||
}
|
||||
|
||||
float 获取下底() {
|
||||
return 下底;
|
||||
}
|
||||
|
||||
void 修改下底(float b) {
|
||||
下底 = b;
|
||||
}
|
||||
}
|
||||
|
||||
public class CommonLader {
|
||||
public static void main(String[] args) {
|
||||
梯形 laderOne = new 梯形(3.0f, 10.0f, 20), laderTwo = new 梯形(2.0f, 3.0f, 10);
|
||||
System.out.println("laderOne的下底:" + laderOne.获取下底());
|
||||
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
|
||||
laderTwo.修改下底(60);
|
||||
System.out.println("laderOne的下底:" + laderOne.获取下底());
|
||||
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
|
||||
}
|
||||
}
|
||||
35
Example_src/Chapter1-10/src/ch05/ExampleInterface.java
Normal file
35
Example_src/Chapter1-10/src/ch05/ExampleInterface.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package ch05;
|
||||
|
||||
interface 收费 {
|
||||
public void 收取费用();
|
||||
}
|
||||
|
||||
class 公共汽车 implements 收费 {
|
||||
public void 收取费用() {
|
||||
System.out.println("公共汽车:一元/张,不计算公里数");
|
||||
}
|
||||
}
|
||||
|
||||
class 出租车 implements 收费 {
|
||||
public void 收取费用() {
|
||||
System.out.println("出租车:1.60元/公里,起价3公里");
|
||||
}
|
||||
}
|
||||
|
||||
class 电影院 implements 收费 {
|
||||
public void 收取费用() {
|
||||
System.out.println("电影院:门票,十元/张");
|
||||
}
|
||||
}
|
||||
|
||||
class ExampleInterface {
|
||||
public static void main(String[] args) {
|
||||
公共汽车 七路 = new 公共汽车();
|
||||
出租车 大众 = new 出租车();
|
||||
电影院 更俗剧场 = new 电影院();
|
||||
七路.收取费用();
|
||||
大众.收取费用();
|
||||
更俗剧场.收取费用();
|
||||
|
||||
}
|
||||
}
|
||||
38
Example_src/Chapter1-10/src/ch05/MemberTest.java
Normal file
38
Example_src/Chapter1-10/src/ch05/MemberTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ch05;
|
||||
|
||||
class Member {
|
||||
static int classVar;
|
||||
int instanceVar;
|
||||
|
||||
static void setClassVar(int i) {
|
||||
classVar = i;
|
||||
// instanceVar=i; // 类方法不能访问实例变量
|
||||
}
|
||||
|
||||
static int getClassVar() {
|
||||
return classVar;
|
||||
}
|
||||
|
||||
void setInstanceVar(int i) {
|
||||
classVar = i; //实例方法不但可以访问类变量,也可以实例变量
|
||||
instanceVar = i;
|
||||
}
|
||||
|
||||
int getInstanceVar() {
|
||||
return instanceVar;
|
||||
}
|
||||
}
|
||||
|
||||
public class MemberTest {
|
||||
public static void main(String[] args) {
|
||||
Member m1 = new Member();
|
||||
Member m2 = new Member();
|
||||
m1.setClassVar(1);
|
||||
m2.setClassVar(2);
|
||||
System.out.println("m1.classVar=" + m1.getClassVar() + " m2.ClassVar=" + m2.getClassVar());
|
||||
m1.setInstanceVar(11);
|
||||
m2.setInstanceVar(22);
|
||||
System.out.println("m1.InstanceVar=" + m1.getInstanceVar() + " m2.InstanceVar=" + m2.getInstanceVar());
|
||||
}
|
||||
}
|
||||
|
||||
21
Example_src/Chapter1-10/src/ch05/Rectangle.java
Normal file
21
Example_src/Chapter1-10/src/ch05/Rectangle.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package ch05;
|
||||
|
||||
class Rectangle { //矩形类
|
||||
int width; // 矩形的宽
|
||||
|
||||
int usethis(int width) { //返回宽度的函数
|
||||
this.width = width; //指自己这个对象
|
||||
return width;
|
||||
}
|
||||
|
||||
int unusethis(int width) {
|
||||
int w = width;
|
||||
return w;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Rectangle r = new Rectangle(); //类对象的实例化
|
||||
System.out.println("r.width=" + r.width + " r.usethis(1)=" + r.usethis(1) + " r.width=" + r.width);
|
||||
System.out.println("r.width=" + r.width + " r.unusethis(2)=" + r.unusethis(2) + " r.width=" + r.width);
|
||||
}
|
||||
}
|
||||
58
Example_src/Chapter1-10/src/ch05/ShapeArea.java
Normal file
58
Example_src/Chapter1-10/src/ch05/ShapeArea.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package ch05;
|
||||
|
||||
abstract class 图形 {
|
||||
public abstract double 求面积();
|
||||
}
|
||||
|
||||
class 梯形1 extends 图形 {
|
||||
double a, b, h;
|
||||
梯形1(double a, double b, double h) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.h = h;
|
||||
}
|
||||
public double 求面积() {
|
||||
return ((1 / 2.0) * (a + b) * h);
|
||||
}
|
||||
}
|
||||
|
||||
class 圆形 extends 图形 {
|
||||
double r;
|
||||
圆形(double r) {
|
||||
this.r = r;
|
||||
}
|
||||
public double 求面积() {
|
||||
return (3.14 * r * r);
|
||||
}
|
||||
}
|
||||
|
||||
class 堆 {
|
||||
图形 底;
|
||||
double 高;
|
||||
堆(图形 底, double 高) {
|
||||
this.底 = 底;
|
||||
this.高 = 高;
|
||||
}
|
||||
void 换底(图形 底) {
|
||||
this.底 = 底;
|
||||
}
|
||||
public double 求体积() {
|
||||
return (底.求面积() * 高) / 3.0;
|
||||
}
|
||||
}
|
||||
|
||||
public class ShapeArea {
|
||||
public static void main(String[] args) {
|
||||
堆 zui;
|
||||
图形 tuxing;
|
||||
tuxing = new 梯形1(2.0, 7.0, 10.7);
|
||||
System.out.println("梯形的面积" + tuxing.求面积());
|
||||
zui = new 堆(tuxing, 30);
|
||||
System.out.println("梯形底的堆的体积" + zui.求体积());
|
||||
tuxing = new 圆形(10);
|
||||
System.out.println("半径是10的圆的面积" + tuxing.求面积());
|
||||
zui.换底(tuxing);
|
||||
System.out.println("圆形底的堆的体积" + zui.求体积());
|
||||
}
|
||||
}
|
||||
|
||||
16
Example_src/Chapter1-10/src/ch05/StaticImportTest.java
Normal file
16
Example_src/Chapter1-10/src/ch05/StaticImportTest.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package ch05;
|
||||
|
||||
import static java.lang.Math.sqrt; //导入静态方法sqrt()
|
||||
import static java.lang.Math.pow; //导入静态方法pow()
|
||||
|
||||
class Hypot {
|
||||
public static void main(String args[]) {
|
||||
double side1, side2;
|
||||
double hypot;
|
||||
side1 = 3.0;
|
||||
side2 = 4.0;
|
||||
// 静态导入后,不再需要通过类名Math来调用方法sqrt()和pow()
|
||||
hypot = sqrt(pow(side1, 2) + pow(side2, 2));
|
||||
System.out.println("给定RT△的两边边长为:" + side1 + "和" + side2 + ",其斜边边长为:" + hypot);
|
||||
}
|
||||
}
|
||||
27
Example_src/Chapter1-10/src/ch05/Stu1.java
Normal file
27
Example_src/Chapter1-10/src/ch05/Stu1.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package ch05;
|
||||
|
||||
public class Stu1 implements Student_info //实现学生情况接口
|
||||
{
|
||||
String name;
|
||||
int birth_year; //类自己的成员变量
|
||||
|
||||
public Stu1(String n1, int y) {
|
||||
name = n1;
|
||||
birth_year = y;
|
||||
}
|
||||
|
||||
public int age() //实现接口的方法
|
||||
{
|
||||
return year - birth_year;
|
||||
}
|
||||
|
||||
public void output() //实现接口的方法
|
||||
{
|
||||
System.out.println(this.name + " " + this.age() + "岁");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stu1 s1 = new Stu1("李明", 1980);
|
||||
s1.output();
|
||||
}
|
||||
}
|
||||
9
Example_src/Chapter1-10/src/ch05/Student_info.java
Normal file
9
Example_src/Chapter1-10/src/ch05/Student_info.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ch05;
|
||||
|
||||
interface Student_info {
|
||||
int year = 2002;
|
||||
|
||||
int age();
|
||||
|
||||
void output();
|
||||
}
|
||||
40
Example_src/Chapter1-10/src/ch05/SuperDemo.java
Normal file
40
Example_src/Chapter1-10/src/ch05/SuperDemo.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package ch05;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class SuperClass { //定义父类
|
||||
int x;
|
||||
SuperClass() { //父类的构造方法
|
||||
x = 10;
|
||||
}
|
||||
void doClass() {
|
||||
System.out.println("SuperClass.doClass()");
|
||||
}
|
||||
}
|
||||
|
||||
class SubClass extends SuperClass { //定义子类
|
||||
int x;
|
||||
SubClass() { //子类的构造方法
|
||||
super(); //调用父类的构造方法
|
||||
x = 100;
|
||||
}
|
||||
void doClass() { //重写父类的doClass方法
|
||||
System.out.println("SubClass.doClass()");
|
||||
}
|
||||
void doDemo() { //演示super和this的方法
|
||||
int x;
|
||||
x = 1000;
|
||||
super.doClass(); //调用父类的doClass方法
|
||||
doClass(); //调用本类的doClass方法
|
||||
System.out.println("super.x=" + super.x); //父类的x
|
||||
System.out.println("this.x=" + this.x); //本类的x
|
||||
System.out.println("x=" + x); //本方法的x
|
||||
}
|
||||
}
|
||||
|
||||
public class SuperDemo {
|
||||
public static void main(String[] args) { //主方法
|
||||
SubClass s = new SubClass();
|
||||
s.doDemo();
|
||||
}
|
||||
}
|
||||
22
Example_src/Chapter1-10/src/ch05/TestAbstract.java
Normal file
22
Example_src/Chapter1-10/src/ch05/TestAbstract.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package ch05;
|
||||
|
||||
abstract class C {
|
||||
abstract void callme();
|
||||
void metoo() {
|
||||
System.out.println("Inside C's metoo( ) method");
|
||||
}
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
void callme() {
|
||||
System.out.println("Inside D's callme( ) method");
|
||||
}
|
||||
}
|
||||
|
||||
public class TestAbstract {
|
||||
public static void main(String[] args) {
|
||||
C c = new D();
|
||||
c.callme();
|
||||
c.metoo();
|
||||
}
|
||||
}
|
||||
53
Example_src/Chapter1-10/src/ch05/TestInterface.java
Normal file
53
Example_src/Chapter1-10/src/ch05/TestInterface.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package ch05;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
|
||||
interface Computable {
|
||||
final int MAX = 100;
|
||||
void speak(String s);
|
||||
int f(int x);
|
||||
float g(float x, float y);
|
||||
}
|
||||
|
||||
class China implements Computable {
|
||||
int xuehao;
|
||||
public int f(int x) { //不要忘记public关键字.
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= x; i++) {
|
||||
sum = sum + i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public float g(float x, float y) {
|
||||
return 6; //至少有return语句.
|
||||
}
|
||||
public void speak(String s) {
|
||||
}
|
||||
}
|
||||
|
||||
class Japan implements Computable {
|
||||
int xuehao;
|
||||
public int f(int x) {
|
||||
return 68;
|
||||
}
|
||||
public float g(float x, float y) {
|
||||
return x + y;
|
||||
}
|
||||
public void speak(String s) {
|
||||
//必须有方法体,但体内可以没有任何语句.
|
||||
}
|
||||
}
|
||||
|
||||
public class TestInterface {
|
||||
public static void main(String[] args) {
|
||||
China Li;
|
||||
Japan Henlu;
|
||||
Li = new China();
|
||||
Henlu = new Japan();
|
||||
Li.xuehao = 991898;
|
||||
Henlu.xuehao = 941448;
|
||||
System.out.println("学号:" + Li.MAX + Li.xuehao + "从1到100 求和" + Li.f(100));
|
||||
System.out.println("学号:" + Henlu.MAX + Henlu.xuehao + "加法" + Henlu.g(2.0f, 3.0f));
|
||||
}
|
||||
}
|
||||
22
Example_src/Chapter1-10/src/ch05/Triangle.java
Normal file
22
Example_src/Chapter1-10/src/ch05/Triangle.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package ch05;
|
||||
|
||||
class 三角形 {
|
||||
double a, b, c;
|
||||
|
||||
三角形(double a, double b, double c) {
|
||||
setABC(this, a, b, c);
|
||||
}
|
||||
|
||||
void setABC(三角形 trangle, double a, double b, double c) {
|
||||
trangle.a = a;
|
||||
trangle.b = b;
|
||||
trangle.c = c;
|
||||
}
|
||||
}
|
||||
|
||||
class Triangle {
|
||||
public static void main(String[] args) {
|
||||
三角形 tra = new 三角形(3, 4, 5);
|
||||
System.out.print("三角形型的三边是:" + tra.a + "," + tra.b + "," + tra.c + ",");
|
||||
}
|
||||
}
|
||||
48
Example_src/Chapter1-10/src/ch06/ArrGen.java
Normal file
48
Example_src/Chapter1-10/src/ch06/ArrGen.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package ch06;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Student {
|
||||
private String name;
|
||||
private int score;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public Student(String name, int score) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name + "的成绩为:" + score + "分";
|
||||
}
|
||||
}
|
||||
|
||||
public class ArrGen {
|
||||
public static void main(String[] args) {
|
||||
Student stu1 = new Student("Smith", 70);
|
||||
Student stu2 = new Student("John", 89);
|
||||
List<Student> newsStu = new ArrayList<Student>();
|
||||
newsStu.add(stu1);
|
||||
newsStu.add(stu2);
|
||||
for (Student s : newsStu) {
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Example_src/Chapter1-10/src/ch06/ArrayListExample.java
Normal file
19
Example_src/Chapter1-10/src/ch06/ArrayListExample.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ch06;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ArrayListExample {
|
||||
public static void main(String[] args) {
|
||||
List list = new ArrayList();
|
||||
list.add("1");
|
||||
list.add("2");
|
||||
list.add(2, "3");
|
||||
System.out.println("list集合有" + list.size() + "个元素");
|
||||
list.remove(1);
|
||||
System.out.println("删除一个元素后,list集合有" + list.size() + "个元素");
|
||||
if (list.contains("3")) {
|
||||
System.out.println("集合中存在元素\"3\"");
|
||||
}
|
||||
System.out.println("\"1\"的索引位置是:" + list.indexOf("1"));
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user