新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java版水果管理系統(tǒng)源碼
水果管理系統(tǒng)Java版分享給大家。
白銀網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),白銀網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為白銀超過(guò)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的白銀做網(wǎng)站的公司定做!

主類 FruitsDemo
/**
* 功能:
* 1. 查看所有的水果
* 2. 添加新的水果(添加的時(shí)候判斷水果名是否有重復(fù))
* 3. 對(duì)所有的水果進(jìn)行排序(價(jià)格排序、庫(kù)存排序)
* 4. 刪除指定的水果
* 5. 退出系統(tǒng)
*
* 注意:
* 1. 每種水果都必須有水果id,水果名,水果數(shù)量,水果價(jià)格
* 2. 添加水果時(shí),要由用戶輸入水果名、數(shù)量和價(jià)格
* 3. 刪除水果時(shí)要二次確認(rèn)
*
* 評(píng)分依據(jù): 功能實(shí)現(xiàn)的情況,代碼規(guī)范性(命名規(guī)范、格式規(guī)范),設(shè)計(jì)的合理性
* @author yj
*
*/
public class FruitsDemo {
public static void main(String[] args) {
int select = 0; // 主菜單功能選擇
boolean isStart = true;// 程序運(yùn)行標(biāo)志位
while (isStart) {
System.out.println("******************水果管理系統(tǒng)******************\n請(qǐng)輸入下列序號(hào)選擇相應(yīng)功能:\n\n 1.查看所有的水果 \t2. 添加新的水果 \n 3.對(duì)所有的水果進(jìn)行排序查看(價(jià)格排序、庫(kù)存排序) \n 4.刪除水果\t5. 退出系統(tǒng)");
select = Calculation.inputIsInt();
switch (select) {
case 1://1.查看所有的水果
Calculation.seeAllFruits();
break;
case 2://2. 添加新的水果
Calculation.add();
break;
case 3://3.對(duì)所有的水果進(jìn)行排序查看(價(jià)格排序、庫(kù)存排序)
Calculation.Sort();
break;
case 4:// 4.刪除水果
System.out.println("請(qǐng)輸入你要?jiǎng)h除的水果");
String index = Calculation.inputIsString();
System.out.println("二次確認(rèn)?。。≌?qǐng)?jiān)俅屋斎肽阋獎(jiǎng)h除的水果");
String index1 = Calculation.inputIsString();
if(index.equals(index1)){
Calculation.remove(index);
}else{
System.out.println("兩次輸入不匹配,刪除失?。。?!");
}
break;
case 5://5. 退出系統(tǒng)
isStart = false;
break;
default:
System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入");
break;
}
}
System.out.println("程序已退出,歡迎使用?。?!");
}
}
Fruits 類
/**
* 水果類
* @author yj
*
*/
public class Fruits {
// 每種水果都必須有水果id,水果名,水果數(shù)量,水果價(jià)格
private int id;//ID
private int nums;//數(shù)量(庫(kù)存)
private String name;//水果名
private double price;//水果價(jià)格
public Fruits(int id, String name, int nums, double price) {
super();
this.id = id;
this.nums = nums;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Calculation 類
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
/**
* 計(jì)算類,存放關(guān)于計(jì)算處理數(shù)據(jù)的函數(shù)
*
* @author yj
*
*/
public class Calculation {
static LinkedList list = new LinkedList();
static Scanner sc = new Scanner(System.in);
static int id = 1;
/**
* 添加水果 get()
*/
public static void add() {
int nums;
String name;
double price;
System.out.print("請(qǐng)輸入你要添加的水果名、數(shù)量(單位:個(gè))和價(jià)格(單位:元)\n");
name = Calculation.inputIsString();
nums = Calculation.inputIsInt();
price = Calculation.inputIsDouble();
if (cals(name, nums, price)) {
list.add(new Fruits(id, name, nums, price));
id++;
}
}
/**
* 查看所有水果 seeAllFruits()
*/
public static void seeAllFruits() {
if (list.size() == 0) {
System.out.println("數(shù)據(jù)為空?。。?);
} else {
Iterator it = list.iterator();
while (it.hasNext()) {
Fruits temp = it.next();
System.out.println("ID -> " + temp.getId() + "\t水果名稱 -> " + temp.getName() + "\t水果數(shù)量 -> "
+ temp.getNums() + "\t水果價(jià)格 -> " + temp.getPrice());
}
}
}
/**
* 刪除水果 remove(String index)
*
* @param index
* 你要?jiǎng)h除的水果名
*/
public static void remove(String index) {
Iterator it = list.iterator();
while (it.hasNext()) {
if (index.equals(it.next().getName())) {
it.remove();
System.out.println(index + "已刪除");
}
}
}
/**
* 判斷是否重復(fù) cals(String name, int nums, double price)
*
* @param name
* 水果名
* @param nums
* 水果數(shù)量
* @param price
* 水果價(jià)格
* @return
*/
public static boolean cals(String name, int nums, double price) {
Iterator it1 = list.iterator();
while (it1.hasNext()) {
Fruits temp = it1.next();
if (name.equals(temp.getName())) {
temp.setNums(nums + temp.getNums());
temp.setPrice(price);
System.out.println("水果——"+name+" 已存在,數(shù)量在原基礎(chǔ)上加上 "+nums+",價(jià)格已更新為 "+price);
return false;
}
}
return true;
}
/**
* 排序輸出 Sort()
*/
public static void Sort() {
System.out.println("1.按照價(jià)格升序 2.按照庫(kù)存升序");
int n = inputIsInt();
switch (n) {
case 1:
Collections.sort(list, new Comparator() {
@Override
public int compare(Fruits o1, Fruits o2) {
if (o1.getPrice() > o2.getPrice()) {
return 1;
} else if (o1.getPrice() < o2.getPrice()) {
return -1;
} else {
return 0;
}
// return (int) (o1.getPrice() * 100 - o2.getPrice() * 100);
}
});
break;
case 2:
Collections.sort(list, new Comparator() {
@Override
public int compare(Fruits o1, Fruits o2) {
if (o1.getNums() > o2.getNums()) {
return 1;
} else if (o1.getNums() < o2.getNums()) {
return -1;
} else {
return 0;
}
// return (int) (o1.getNums() - o2.getNums());
}
});
break;
default:
System.out.println("輸入指令錯(cuò)誤?。?!");
break;
}
seeAllFruits();
}
/**
* 輸入是否是Int inputIsInt()
*
* @return
*/
public static int inputIsInt() {
boolean isRight = true;
int select = 0;
do {
try {
select = sc.nextInt();
isRight = true;
} catch (Exception e) {
System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)整數(shù)(Int)");
sc.nextLine();
isRight = false;
}
} while (!isRight);
return select;
}
/**
* 輸入是否是String inputIsString()
*
* @return
*/
public static String inputIsString() {
boolean isRight = true;
String select = null;
do {
try {
select = sc.next();
isRight = true;
} catch (Exception e) {
System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)字符串(String)");
sc.nextLine();
isRight = false;
}
} while (!isRight);
return select;
}
/**
* 輸入是否為Douuble
*
* @return
*/
public static Double inputIsDouble() {
boolean isRight = true;
Double select = null;
do {
try {
select = sc.nextDouble();
isRight = true;
} catch (Exception e) {
System.out.println("輸入類型不匹配,請(qǐng)輸入一個(gè)小數(shù)(Double)!!!");
sc.nextLine();
isRight = false;
}
} while (!isRight);
return select;
}
}
更多學(xué)習(xí)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開(kāi)發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
標(biāo)題名稱:Java版水果管理系統(tǒng)源碼
分享URL:http://fisionsoft.com.cn/article/jochco.html


咨詢
建站咨詢
