linklistapp.java
来自「初学java的一个简单的作业题」· Java 代码 · 共 165 行
JAVA
165 行
//linkList.java
///////////////////////////////////
import java.io.*;
import java.util.*;
//-------------------------------------
class Link{
public String name;
public int num;
public double price;
public String ID;
public Link next;
//-----------------------------------
public Link(){}
//-----------------------------------
public Link(String aname,int anum, double aprice,String aID){
name=aname;
num=anum;
price=aprice;
ID=aID;
}
//----------------------------------
public void GenerateLink() throws IOException{
System.out.println("*************根据提示信息输入***********");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("输入订票人姓名: ");
name=br.readLine();
System.out.print("输入想订票张数: ");
num=Integer.parseInt(br.readLine());
System.out.print("输入所订票价格: ");
price=Double.parseDouble(br.readLine());
System.out.print("输入订票人证件号码: ");
ID=br.readLine();
Link aLink=new Link(name,num,price,ID);
}
//----------------------------------
public void displayLink(){
System.out.print("{"+name+","+num+","+price+","+ID+"}");
}
}
///////////////////////////////////////
class LinkList{
private Link first;
//---------------------------------
public LinkList(){
first=new Link();
first.next=null;
}
//---------------------------------
public boolean isEmpty(){
return(first==null);
}
//----------------------------------
public void insertFirst(Link newLink) throws IOException{
newLink.next=first.next;
first.next=newLink;
}
//-----------------------------------
public Link delete(String name){
Link current=first.next;
Link previous=first;
if(current==null){
System.out.println("nothing can be deleted!");
return null;
}
else{
while(current!=null&&!name.equals(current.name)) {
previous=current;
current=current.next;
}
if(current==null){
System.out.println("Nothing can be deleted!!");
return null;
}
else
previous.next=current.next;
}
return current;
}
//-----------------------------------
public void displayList(){
System.out.print("List (first-->last): ");
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;
}
System.out.println("");
}
}
///////////////////////////////////////
class LinkListApp {
public static void main(String[] args)throws IOException{
int totalNum=0;
double totalMoney=0;
LinkList theList=new LinkList();
while(true){
new Menu();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int choice=Integer.parseInt(br.readLine());
if(choice==1){
while(true){
Link newLink = new Link();
newLink.GenerateLink();
theList.insertFirst(newLink);
totalNum+=newLink.num;
totalMoney+=newLink.price*newLink.num;
theList.displayList();
System.out.println("go on adding?(y of Y): ");
if(br.readLine().equalsIgnoreCase("y")) continue;
else{
System.out.println("By far the total numbers of tickets is: "+totalNum);
System.out.println("By far the total money of tickets is: "+totalMoney);
break;
}
}
}
else if(choice==2){
try{
while(true){
System.out.print("Input the name you want to delete: ");
String key=br.readLine();
Link temp=new Link();
temp=theList.delete(key);
totalNum-=temp.num;
totalMoney-=temp.price*temp.num;
System.out.println("By far the total numbers of tickets is: "+totalNum);
System.out.println("By far the total money of tickets is: "+totalMoney);
theList.displayList();
System.out.println("go on deleting?(y of Y): ");
if(br.readLine().equalsIgnoreCase("y")) continue;
else break;
}
continue;
}catch(NullPointerException exc){
System.out.println("there is no link error!");
}
}
else if(choice==0)
System.exit(0);
else{
System.out.println("choice not exist!choose again!");
continue;
}
}
}
}
//////////////////////////////////////////////
class Menu {
//--------------------------------------
Menu(){
System.out.println("**********欢迎光临航空订票系统*********");
System.out.println("///////////选择1添加订票信息///////////");
System.out.println("///////////选择2删除订票信息///////////");
System.out.println("///////////选择0退出订票系统///////////");
System.out.println("***************************************");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?