java相关问题关于类和对象
发布网友
发布时间:2022-08-18 18:52
我来回答
共3个回答
热心网友
时间:2023-11-10 16:59
无聊帮你写一个,这不就是模拟一哥ATM机么 开辟一个线程就行 基本逻辑通过详细逻辑自己加
首先是DB类模拟数据库
/**
* 模拟数据库
*
* @author Administrator
*
*/
public class DB {
private static Map<Integer, Integer> accountDB = new HashMap<Integer, Integer>();
static {
for (int i = 0; i < 10; i++) {
accountDB.put(i, 100);
}
}
private DB() {
}
private static class DBFactroy {
private static DB instance = new DB();
}
public static DB getInstance() {
return DBFactroy.instance;
}
public static boolean contain(int id) {
return accountDB.containsKey(id);
}
public static int cheackBalance(int id) {
return accountDB.get(id);
}
public static boolean withdraw(int id, int money) {
int leftMoney = accountDB.get(id);
if (leftMoney >= money) {
accountDB.put(id, accountDB.get(id) - money);
return true;
}
return false;
}
public static void deposit(int id, int money) {
accountDB.put(id, accountDB.get(id) + money);
}
}
之后是Accont类模式ATM机 是线程
//ATM取款线程类
public class Account implements Runnable {
private static int currentAccountId = -1;
Scanner read = new Scanner(System.in);
// 执行界面初始化
public void init() {
System.out.println("1.查询");
System.out.println("2.取款");
System.out.println("3.存款");
System.out.println("4.返回主界面(退卡)");
}
//接受输入命令
public Integer acceptCommand(String commamd) {
System.out.println("输入".concat(commamd).concat("..."));
String input = read.nextLine();
if (input.matches("^[0-9]*$")) {
return Integer.valueOf(input);
}
return -1;
}
//操作选择
public void doOperator(int command, int accountId) {
if (!validate(accountId)) {
return;
}
switch (command) {
case 1:
cheackBalance(accountId);
break;
case 2:
withdraw(accountId);
break;
case 3:
deposit(accountId);
break;
case 4:
clearCurrentAccountId();
return;
default:
System.out.println("无次帐号请重新输入");
break;
}
}
private void clearCurrentAccountId() {
currentAccountId = -1;
}
//验证是否有当前帐号
public boolean validate(int id) {
if (!DB.contain(id)) {
return false;
}
return true;
}
//查询余额
public void cheackBalance(int id) {
System.out.println(DB.cheackBalance(id));
read.nextLine();
}
//取款
public void withdraw(int id) {
int money = acceptCommand("取钱数目");
if (!DB.withdraw(id, money)) {
System.out.println("取钱失败");
} else {
System.out.println("取钱成功");
}
read.nextLine();
}
//存款
public void deposit(int id) {
int money = acceptCommand("存钱数目");
DB.deposit(id, money);
System.out.println("存数成功");
read.nextLine();
}
//模拟主方法
public void command() {
init();
int commandNum = acceptCommand("操作列表");
if (currentAccountId == -1) {
currentAccountId = acceptCommand("帐号");
}
doOperator(commandNum, currentAccountId);
}
@Override
public void run() {
while (true) {
command();
}
}
public static void main(String[] args) {
Account ac = new Account();
Thread thread = new Thread(ac);
thread.start();
}
}
执行结果
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
1
输入帐号...
1
100
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
2
输入取钱数目...
50
取钱成功
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
1
50
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
3
输入存钱数目...
50
存数成功
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
1
100
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
4
1.查询
2.取款
3.存款
4.返回主界面(退卡)
输入操作列表...
1
输入帐号...
1
100
热心网友
时间:2023-11-10 17:00
import java.util.Scanner;
public class App {
private Account[] accountIDs;// 用户储存账户的数组
public static void main(String[] args) {
// 启动程序
App app = new App();
app.start();
}
/**
*
* @param i为传入的用户ID
*/
private void showSystemMenu(int i) {
System.out.println("account is exist");
System.out.println("================================================");
System.out.println("Main menu");
System.out.println("1:check balance");
System.out.println("2:withdraw");
System.out.println("3:deposit");
System.out.println("4:exit");
System.out.println("================================================");
System.out.println("Enter a choice: ");
Scanner input = new Scanner(System.in);
int menu = input.nextInt();
if (menu == 1) {
System.out.println("The balance is " + accountIDs[i].getBalance());
showSystemMenu(i);
} else if (menu == 2) {
System.out.println("Enter an amount to withdraw: ");
double amount = input.nextDouble();
accountIDs[i].withdraw(amount);
showSystemMenu(i);
} else if (menu == 3) {
System.out.println("Enter an amount to deposit: ");
double amount = input.nextDouble();
accountIDs[i].deposit(amount);
showSystemMenu(i);
} else if (menu == 4) {
// 提出系统
System.exit(0);
}
}
public void start() {
int id;
accountIDs = new Account[10];
// 开始先初始化十个用户,并赋初值余额100
for (int i = 0; i < 10; i++) {
Account account = new Account(i, 100);
accountIDs[i] = account;
}
System.out.println("accountIDs.length:" + accountIDs.length);
System.out.println("Enter an id: ");
Scanner input = new Scanner(System.in);
id = input.nextInt();
for (int i = 0; i < accountIDs.length; i++) {
while (id == accountIDs[i].getId()) {
showSystemMenu(i);//调用方法并传入当前的用户ID
}
}
System.out.println("输入的账户不正确");
start();
}
}
import java.util.Date;
public class Account {
private int id;
private double balance = 100;
private double annualInterestRate;
private Date dateCreated;
public Account() {
super();
}
public Account(int id, double balance) {
super();
this.id = id;
this.balance = balance;
this.dateCreated = new Date();
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public void withdraw(double amount) {
this.balance -= amount;
}
public void deposit(double amount) {
this.balance += amount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
}
热心网友
时间:2023-11-10 17:00
需要实体类吗??你这个实体类是干什么的?