我有一个程序,通过要求用户输入他们的属性来创建一个Account对象数组,而不是系统将这些插入的输入组成一个数组。
这是我的代码
package question1;
import java.util.Date;
public class Account {
public int AccountNum;
public double BALANCE;
public Date OPENDATE;
public String OwnerName;
public Account() {
// TODO Auto-generated constructor stub
}
public Account(int accnum, double balance, Date opendate, String ownername) {
this.AccountNum = accnum;
this.BALANCE = balance;
this.OPENDATE = opendate;
this.OwnerName = ownername;
}
public int getAccountNum() {
return AccountNum;
}
public void setAccountNum(int accountNum) {
AccountNum = accountNum;
}
public double getBALANCE() {
return BALANCE;
}
public void setBALANCE(double bALANCE) {
BALANCE = bALANCE;
}
public Date getOPENDATE() {
return OPENDATE;
}
public void setOPENDATE(Date oPENDATE) {
OPENDATE = oPENDATE;
}
public String getOwnerName() {
return OwnerName;
}
public void setOwnerName(String ownerName) {
OwnerName = ownerName;
}
public double yearlyInterest(double balace) {
return balace;
}
}
package question1;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Bank {
public static void main(String[] args) {
List<Account> accounts = new ArrayList<Account>(4);
Scanner sc = new Scanner(System.in);
while(!sc.hasNext()){
System.out.println("enter your balance");
int b = sc.nextInt();
System.out.println("enter your name");
String s = sc.nextLine();
}
}
}
我不知道如何在银行类中继续以及如何使用扫描仪创建此阵列。
如果有人能帮助我,我会很感激。
答案 0 :(得分:0)
你可以做这样的事情
Scanner scanner = new Scanner(System.in);
System.out.println("Do you wanna create new account?Y/N");
while(scanner.next().equalsIgnoreCase("y")){
//Get needed information. You shouldn't take account number from user. It must be generated
System.out.println("Enter Name");
String name = scanner.next();
System.out.println("Enter Account Balance");
float balance = scanner.nextFloat();
......
//create new instance of Account and set values
Account acc = new Account();
acc.setBalance(balance);
.....
//add instance to list
accounts.add(acc);
System.out.println("Do you wanna create new account?Y/N");
}
答案 1 :(得分:0)
I think you are studying for the exams of Lebanese army LOL anw this is the code bro.
AccountDriver
//Demonstrates the BankAccount and derived classes
import java.text.*; // to use Decimal Format
public class AccountDriver
{
public static void main(String[] args)
{
double put_in = 500;
double take_out = 1000;
DecimalFormat myFormat;
String money;
String money_in;
String money_out;
boolean completed;
// to get 2 decimals every time
myFormat = new DecimalFormat("#.00");
//to test the Checking Account class
CheckingAccount myCheckingAccount =
new CheckingAccount ("Benjamin Franklin", 1000);
System.out.println ("Account Number "
+ myCheckingAccount.getAccountNumber() +
" belonging to " + myCheckingAccount.getOwner());
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("Initial balance = $" + money);
myCheckingAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = myCheckingAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(myCheckingAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test the savings account class
SavingsAccount yourAccount =
new SavingsAccount ("William Shakespeare", 400);
System.out.println ("Account Number "
+ yourAccount.getAccountNumber() +
" belonging to " + yourAccount.getOwner());
money = myFormat.format(yourAccount.getBalance());
System.out.println ("Initial balance = $" + money);
yourAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = yourAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(yourAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
yourAccount.postInterest();
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After monthly interest has been posted,"
+ "balance = $" + money);
System.out.println();
// to test the copy constructor of the savings account class
SavingsAccount secondAccount =
new SavingsAccount (yourAccount,5);
System.out.println ("Account Number "
+ secondAccount.getAccountNumber()+
" belonging to " + secondAccount.getOwner());
money = myFormat.format(secondAccount.getBalance());
System.out.println ("Initial balance = $" + money);
secondAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(secondAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
secondAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(secondAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test to make sure new accounts are numbered correctly
CheckingAccount yourCheckingAccount =
new CheckingAccount ("Issac Newton", 5000);
System.out.println ("Account Number "
+ yourCheckingAccount.getAccountNumber()
+ " belonging to "
+ yourCheckingAccount.getOwner());
}
}
BankAccount(Parent class)
//Defines any type of bank account
public abstract class BankAccount
{
// class variable so that each account has a unique number
protected static int numberOfAccounts = 100001;
// current balance in the account
private double balance;
// name on the account
private String owner;
// number bank uses to identify account
private String accountNumber;
//default constructor
public BankAccount()
{
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//standard constructor
public BankAccount(String name, double amount)
{
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//copy constructor
public BankAccount(BankAccount oldAccount, double amount)
{
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
//allows you to add money to the account
public void deposit(double amount)
{
balance = balance + amount;
}
//allows you to remove money from the account if
//enough money is available
//returns true if the transaction was completed
//returns false if the there was not enough money
public boolean withdraw(double amount)
{
boolean completed = true;
if (amount <= balance)
{
balance = balance - amount;
}
else
{
completed = false;
}
return completed;
}
//accessor method to balance
public double getBalance()
{
return balance;
}
//accessor method to owner
public String getOwner()
{
return owner;
}
//accessor method to account number
public String getAccountNumber()
{
return accountNumber;
}
//mutator method to change the balance
public void setBalance(double newBalance)
{
balance = newBalance;
}
//mutator method to change the account number
public void setAccountNumber(String newAccountNumber)
{
accountNumber = newAccountNumber;
}
}
CheckingAccount
public class CheckingAccount extends BankAccount {
private double fee = .15;
private String accountNumber = getAccountNumber() + "-10";
public CheckingAccount(String name, double amount)
{
super(name, amount);
setAccountNumber(accountNumber);
}
public boolean withdraw(double amount)
{
double finalAmount = amount + fee;
super.withdraw(finalAmount);
boolean completed = true;
return completed;
}
}
And finally, SavingsAccount
public class SavingsAccount extends BankAccount{
double rate = .025;
static int savingsNumber = 0;
private String accountNumber = super.getAccountNumber();
//Default constructor
public SavingsAccount(String name, double amount){
super(name, amount);
accountNumber = accountNumber + "-" + Integer.toString(savingsNumber);
}
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
owner = oldAccount.getOwner();
balance = oldAccount.getBalance();
}
public void postInterest(){
double interestAmount = getBalance() * .025/12;
double finalAmount = getBalance() + interestAmount;
setBalance(finalAmount);
}
public String getAccountNumber()
{
return accountNumber;
}
}