Hallo,
ich komme bei meiner Hausaufgabe leider nicht weiter, dies ist die Aufgabe:
Das Programm besteht aus 3 Klassen (vorgegeben)
Hier ist schon meine erste Frage kann ich das so machen mit dem Object constructor um ein Objekt Account zu erstellen?
Hier dann meine Hauptprobleme, was ich bei enroll und deposit mache funktioniert nicht (die anderen sind noch nicht eingefuellt weil ich dort das selbe Problem haette). Wie kann ich nun der arraylist ein Objekt account hinzufuegen?
Der Commandreader ist komplett so vorgegeben:
Das Programm compiled zwar aber wenn ich es laufen lasse kommt immer ein Error.
Wenn ich z.B. erst enroll und dann in der zweiten Zeile einen Namen eingebe kommt folgendes:
Danke schonmal fuer die Hilfe!
ich komme bei meiner Hausaufgabe leider nicht weiter, dies ist die Aufgabe:
You are going to build a simple system of a bank with accounts. The program reads commands from input and executes them on the accounts (such as withdrawals, deposits, enrolling of new accounts, printing, etc.).
For this assignment the command reader has already been implemented and is provided together with the other skeleton files.
You are required to use these files as the starting point for this homework assignment.
Functionality
Your program should process the following commands. All amounts are integer numbers. Interest calculations are rounded to the nearest integer.
Robustness
- deposit <name> <amount> adds <amount> to the account of <name>; you may assume that amount is positive;
- enroll <name> enrolls a new account under the name <name>; (the initial balance is 0)
- print <name> prints the name and balance of the account with name <name>.
- printAll prints the name and balance for all accounts.
- printRed only prints the name and balance of all accounts with a negative balance.
- withdraw <name> <amount> subtracts <amount> from the account of <name>.
- You may assume that amount is positive.
- Whenever the operation would result in a balance of less than -1000, the withdrawal is not performed.
- interest <rate> which adds interest to all positive accounts.
- rate is a floating point number (double) representing the percentage of the current balance that should be added.
- for negative accounts, a fixed penalty of 10% is subtracted, even if the resulting balance would be less than -1000.
- stop stops the program. (and is Already implemented within the CommandReader)
Make the program a little bit more robust and have it perform elegantly when the commands print, withdraw, and deposit are given with a name that does not exist in the accounts. No operation should be performed and the message "no such name ..." should be printed, where the given name should appear instead of the dots.
Furthermore, the command enroll with a name that is already in the accounts should result in no operation and the message "... already enrolled", where the given name should appear instead of the dots.
2. The Design
You are required to use the following design in your program (this is enforced by the skeleton files):
Account
The class Account concerns user accounts. Each object of this class has a private String name and a private int balance as instance variables.
Design methods for the class Account, including those that enable to comply with the requirement that instance variables of a class should never be accessed directly outside of the Account class.
Bank
The central class of the program is class Bank which holds the accounts in an instance variable ArrayList<Account> accounts. The banking class has at least the following methods, which each should provide the described functionality of the corresponding command as described in the first section.
Command Reader
- void deposit(String name, int amount)
- void enroll(String name)
- void interest(double rate)
- void print(String name)
- void printAll()
- void printRed()
- void withdraw(String name, int amount)
Input is read and processed by an object of the class CommandReader. You do not have to write this class. It is provided in the skeleton files.
3. Input
A list of commands in the format as specified in the first section. You may assume that every command is complete, so, e.g., deposit is always followed by a word (the name of an account) and an integer number (the amount to be withdrawn), etc.
Two features are added in CommandReader for your convenience:
4. Output
- When a word is input that is not a command, the rest of the input line will be ignored and the message unknown command will be output.
- Furthermore, command words are not case sensitive.
The data of an account with the name “piet” and balance 200 should be printed as follows:
Account of piet
balance: 200
NB. The word balance should be preceded by 2 spaces.
When printAll is issued is and there are no accounts, one line is to be printed with the text:
no accounts
When printRed is issued is and there are no accounts with a negative balance, one line is to be printed with the text:
no negative balances
Das Programm besteht aus 3 Klassen (vorgegeben)
Java:
class Account {
private String name;
private int balance;
//object constructor
Account (String name){
balance = 0; //sets initial balance to 0
this.name = name; //sets name to given name
}
//method to give name to account
void setName(String name){
this.name = name; //this refers to object on which method is called
}
//method to add specified amount to current balance
void addAmount(int amount){
balance = balance + amount;
}
}
Hier ist schon meine erste Frage kann ich das so machen mit dem Object constructor um ein Objekt Account zu erstellen?
Java:
import java.util.ArrayList;
class Bank {
Account account;
ArrayList<Account> accounts;
// creates the arraylist
void createArray() {
accounts = new ArrayList<Account>();
}
//checks if account name is in arraylist, if yes adds amount to it
void deposit(String name, int amount) {
if (accounts.contains(name)) { //doesn't work
int index = accounts.indexOf(name);
accounts.get(index).addAmount(amount);
//balance = accounts.get(index);
}else {
System.out.println("no such name "+name);
}
}
//puts a new account in arraylist if arraylist does not contain name already
void enroll(String name) {
if (accounts.contains(name)) {
System.out.println(name+" already enrolled");
}else {
accounts.add(new Account(name));
int index = accounts.indexOf(name);
accounts.get(index).setName(name);
}
}
void interest(double rate) {
}
void print(String name) {
}
void printAll() {
System.out.println(accounts);
}
void printRed() {
}
void withdraw(String name, int amount) {
}
}
Hier dann meine Hauptprobleme, was ich bei enroll und deposit mache funktioniert nicht (die anderen sind noch nicht eingefuellt weil ich dort das selbe Problem haette). Wie kann ich nun der arraylist ein Objekt account hinzufuegen?
Der Commandreader ist komplett so vorgegeben:
Java:
import java.util.Scanner;
public class CommandReader {
Bank bank;
Scanner scanner;
/**
* CommandReader constructor, creates the Bank and Scanner objects.
*/
public CommandReader() {
bank = new Bank();
scanner = new Scanner(System.in);
}
/**
* readCommands scans the input for commands and distributes the commands
* to the appropriate method in bank.
*/
public void readCommands() {
String command = "";
String name = "";
int amount = 0;
double rate = 0.0;
do {
command = scanner.next().toLowerCase();
switch (command) {
case "enroll":
name = scanner.next();
bank.enroll(name);
break;
case "deposit":
name = scanner.next();
amount = scanner.nextInt();
bank.deposit(name, amount);
break;
case "withdraw":
name = scanner.next();
amount = scanner.nextInt();
bank.withdraw(name, amount);
break;
case "print":
name = scanner.next();
bank.print(name);
break;
case "printall":
bank.printAll();
break;
case "printred":
bank.printRed();
break;
case "interest":
rate = scanner.nextDouble();
bank.interest(rate);
break;
case "stop":
// do nothing
break;
default:
System.out.println("unknown command");
//skip rest of line
scanner.nextLine();
}
} while (!command.equals("stop"));
}
/**
* Starting point of the program, creates a new commandReader and calls
* the readCommands method.
*/
public static void main(String[] args) {
CommandReader commandReader = new CommandReader();
commandReader.readCommands();
}
}
Das Programm compiled zwar aber wenn ich es laufen lasse kommt immer ein Error.
Wenn ich z.B. erst enroll und dann in der zweiten Zeile einen Namen eingebe kommt folgendes:
java.lang.NullPointerException
at Bank.enroll(Bank.java:30)
at CommandReader.readCommands(CommandReader.java:36)
at CommandReader.main(CommandReader.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Danke schonmal fuer die Hilfe!