我有一门课可以测试放入自动售货机的金额。初始值存储在 txt 文件中。我正在尝试使用 printWriter 获取用户输入并将其存储在 txt 文件中。例如,一角硬币的起始数量是 5。用户输入 2 个一角硬币,文本文件中的新数量是 7。
这就是 Money.txt 的全部内容:
5 //nickels
5 //dimes
5 //quarters
0 //halfdollars
这是我的理财课程:public class Money
{
private int nickels;
private int quarters;
private int dimes;
private int halfs;
public void increNickels() {
nickels ++;
}
public int getNickels(){
return nickels;
}
public void increQuarters(){
quarters ++;
}
public int getQuarters(){
return quarters;
}
public void increDimes(){
dimes ++;
}
public int getDimes(){
return dimes;
}
public void increHalfs(){
halfs ++;
}
public int getHalfs(){
return halfs;
}
}
这是我的自动售货班:
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.ArrayList;
/**
*
* @author Mira and Ty
*/
public class VendingClass {
Money moneyObj;
public Item [] itemList;
public Money [] moneyList;
Integer noCode = null;
int itemChoiceNum;
public VendingClass(){
moneyObj = new Money();
itemList = new Item [4];
moneyList = new Money [4];
}
public void setItemList() throws IOException {
String welcome = "Welcome to the \n \t\tPolemon Distribution Center \n \t\t\tPlease note that if there is not enough \nmoney in "
+ "machine correct change \nwill not be given."; // creates welcome message
JOptionPane.showMessageDialog(null, welcome);
int itemQuantity; //variable that will display item code
String items; //creates the String variable items which elements from itemNames array will be stored into
int itemCost; //creates the integer variable itemCost whic elements from itemCosts array will be stored into
File itemFile = new File("items.txt");
Scanner itemScan = new Scanner(itemFile);
int code = -1;
for(int x = 0; x < itemList.length; x++){ //for loop addes the elements of each array and itemCode to itemList object
items = itemScan.nextLine();
itemCost = itemScan.nextInt();
itemQuantity = itemScan.nextInt();
itemScan.nextLine();
code++;
itemList[x] = new Item(code, items, itemCost, itemQuantity);
}
itemScan.close();
}
public void setMoney() throws IOException {
File moneyFile = new File("Money.txt");
Scanner moneyScan = new Scanner(moneyFile);
for(int x = 0; x < moneyList.length; x++){
int nickels = moneyScan.nextInt();
int dimes = moneyScan.nextInt();
int quarters = moneyScan.nextInt();
int halfs = moneyScan.nextInt();
}
moneyScan.close();
}
public void displayVend(){
String itemChoice = JOptionPane.showInputDialog(null, itemList);
itemChoiceNum = Integer.parseInt(itemChoice);
if(itemChoice == null){
JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
}
JOptionPane.showMessageDialog(null, "You've caught a " + itemList[itemChoiceNum].getItem());
}
public void insertMoney() throws IOException{
int moneyNeeded = itemList[itemChoiceNum].getCost();//price of item will be decremented
while(moneyNeeded > 0){ //while loop testing the amount of money added to vending machine
String message = JOptionPane.showInputDialog(null, "Pokemon: " + itemList[itemChoiceNum].getItem() + "\n You owe" + moneyNeeded + "\nEnter 50, 25, 10, or 5");
PrintWriter mw = new PrintWriter("Money.txt");
int userMoney = Integer.parseInt(message);
moneyNeeded = moneyNeeded - userMoney;
if(userMoney == 5){
mw.moneyObj.increNickels();
}
if(userMoney == 10){
mw.moneyObj.increDimes();
}
if(userMoney == 25){
mw.moneyObj.increQuarters();
}
if(userMoney == 50){
mw.moneyObj.increHalfs();
}
if(message == null){
JOptionPane.showMessageDialog(null, "Thank you for your service. Goodbye!");
System.exit(0);
}
}
JOptionPane.showMessageDialog(null, "Thank you for coming to the Pokemon Distribution Center! \nWe hope you and your " + itemList[itemChoiceNum].getItem() + " catch them all!");
}
}