同じ波長で通信できるように、前に簡単な背景を説明します。データ構造からプログラミング、すべての言語、Java や C++ などの特定の言語まで、大学で 8 ~ 10 のコースを受講しました。私は通常、コーディングから 2 ~ 3 か月の休憩を取るため、少しさびています。これは私が 2 年前に考え始めた個人的なプロジェクトです。
詳細と特定の質問に至るまで、ミューテーター機能に問題があります。プライベート変数に間違ってアクセスしようとしているようです。問題は、クラスをネストしすぎて、基本クラスの変数を間違った方法で変更しようとしていないかということです。その場合は、正しい文献を教えてください。または、これが私の問題であることを確認して、この情報を再調査できるようにしてください。ありがとう
package GroceryReceiptProgram;
import java.io.*;
import java.util.Vector;
public class Date {
private int hour, minute, day, month, year;
Date() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What's the hour? (Use 1-24 military notation");
hour = Integer.parseInt(keyboard.readLine());
System.out.println("what's the minute? ");
minute = Integer.parseInt(keyboard.readLine());
System.out.println("What's the day of the month?");
day = Integer.parseInt(keyboard.readLine());
System.out.println("Which month of the year is it, use an integer");
month = Integer.parseInt(keyboard.readLine());
System.out.println("What year is it?");
year = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (IOException e) {
System.out.println("Yo houston we have a problem");
}
}
public void setHour(int hour) {
this.hour = hour;
}
public void setHour() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What hour, use military notation?");
this.hour = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public int getHour() {
return hour;
}
public void setMinute(int minute) {
this.minute = minute;
}
public void setMinute() {
try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("What minute?");
this.minute = Integer.parseInt(keyboard.readLine());
} catch (NumberFormatException e) {
System.out.println(e.toString() + ": doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString() + ": minute shall not cooperate");
} catch (NullPointerException e) {
System.out.println(e.toString() + ": in the setMinute function of the Date class");
}
}
public int getMinute() {
return minute;
}
public void setDay(int day) {
this.day = day;
}
public void setDay() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What day 0-6?");
this.day = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public int getDay() {
return day;
}
public void setMonth(int month) {
this.month = month;
}
public void setMonth() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What month 0-11?");
this.month = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public int getMonth() {
return month;
}
public void setYear(int year) {
this.year = year;
}
public void setYear() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What year?");
this.year = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public int getYear() {
return year;
}
public void set() {
setMinute();
setHour();
setDay();
setMonth();
setYear();
}
public Vector<Integer> get() {
Vector<Integer> holder = new Vector<Integer>(5);
holder.add(hour);
holder.add(minute);
holder.add(month);
holder.add(day);
holder.add(year);
return holder;
}
};
それは明らかに Date クラスで、次はもう 1 つの基本クラス Location です。
package GroceryReceiptProgram;
import java.io.*;
import java.util.Vector;
public class Location {
String streetName, state, city, country;
int zipCode, address;
Location() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the street name");
streetName = keyboard.readLine();
System.out.println("Which state?");
state = keyboard.readLine();
System.out.println("Which city?");
city = keyboard.readLine();
System.out.println("Which country?");
country = keyboard.readLine();
System.out.println("Which zipcode?");//if not u.s. continue around this step
zipCode = Integer.parseInt(keyboard.readLine());
System.out.println("What address?");
address = Integer.parseInt(keyboard.readLine());
} catch (IOException e) {
System.out.println(e.toString());
}
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
public void setZipCode() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What zipCode?");
this.zipCode = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public void set() {
setAddress();
setCity();
setCountry();
setState();
setStreetName();
setZipCode();
}
public int getZipCode() {
return zipCode;
}
public void setAddress(int address) {
this.address = address;
}
public void setAddress() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.address = Integer.parseInt(keyboard.readLine());
keyboard.close();
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString());
}
}
public int getAddress() {
return address;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public void setStreetName() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.streetName = keyboard.readLine();
keyboard.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public String getStreetName() {
return streetName;
}
public void setState(String state) {
this.state = state;
}
public void setState() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.state = keyboard.readLine();
keyboard.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public String getState() {
return state;
}
public void setCity(String city) {
this.city = city;
}
public void setCity() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.city = keyboard.readLine();
keyboard.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public String getCity() {
return city;
}
public void setCountry(String country) {
this.country = country;
}
public void setCountry() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.country = keyboard.readLine();
keyboard.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public String getCountry() {
return country;
}
};
彼らの親(正式名称は?)クラス
package GroceryReceiptProgram;
import java.io.*;
public class FoodGroup {
private int price, count;
private Date purchaseDate, expirationDate;
private Location location;
private String name;
public FoodGroup() {
try {
setPrice();
setCount();
expirationDate.set();
purchaseDate.set();
location.set();
} catch (NullPointerException e) {
System.out.println(e.toString() + ": in the constructor of the FoodGroup class");
}
}
public void setPrice(int price) {
this.price = price;
}
public void setPrice() {
try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("What Price?");
price = Integer.parseInt(keyboard.readLine());
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString() + ": in the FoodGroup class, setPrice function");
} catch (NullPointerException e) {
System.out.println(e.toString() + ": in FoodGroup class. SetPrice()");
}
}
public int getPrice() {
return price;
}
public void setCount(int count) {
this.count = count;
}
public void setCount() {
try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("What count?");
count = Integer.parseInt(keyboard.readLine());
} catch (NumberFormatException e) {
System.out.println(e.toString() + ":doesnt seem to be a number");
} catch (IOException e) {
System.out.println(e.toString() + ": in the FoodGroup class, setCount()");
} catch (NullPointerException e) {
System.out.println(e.toString() + ": in FoodGroup class, setCount");
}
}
public int getCount() {
return count;
}
public void setName(String name) {
this.name = name;
}
public void setName() {
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What minute?");
this.name = keyboard.readLine();
} catch (IOException e) {
System.out.println(e.toString());
}
}
public String getName() {
return name;
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
public void setPurchaseDate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public void setPurchaseDate() {
this.purchaseDate.set();
}
public Date getPurchaseDate() {
return purchaseDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public void setExpirationDate() {
this.expirationDate.set();
}
public Date getExpirationDate() {
return expirationDate;
}
}
最後にメイン クラスです。これにより、このすべての作業にアクセスできます。
package GroceryReceiptProgram;
public class NewMain {
public static void main(String[] args) {
FoodGroup test = new FoodGroup();
}
}
誰かがさらに興味を持っている場合は、UML へのリンクを次に示します。