打印出内存位置而不是数据

时间:2019-05-07 22:20:02

标签: java

我正在制作有关报告和查找丢失手机的概念证明程序。我正在浏览菜单,最终打印出内存位置而不是数据。

CellPhoneClass.java

import java.io.*;
import java.util.Scanner;

public class CellPhoneClass {
    private /*int*/long cellID;
    private String cellName;
    private int cellCoordinateX;
    private int cellCoordinateY;

    public CellPhoneClass(/*int*/long id, String name)
    {
        cellID = id;
        cellName = name;
    }
    public static void registerItem(String itemID, String owner, String status) throws IOException  
    {
        FileWriter file = new FileWriter("itemRegister.txt");
        BufferedWriter writer = new BufferedWriter(file);
        writer.write(itemID + "\n");
        writer.write(owner + "\n");
        writer.write(status);
        writer.close();
        file.close();
    }
    public /*int*/long getID ()
    {
        return cellID;
    }
    public String getName()
    {
        return cellName;
    }

    public void updateCoordinate(int x, int y)
    {
        cellCoordinateX = x;
        cellCoordinateY = y;
    }
    public void reportLostItem() throws IOException
    {
        FileWriter file = new FileWriter("itemLoss.txt");
        BufferedWriter writer = new BufferedWriter(file);
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the lost item ID:");
        while(input.hasNext())
        {
            if (input.hasNextInt())
            {   
            writer.write(Integer.toString(input.nextInt()));
            }
            else
            {
                System.out.print("invalid ID!!!");
                break;
            }
        }
        input.close();
        writer.close();
        file.close();
    }
}

ItemClass.java

public class ItemClass
{
    private int CellID;
    private String CellName;
    private int cellCoordinateX;
    private int cellCoordinateY;

        public ItemClass(int CellID, String CellName, int cellCoordinateX, int cellCoordinateY)
            {

            this.CellID = CellID;
            this.CellName = CellName;
            this.cellCoordinateX = cellCoordinateX;
            this.cellCoordinateY = cellCoordinateY;


            }
        public int CellID()
        {
            return CellID;
        }

        public String CellName()
        {
            return CellName;
        }

        public int cellCoordinateX()
        {
            return cellCoordinateX;
        }

        public int cellCoordinateY()
        {
            return cellCoordinateY;
        }
}

ServerClass.java

import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.util.Random;

public class ServerClass 
{
    public static void main(String[] args) throws IOException
    {
        //Array list of CellPhoneClass which keeps track of people who register their phone
        //the string that is entered as their name is also used as the key for the hashmap
        ArrayList<CellPhoneClass> cellList = new ArrayList<CellPhoneClass>();
        HashMap<String, ItemClass> map = new HashMap<String, ItemClass>();
        Scanner in = new Scanner(System.in);
        int answer = 0;
        boolean repeat = true;
        boolean nextStep = intro(cellList, in);
        if(nextStep == true)
        {
            System.out.println("Please register your phone with the app.");
            System.out.print("Enter your phone number: ");
            long cellNum = in.nextLong();
            System.out.print("\nEnter your name: ");
            in.nextLine();
            String name = in.nextLine();
            CellPhoneClass cell = new CellPhoneClass(cellNum, name);
            cellList.add(cell);
        }
        while(repeat)
        {
            System.out.println("What would you like to do? \n\n1. Register a new item.");
            System.out.println("2. Report an item lost.");
            System.out.println("3. Report an item found.");
            System.out.println("4. Exit");
            System.out.print("\nEnter the number of what you would like to do: ");
            if(in.hasNextInt() == true)
            {
                answer = in.nextInt();
            }
            else
            {
                answer = 5;
                in.nextLine();
            }
            if(answer == 1)
            {
                readItem(map, in);
            }
            else if(answer == 2)
            {
                break;
            }
            else if(answer == 3)
            {
                break;
            }
            else if(answer == 4)
            {
                System.out.println("Good bye");
                break;
            }
            else
            {
                System.out.println("\nInvalid input.\n");
            }
        }

    }
    //this method gets run first and lets the user login or register if they are new
    //calls verify method if they claim to not be a new user and checks against ArrayList of cellphones
    //if they are a new user, then they can register their phone and it will be added to the cellList
    public static boolean intro(ArrayList<CellPhoneClass> cellList, Scanner in)
    {
        boolean register = false;
        boolean user = false;
        while(user == false)
        {
            System.out.println("Are you a new user?");
            String response = in.next();
            if(response.equalsIgnoreCase("Y"))
            {
                register = true;
                user = true;
            }
            else if(response.equalsIgnoreCase("N"))
            {
                System.out.print("Enter your phone number: ");
                in.nextLine();
                long cellNum = in.nextLong();
                System.out.print("\nEnter your name: ");
                in.nextLine();
                String name = in.nextLine();
                user = verify(cellList, cellNum, name);
                if(user == false)
                {
                    System.out.println("User not found.");
                }
                register = false;
            }
            else
            {
                System.out.println("Invalid input.");
                register = false;
            }
        }
        return register;
    }
    //verify method checks the name and number entered by the user against the celllist
    //it returns true if they have registered before or false if they have not
    //if false then it will output an error and take them through the process again
    public static boolean verify(ArrayList<CellPhoneClass> cellList, long cellNum, String name)
    {
        boolean user = false;
        while(user == false && cellList.size() != 0)
        {
            for(int i = 0; i < cellList.size(); i++)
            {
                if(name.equals(cellList.get(i).getName()))
                {
                    if(cellNum == cellList.get(i).getID())
                    {
                        user = true;
                        break;
                    }
                }
            }
        }
        return user;
    }
    //I made these methods to generate some random x and y coordinates between -150 and 150
    public static int randomX()
    {
        Random rand = new Random();
        int x = rand.nextInt(150-(-150))-150;
        return x;
    }
    public static int randomY()
    {
        Random rand = new Random();
        int y = rand.nextInt(150-(-150))-150;
        return y;
    }
    //readItem method gets info from the user and calls registerItem from cellPhoneClass

