Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PrintWriter on an object in a class
    text
    copied!<p>I have a class that test for the amount of money put into a vending machine. The initial values are stored in a txt file. I'm trying to use a printWriter to take the user input and store it in the txt file. For example, the starting amount of dimes is 5. The user puts in 2 dimes and the new amount in the text file is 7.</p> <p>This is all Money.txt has in it:</p> <pre><code>5 //nickels 5 //dimes 5 //quarters 0 //halfdollars </code></pre> <p>This is my money class: public class Money</p> <pre><code>{ 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; } } </code></pre> <p>This is my vending class:</p> <pre><code> 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 &lt; 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 &lt; 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 &gt; 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!"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload