当前位置:首页 > VUE

vue实现atm

2026-02-25 15:31:33VUE

Vue 实现 ATM 机功能

使用 Vue 实现一个简单的 ATM 机功能,可以模拟存款、取款、查询余额等操作。以下是实现步骤:

创建 Vue 项目

使用 Vue CLI 创建一个新项目:

vue实现atm

vue create vue-atm

设计 ATM 组件

src/components 目录下创建 ATM.vue 文件,编写 ATM 机的主要逻辑。

<template>
  <div class="atm">
    <h1>ATM 机</h1>
    <div class="balance">
      <p>当前余额: {{ balance }} 元</p>
    </div>
    <div class="operations">
      <input v-model="amount" type="number" placeholder="输入金额" />
      <button @click="deposit">存款</button>
      <button @click="withdraw">取款</button>
      <button @click="checkBalance">查询余额</button>
    </div>
    <div class="message">
      <p>{{ message }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      balance: 1000,
      amount: 0,
      message: ''
    };
  },
  methods: {
    deposit() {
      if (this.amount <= 0) {
        this.message = '存款金额必须大于 0';
        return;
      }
      this.balance += this.amount;
      this.message = `成功存款 ${this.amount} 元`;
      this.amount = 0;
    },
    withdraw() {
      if (this.amount <= 0) {
        this.message = '取款金额必须大于 0';
        return;
      }
      if (this.amount > this.balance) {
        this.message = '余额不足';
        return;
      }
      this.balance -= this.amount;
      this.message = `成功取款 ${this.amount} 元`;
      this.amount = 0;
    },
    checkBalance() {
      this.message = `当前余额为 ${this.balance} 元`;
    }
  }
};
</script>

<style scoped>
.atm {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  text-align: center;
}
.operations {
  margin: 20px 0;
}
input {
  padding: 8px;
  margin-right: 10px;
}
button {
  padding: 8px 15px;
  margin: 5px;
  cursor: pointer;
}
.message {
  color: green;
  font-weight: bold;
}
</style>

在主组件中使用 ATM 组件

src/App.vue 中引入并使用 ATM 组件:

vue实现atm

<template>
  <div id="app">
    <ATM />
  </div>
</template>

<script>
import ATM from './components/ATM.vue';

export default {
  name: 'App',
  components: {
    ATM
  }
};
</script>

运行项目

启动开发服务器:

npm run serve

功能说明

  • 存款:输入金额并点击“存款”按钮,余额增加。
  • 取款:输入金额并点击“取款”按钮,余额减少(需确保余额充足)。
  • 查询余额:点击“查询余额”按钮,显示当前余额。

扩展功能

可以根据需要进一步扩展功能,例如:

  • 添加交易记录功能。
  • 实现用户登录和账户切换。
  • 添加转账功能。

以上代码实现了一个基本的 ATM 机功能,可以根据实际需求进行调整和扩展。

标签: vueatm
分享给朋友:

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…