私は在庫管理アプリケーションを作成しました。ここで、ユーザーは在庫を追加、表示、削除できます。ユーザーがAを入力すると、プログラムはユーザーにデータの入力を促します。ユーザーがDを入力すると、プログラムはそのデータをインベントリーに表示します。私はこれを達成するためにLinkedListを使用します。私はこれに全く新しいです。ユーザーにデータの入力を促すことができましたが、表示できませんでした。コード上に赤い線があります。どこが間違っていたのですか?私が尋ねる方法が間違っているなら、私は残念です。私を訂正してください。データを表示する際のJava LinkedList
Main.java
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 0;
LinkedList myList = new LinkedList();
while(t != 1)
{
System.out.print("I, A, D");
char input = sc.next().charAt(0);
if(input == 'A')
{
System.out.print("Please enter item id: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Please enter item name: ");
String name = sc.nextLine();
System.out.print("Please enter item type: ");
String type = sc.nextLine();
System.out.print("Please enter item price: ");
double price = sc.nextDouble();
sc.nextLine();
Item I1 = new Item(id, name, type, price);
myList.addItemToFront(I1);
}
else if(input == 'D')
{
myList.DisplayItem(); //at this point, theres red line which i dont know why?
}
}
LinkedList.java
class LinkedList {
private Node head; // first node in the linked list
private int count;
public int getCount() {
return count;
}
public Node getHead() {
return head;
}
public LinkedList() {
head = null; // creates an empty linked list
count = 0;
}
public void addFront(int n) {
Node newNode = new Node(n);
newNode.setLink(head);
head = newNode;
count++;
}
public void deleteFront() {
if (count > 0) {
head = head.getLink();
count--;
}
}
public void addItemToFront(Item I1)
{
Node itemNode = new Node(I1);
itemNode.setLink(head);
head = itemNode;
}
public void DisplayItem(Node head)
{
if(head == null)
{
return;
}
Node current = head;
while (current != null)
{
System.out.println(current.data.toString());
current = current.getLink();
}
System.out.println(current);
}
public int length(Node head)
{
if(head == null)
{
return 0;
}
int count = 0;
Node current = head;
while(current != null)
{
count++;
current = current.getLink();
}
return count;
}
Node.java
public class Node {
Object data;
private Node link;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
public Node(Object data) {
this.data = data;
this.link = null;
}
}
サンプル出力(どのプログラムが例を実行する必要があります)
I, A, D
A
Please enter item id: 001
Please enter item name: Wooden Chair
Please enter item type: Furniture
Please enter item price: 50.30
I, A, D
D
001, Wooden Chair, Furniture, 50.30
は「_butはit_を表示することができませんでした」。 –
すみません。これはまったく新しいものです。私はそれに応じて投稿を編集した。 –
また、Nodeクラスもポストすることができます。また、次のようにして、型安全性のためにLinkedListクラスGenericを作成することも推奨されています。Nodeクラスをより一般的なものにする。 –