flyingsliverfin
December 4th, 2012, 06:17 AM
Hi! I'm about 3 months into java for my high school APCS class. I'm taking it far beyond what the book exercise asks, but since I'm already familiar with programming I thought I'd take the simple exercise into an expandable structure.
Basically, what I'm aiming for is to have a program that will take input and do actions: there should be 'customer' objects, which have properties like maxCredit, accountNum, balance, and totalCharge. Then they need to have functions to access these numbers and to see if the customer has exceeded their credit limit.
My problem is that I need to want to make arbitrary numbers of customers. Whenever I try to make a new customer object inside main() it tells me I cannot do that since that is trying to make a non-static call in a static reference?
Here is the code (which is not quite complete but i want to get this error sorted out because it seems pretty basic):
package ch4;
import java.util.ArrayList;
import java.util.Scanner;
public class exercise4$18 {
private class customer {
int acc, balance,total, credits,allowedCred;
customer(int acc, int balance, int total, int credits, int allowedCred) {
this.acc = acc;
this.balance = balance;
this.total = total;
this.credits = credits;
this.allowedCred = allowedCred;
}
void setAcc(int val){
this.acc = val;
}
void setBalance(int val){
this.balance = val;
}
void setTotal(int val){
this.total = val;
}
void setCedits(int val){
this.credits = val;
}
void setAllowedCred(int val){
this.allowedCred = val;
}
int difference() {
return this.allowedCred - this.total;
}
}
ArrayList<customer> c = new ArrayList<customer>();
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.printf("1: new acc\n, 2: get total charged for acc\n, 3: get credit allowed: ");
int i = input.nextInt(); //***"non static variable cannot be accessed from static context"
if (i == 1) {
makeCustomer(); //***"non static method cannot be referenced from static context
}
else if (i == 2) {
System.out.println("account number: ");
int j = input.nextInt(); //***"non static variable cannot be accessed from static context"
printTotal(j); //***"non static method cannot be referenced from static context
}
else if (i == 3) {
System.out.println("account number: ");
int j = input.nextInt(); //***"non static variable cannot be accessed from static context"
printMaxCredit(j); //***"non static method cannot be referenced from static context
}
}
}
void printTotal(int k) {
System.out.printf("Current charged total: %d",c.get(customerIndex(k)).total);
}
void printMaxCredit(int k) {
System.out.printf("Account's allowed credit is: %d", c.get(customerIndex(k)).allowedCred);
}
void makeCustomer() {
int acc, balance,total, credits,allowedCred;
System.out.println("New account #: ");
acc = input.nextInt();
System.out.println("New account startingbalance: ");
balance = input.nextInt();
System.out.println("New account starting credit: ");
credits = input.nextInt();
System.out.println("New account starting totalbalance: ");
total = input.nextInt();
System.out.println("New account allowed credit: ");
allowedCred = input.nextInt();
c.add(new customer(acc,balance,total,credits,allowedCred));
}
int customerIndex(int accNum) {
int i;
for (i = 0; i < c.size(); i++){
if (c.get(i).getAcc() == accNum){
return i;
}
}
return i;
}
}
Thanks!
Basically, what I'm aiming for is to have a program that will take input and do actions: there should be 'customer' objects, which have properties like maxCredit, accountNum, balance, and totalCharge. Then they need to have functions to access these numbers and to see if the customer has exceeded their credit limit.
My problem is that I need to want to make arbitrary numbers of customers. Whenever I try to make a new customer object inside main() it tells me I cannot do that since that is trying to make a non-static call in a static reference?
Here is the code (which is not quite complete but i want to get this error sorted out because it seems pretty basic):
package ch4;
import java.util.ArrayList;
import java.util.Scanner;
public class exercise4$18 {
private class customer {
int acc, balance,total, credits,allowedCred;
customer(int acc, int balance, int total, int credits, int allowedCred) {
this.acc = acc;
this.balance = balance;
this.total = total;
this.credits = credits;
this.allowedCred = allowedCred;
}
void setAcc(int val){
this.acc = val;
}
void setBalance(int val){
this.balance = val;
}
void setTotal(int val){
this.total = val;
}
void setCedits(int val){
this.credits = val;
}
void setAllowedCred(int val){
this.allowedCred = val;
}
int difference() {
return this.allowedCred - this.total;
}
}
ArrayList<customer> c = new ArrayList<customer>();
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.printf("1: new acc\n, 2: get total charged for acc\n, 3: get credit allowed: ");
int i = input.nextInt(); //***"non static variable cannot be accessed from static context"
if (i == 1) {
makeCustomer(); //***"non static method cannot be referenced from static context
}
else if (i == 2) {
System.out.println("account number: ");
int j = input.nextInt(); //***"non static variable cannot be accessed from static context"
printTotal(j); //***"non static method cannot be referenced from static context
}
else if (i == 3) {
System.out.println("account number: ");
int j = input.nextInt(); //***"non static variable cannot be accessed from static context"
printMaxCredit(j); //***"non static method cannot be referenced from static context
}
}
}
void printTotal(int k) {
System.out.printf("Current charged total: %d",c.get(customerIndex(k)).total);
}
void printMaxCredit(int k) {
System.out.printf("Account's allowed credit is: %d", c.get(customerIndex(k)).allowedCred);
}
void makeCustomer() {
int acc, balance,total, credits,allowedCred;
System.out.println("New account #: ");
acc = input.nextInt();
System.out.println("New account startingbalance: ");
balance = input.nextInt();
System.out.println("New account starting credit: ");
credits = input.nextInt();
System.out.println("New account starting totalbalance: ");
total = input.nextInt();
System.out.println("New account allowed credit: ");
allowedCred = input.nextInt();
c.add(new customer(acc,balance,total,credits,allowedCred));
}
int customerIndex(int accNum) {
int i;
for (i = 0; i < c.size(); i++){
if (c.get(i).getAcc() == accNum){
return i;
}
}
return i;
}
}
Thanks!