将信用卡和借记卡 类 链接到银行帐户 Class
Linking Credit Card and Debit Card classes to a Bank Account Class
我正在学习 Java,我的一项作业是创建信用卡和借记卡 classes,以便您可以在创建新卡时创建链接到新个人帐户的新卡.信用卡应该存储购买,当用户支付(全部或部分)时,从账户中扣除金额(加上 3% 的利息)。借记卡立即从账户中扣除购买金额。
我已经编写了所有内容并使其运行,但仅限于在帐户 class 中嵌套 classes。有没有一种方法可以将帐户、信用卡和借记卡作为三个独立的 classes,这样每次您创建一个新帐户时,您都可以选择创建链接到每个新个人帐户的卡?
免责声明:我正在删减部分代码,因为它们都是西班牙语,所以我不必全部翻译(主要是用于检查正数和类似内容的布尔值),但代码仍然有效.非常感谢!
package accounts;
public class Account {
protected static double balance;
protected static String accountNumber;
public Account() {
}
public Account(String accountNumber, double balance) {
Account.accountNumber = accountNumber;
Account.balance = balance;
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void deposit(double depositAmount) {
Account.balance += depositAmount;
}
public void extraction(double extractionAmount) {
Account.balance -= extractionAmount;
}
public void showBalance() {
System.out.println("Your current balance is: " + getBalance());
}
protected static class DebitCard {
private String userName;
private int cardNumber;
public DebitCard () {
}
public DebitCard (String userName, int cardNumber) {
this.userName = userName;
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public void debitPurchase(double purchaseAmount) {
Account.balance -= purchaseAmount;
}
}
protected static class CreditCard {
private String userName;
private int cardNumber;
private double creditCardDebt;
private double limit;
public CreditCard () {
}
public CreditCard(String userName, int cardNumber, double limit) {
this.userName = userName;
this.cardNumber = cardNumber;
this.limit = limit;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public double getLimit() {
return limit;
}
public double getCrediCardDebt() {
return creditCardDebt;
}
public void creditCardPurchase(double purchaseAmount) {
if (this.creditCardDebt + purchaseAmount > this.limit) {
Error notEnoughLimit = new Error("There's not enough limit to make this purchase");
throw notEnoughLimit ;
} else {
this.creditCardDebt += purchaseAmount + (purchaseAmount * 0.03);
this.limit -= purchaseAmount;
this.creditCardDebt += purchaseAmount + (purchaseAmount* 0.03);
this.limit -= purchaseAmount;
}
}
public void payCard(double payAmount) {
Account.balance -= payAmount;
this.creditCardDebt = this.creditCardDebt - payAmount;
}
}
}
是的,这是可能的。但在此之前,您应该解决将 balance
和 accountNumber
声明为 static
变量所引起的问题。通过这样做,您使每个 Account
实例共享一个帐号和一个余额。
这些变量也应该是 private
以便其他 类 无法直接访问或更改它们。
一旦你解决了这个问题,你就应该更改 Credit
和 Debit
的构造函数,将 Account
的 实例 作为一个参数。他们将其存储在私有字段中,然后通过对 Account
对象的操作来执行从 Account
添加和删除钱的操作。
我正在学习 Java,我的一项作业是创建信用卡和借记卡 classes,以便您可以在创建新卡时创建链接到新个人帐户的新卡.信用卡应该存储购买,当用户支付(全部或部分)时,从账户中扣除金额(加上 3% 的利息)。借记卡立即从账户中扣除购买金额。
我已经编写了所有内容并使其运行,但仅限于在帐户 class 中嵌套 classes。有没有一种方法可以将帐户、信用卡和借记卡作为三个独立的 classes,这样每次您创建一个新帐户时,您都可以选择创建链接到每个新个人帐户的卡?
免责声明:我正在删减部分代码,因为它们都是西班牙语,所以我不必全部翻译(主要是用于检查正数和类似内容的布尔值),但代码仍然有效.非常感谢!
package accounts;
public class Account {
protected static double balance;
protected static String accountNumber;
public Account() {
}
public Account(String accountNumber, double balance) {
Account.accountNumber = accountNumber;
Account.balance = balance;
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void deposit(double depositAmount) {
Account.balance += depositAmount;
}
public void extraction(double extractionAmount) {
Account.balance -= extractionAmount;
}
public void showBalance() {
System.out.println("Your current balance is: " + getBalance());
}
protected static class DebitCard {
private String userName;
private int cardNumber;
public DebitCard () {
}
public DebitCard (String userName, int cardNumber) {
this.userName = userName;
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public void debitPurchase(double purchaseAmount) {
Account.balance -= purchaseAmount;
}
}
protected static class CreditCard {
private String userName;
private int cardNumber;
private double creditCardDebt;
private double limit;
public CreditCard () {
}
public CreditCard(String userName, int cardNumber, double limit) {
this.userName = userName;
this.cardNumber = cardNumber;
this.limit = limit;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public double getLimit() {
return limit;
}
public double getCrediCardDebt() {
return creditCardDebt;
}
public void creditCardPurchase(double purchaseAmount) {
if (this.creditCardDebt + purchaseAmount > this.limit) {
Error notEnoughLimit = new Error("There's not enough limit to make this purchase");
throw notEnoughLimit ;
} else {
this.creditCardDebt += purchaseAmount + (purchaseAmount * 0.03);
this.limit -= purchaseAmount;
this.creditCardDebt += purchaseAmount + (purchaseAmount* 0.03);
this.limit -= purchaseAmount;
}
}
public void payCard(double payAmount) {
Account.balance -= payAmount;
this.creditCardDebt = this.creditCardDebt - payAmount;
}
}
}
是的,这是可能的。但在此之前,您应该解决将 balance
和 accountNumber
声明为 static
变量所引起的问题。通过这样做,您使每个 Account
实例共享一个帐号和一个余额。
这些变量也应该是 private
以便其他 类 无法直接访问或更改它们。
一旦你解决了这个问题,你就应该更改 Credit
和 Debit
的构造函数,将 Account
的 实例 作为一个参数。他们将其存储在私有字段中,然后通过对 Account
对象的操作来执行从 Account
添加和删除钱的操作。