    //registerItem writes the information to the file --> itemRegister.txt

    //then readItem reads that information and creates an instance of the ItemClass

    //the instance of ItemClass is then added to the hashmap where the name of the person
    //who registered the item is used as the key
    public static void readItem(HashMap<String, ItemClass> map, Scanner in) throws IOException
    {
        String status = "";
        boolean repeat = true;
        System.out.print("To register a new item, enter an Item ID: ");
        int itemID = in.nextInt();
        System.out.print("\nNext, enter the owner's name: ");
        in.nextLine();
        String owner = in.nextLine();
        System.out.print("\nIs this item lost or found? Type L for lost or F for found.");
        String check = in.next();
        while(repeat)
        {
            if(check.equalsIgnoreCase("L"))
            {
                status = "lost";
                break;
            }
            else if(check.equalsIgnoreCase("F"))
            {
                status = "found";
                break;
            }
            else
            {
                System.out.println("That input is invalid. Type L for lost or F for found.");
            }
        }
        int cellCoordinateX = randomX();
        int cellCoordinateY = randomY();
        CellPhoneClass.registerItem(Integer.toString(itemID), owner, status);
        try
        {
            BufferedReader inputStream = new BufferedReader(new FileReader("itemRegister.txt"));
            String line = "";
            int i = 0;
            while((line = inputStream.readLine()) != null && i < 3)
            {
                if(i == 0)
                {
                    itemID = Integer.parseInt(line);
                    /*test*/System.out.println(itemID);
                }
                else if(i == 1)
                {
                    owner = line;
                    /*test*/System.out.println(owner);
                }
                else if(i == 2)
                {
                    status = line;
                    /*test*/System.out.println(status);
                }
                i++;
            }
            inputStream.close();
        }
        catch(FileNotFoundException e)
        {
            System.out.println("The file you're attempting to read from does not exist.");
        }
        catch(IOException e)
        {
            System.out.println("Error reading from the file");
        }
        ItemClass item = new ItemClass(itemID, owner, cellCoordinateX, cellCoordinateY);
        map.put(owner, item);
        /*test*/for(String key : map.keySet())
        {
            System.out.print(key + " " + map.get(key));
        }




    }
}
Are you a new user?
y
Please register your phone with the app.
Enter your phone number: 1

Enter your name: bob
What would you like to do? 

1. Register a new item.
2. Report an item lost.
3. Report an item found.
4. Exit

Enter the number of what you would like to do: 1
To register a new item, enter an Item ID: 2

Next, enter the owner's name: jon

Is this item lost or found? Type L for lost or F for found.L
2
jon
lost
jon ItemClass@5ba23b66What would you like to do? 

在不应该的情况下最终输出jon ItemClass @ 5ba23b66。它应该只打印出带有ItemID号的jon。

0 个答案:

没有答案